There's a trap in interviews today, and I'm wrestling with how to address it.
Let's take for example the classic question of how to reverse a singly-linked list. The traditional answer is to create temp pointers so that nodes don't get lost in the shuffle, and flip the pointers from forward to backward:
n1 -> n2 -> n3 -> n4 -> n5
n1 <- n2 <- n3 <- n4 <- n5
Here I find myself caught between a rock and a hard-place. If I model the problem using C-style structures, or C++ classes, as nodes, it's easy for the interviewer to assume that I do not have knowledge of std:: data structures. Furthermore, I really want to demonstrate my std:: knowledge by modeling the problem with std::forward_list
Now the std:: happily provides a .reverse() method:
http://www.cplusplus.com/reference/forward_list/forward_list/reverse/
With this, I could easily scribble out this one liner:
myForwardList.reverse();
..and ask if we get the job. This would probably be greeted with a patient smirk and a request to solve the problem without using library functions.
NOW, here's the real trap. If I use std::forward_list to model the data:
- I can't use the .reverse() method
- I can't easily flip the pointers, because I don't have direct access to the underlying structures!
What's the best way to approach this issue?
Thanks, Keith :^)