The question was a new one I hadn't seen before — roughly, implement an in-memory key-value store that supports transactions. Your system needs to process a series of commands that assign values to variables, print variable values, and manage transactional blocks. The tricky part is that you can assign to a variable that was already assigned before entering the transaction.
My first instinct was to go with recursion, since the pattern in the problem was pretty obvious. But the interviewer kept pushing me to follow his own approach instead. I didn't really get why, but I figured if the interviewer was saying that, there must be something wrong with my recursive solution, so I went along with him and tried to follow his line of thinking.
Since I kept having to guess what he was actually thinking, the discussion dragged on for almost half an hour. He couldn't even explain why his solution was better. After I really couldn't guess anymore, he finally said he wanted me to use a stack. I was speechless — isn't a stack just the iterative implementation of recursion? I have no idea why he needed to block my original idea. I wasn't expecting the interviewer to go easy on me, but if you haven't even fully worked through the problem yourself and can only handle the one solution you happen to know, that's a pretty weak level for an interviewer.
Discussion
Loading comments…