Created
June 28, 2022 14:12
-
-
Save r3wt/7127f71d8f12bf9cf4d9bfb0ee4ef244 to your computer and use it in GitHub Desktop.
Splunk Interview Question - Find first duplicate in sequence whose next value is the sum of its digits squared.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// i did poorly in my interview, the solution i gave worked but it was innefficient. | |
// splunk passed on me, but i couldn't let it go. so here is my final solution of the problem | |
function findFirstDuplicateInSeq( _seq ) { | |
const seq = [..._seq];// clone the array first so we are not mutating it. | |
while(true){ | |
const next = seq[seq.length-1].toString().split('').map(v=>Math.pow(~~v,2)).reduce((a,b)=>a+b,0);// next val is sum of digits squared | |
if(seq.indexOf(next)!==-1){ | |
return seq.length+1;// if seq contains our next value already, we found the first duplicate. return length+1 | |
} | |
seq.push(next);// no match, push the next val in to the sequence | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment