As silly as this sounds, if you can demonstrate (working) recursion during a whiteboard/screenshare interview, it often carries more weight with the interviewers than it warrants. Like you've certified yourself as a master of the dark arts.
I humbly submit that this solution is a bit more robust, because it doesn't depend upon the idea that there are given properties in the source object, or that they are set to a specific value of null. That while loop above is problematic if values other than Object/null are set on next.
Finally, by encapsulating the answer in a function, the function can truly operate as a standalone algorithm. But the while loop is dependent upon the setting of the head variable before you enter the loop.
Are all of these points incredibly nitpicky?? Yes. They absolutely are. I'm waiting for my next meeting to begin, and I'm bored. So I went through this in much more detail than you should expect in most interviews.
Cheers.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
An alternate solution using recursion:
As silly as this sounds, if you can demonstrate (working) recursion during a whiteboard/screenshare interview, it often carries more weight with the interviewers than it warrants. Like you've certified yourself as a master of the dark arts.
I humbly submit that this solution is a bit more robust, because it doesn't depend upon the idea that there are given properties in the source object, or that they are set to a specific value of
null
. Thatwhile
loop above is problematic if values other thanObject
/null
are set onnext
.Finally, by encapsulating the answer in a function, the function can truly operate as a standalone algorithm. But the
while
loop is dependent upon the setting of thehead
variable before you enter the loop.Are all of these points incredibly nitpicky?? Yes. They absolutely are. I'm waiting for my next meeting to begin, and I'm bored. So I went through this in much more detail than you should expect in most interviews.
Cheers.