Coding Interview: The Game Plan
I am doing interviews few times a year, and it's probably not enough[1]. Opportunities aside, I learn a valuable lesson every time.
Interview pressure uncovers weaknesses. Mine are:
- Overcomplication: I somehow expect the problem to be hard. So, I dismiss valuable cues and intuitions if they look too obvious.
- Impatience: If I feel a hunch, I dive in without full understanding. I keep stepping on this rake... and solving a wrong problem!
- Panic: I the problem sounds intimidating, I panic and freeze, not knowing where to start.
Brute-force
To compensate these weaknesses, I now start with a simplest solution that comes to mind. I work on a na?ve solution first, clarifying my understanding, and then brainstorm ideas to improve it.
This approach helps reduce the cognitive load when you start working on an improved solution.
This is an intuitive and well-recommended technique. However, I feel that candidates are afraid to look inexperienced and skip this step. As an interviewer, you may want to offer your candidate to start easy.
The Game Plan
It helps to mentally structure the interview into phases. The opening is important as it sets the tone, and brute-force can help you get through.
The game plan also helps manage your time and deliver a solution before the interview is over.
The Opening
The entire phase should feel like a fluid conversation with some prototyping. The goal here is to understand and get a feel of the problem. Do not try to ask every question upfront - build it as you go. You will have more questions, and some questions will resolve themselves.
Discuss and implement a brute-force solution. Write some good code, show your skills by following design principles. Make sure your solution is correct, check for edge-cases. Determine runtime and memory complexity. Have a chat, relax.
The Middlegame
As you talk about the complexity, discuss any trade-off and think what the best conceivable runtime is. This narrows down approaches you can try. For example, if you are aiming for O(n log n), you could try the sorting or some other divide-and-conquer technique. If the problem is about contiguous subarrays, the sliding window may help solve it in O(n).
It is important to have a good test case to run your ideas against. Your interviewer could provide just a basic one to explain the problem. This is often done purposely, so go find a good test case - it's essential to spark your intuition!
Now it's time to ask - again - about constraints. Will the input fit in memory? What are possible values of a parameter - can we map it in a vector, or we must use a hash set?
If you got stuck, ask for a hint. This is better than continue digging yourself in. If you pick up the hint and do a good job with your code, it will be a successful interview! Some interviewers would consider it a stronger signal than when you come up with a solution by yourself (maybe you knew the answer?)
I am not suggesting asking when you know the answer; the point here is that you should not be afraid to ask for a hint.
The Endgame
Finalize the approach with your interviewer and carefully implement it. This should be easy as you have everything figured out - just focus on the code and tune out distractions!
Verify your code using the test cases from before. It's important to double-check you work even if you feel confident [2].
Brainstorm any new ideas that came to you during the focus time. Have a chat, smile :)
Coding Practice
The most important advice - keep solving problems! You need to practice various techniques and approaches; this is like a set of tools you can try to crack your next problem!
LeetCode is my favorite destinations for algorithmic problems. It has one thousand problems and counting, with test cases to validate the correctness, as well as computational and memory complexity. There is also a large community discussing different approaches, tips and tricks.
[1] The next best thing is online contests (or mock interviews, if you have a programmer friend), which I am trying to take every week. You need to solve 3-4 problems - easy, medium, and hard - within 90 minutes, and write code that passes all test cases, as well as time and memory limits.
[2] I failed several important interviews by not double-checking my work. It's so disappointing when you figure out a complex problem but then undo it by missing a silly mistake!
Vlad Trubachov thank you so much for sharing this post. Interviews are hard for many reasons. I've been meaning to ask, would you happen to have a study list of Leetcode problems that you think are important for practice? Thank you again.
Senior Software Engineer | Bachelor's in Computer Science
4 年Great article from an experienced leetcoder! Otlichno!
Staff Software Engineer at CVS Health | xMeta
4 年Thanks Vlad Trubachov for sharing never thought I would see someone who can complete all the lc.
Software Engineer at Facebook
4 年Nice read! I especially related to 'impatience'. Many candidates jump into coding before they have a clear algorithm in mind, only to find themselves lost down a rabbit hole when things go wrong.
Software Engineer 2.1 @ ZEISS Medical Technology | Masters in Computer Science
4 年Much needed article at this time, Vlad.