a month ago I was interviewed by some members of the Google PTO. One of the questions: Invert string recursively to js and explain runtime with large musical notation
This was my solution:
function invert(s){ return (s.length > 1) ? s.charAt(s.length-1)+invert(s.substring(0,s.length-1)) : s; }
Pretty simple, I think.
And, about the big notation, I quickly answered O (n), since the runtime is linearly dependent on the input. “Silence,” and then, he asked me, what are the differences in deadlines if you implement it by iteration?
I replied that sometimes the compiler "translates" recursion into iteration (some language courses of the programming language), so in this case there are no differences in iteration and recursion. By the way, since I didn’t have feedback on this particular question, and the interviewer did not answer “good” or “no”, I would like to know if you agree with me or if you can explain to me whether there may be differences regarding 2 type of implementation.
Thanks a lot and welcome!
javascript big-o iteration recursion
stecb
source share