icon sc-linkedin logo of codepen-icon logo of github-icon youtube play button

notes by Adam Sullovey

web & mobile application developer
practicing in Toronto, ON

Interview Cake Problem #11

I was on the right track with breaking down the problem. I was ready to start looking for sequences of matching cards across the deck and 2 piles, but the hint suggested to break the problem down further to matching individual cards. With that hint, I was able to implement a good solution.

I chose to actually pop and push elements from the arrays when doing comparisons between the piles instead of just incrementing an index every time a match was found. It is slower, but I think it is less prone to human off-by-one errors.

Neat JS stuff that came in handy

Need to make a shallow copy an array quickly? Try slice

var copy = original.slice(0);

Need to combine 2 arrays without creating a new array (like with concat()) ? Try using apply with push. push usually expects to receive a list of values as arguments instead of 1 array of values. apply helps with that.

original.push.apply(original, additional)

This says to run the push function in the scope of the array original, and will treat the array additional as a list of arguments the way push usually expects to receive values. The result is that the values in additional will be added to original.

comments powered by Disqus