After doing some interviews, I've realized that I usually know what I want to do to solve a problem, but I often get my logic complicated (and introduce a lot of bugs) when I'm actually coding.
For example, when I tried to code up insertion into a sorted singly linked list, this is what I knew I had to do:
- I had to check for the case where the list is currently null
- I had to check if the element I want to insert is less than the current head
- To insert between nodes, we had to link one node to the new node, and the new node's next to the one that is supposed to be after.
I had trouble inserting between nodes because I was actually supposed to check the NEXT node's data and not the node which I had my pointer pointing to (so I could set current's next to the new node). I also struggled on this problem (note that this was coded on white board) because I was pressured under time and I think I made it more complicated in my mind that I should have. When I went back to this problem after the interview, it turns out the solution isn't complicated at all (as it shouldn't be). However, I just tend to complicate things and confuse myself.
Would anyone have any suggestions on how I could improve this vulnerability of mine?