Skip to content

Instantly share code, notes, and snippets.

@r3wt
Created June 28, 2022 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r3wt/7127f71d8f12bf9cf4d9bfb0ee4ef244 to your computer and use it in GitHub Desktop.
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.
// 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