It starts with a simple question:
What should I do when I’m stuck in a coding interview?
Most people would suggest you go back and practice more, which can’t be more true.
But I’ll tell you that there are still a lot of things you can do during the interview. You might be nervous and your mind just goes blank, which is quite common when people are stuck.
So in this post, instead of talking about preparation, I’ll share tips and hacks you can use when you have no idea in an interview.
Keep a clear mind
It might sound like a cliche, but nothing can be more important than keeping a clear mind in coding interviews
To make things easier for you, I’ll explain in detail about what you can do to be clear.
When I say “keep a clear mind”, I would expect you to be clear about at least the following three things: what you are working on, what’s your current problem and what doesn’t work.
- Ask yourself what you are working on when you get stuck. After years of interview experience, I found that many people tend to lose the big picture when they get stuck by some details. For instance, many candidates like to start coding when they can’t figure out the solution, hoping that they will get inspirations from the code. However, when writing code, there will be a bunch of other issues popping up and they may totally get overwhelmed. So it’s important to be clear about what you are doing right now. If you are trying to find the right solution, coding may not be helpful. Only do things that matter instead of always keeping yourself busy.
- Be clear about what’s your current problem. It’s kind of funny that a lot of people have no idea of what they’re stuck on during coding interviews. What I usually suggest is to have a bigger picture about the whole process. Try to figure out which part of the problem you’ve already solved and what you are stuck on right now.
- It’s a waste of time to keep trying with approaches that won’t work for sure. If you have already tried with some methods and they don’t work, never try them again unless something has changed. This principle is so easy to understand, but a lot of candidates are still caught unaware of this common pitfall.
Sometimes you may need to jump out of the question itself and think about concepts/techniques related to what you’re stuck on.
For instance, when you are dealing with storage related problems, you should definitely consider data structures like hash, trees, linked list etc.. And when you need to further optimize your O(n^2) solution, you may think about algorithms like sorting, binary search etc. as they are likely to make it to O(nlogn). Also when your solution is too slow, you should consider if you can take advantage of some extra memory like a hash map.
I would say all these should come naturally to you. With that in mind, Summarizing some common patterns when practicing is highly recommended.
Start with simple situation
It’s likely that you will get inspiration by solving a simpler problem, which is also a common pattern to learn everything.
If the problem requires a lot of 2-D intervals with overlaps, maybe you can try to solve the problem without overlap. If there’s an array of numbers, see if you can solve it when all numbers are sorted.
Also you can try to work on a simple example step-by-step. If it’s a problem related to arrays, create an example with 7 numbers and see how you can solve it. A lot of people can get inspiration from this process.
Consider extreme cases
You will be surprised by how helpful this tactic is and sometimes you may even come up with solutions that are out of interviewers’ expectation.
The magic is to remove some restrictions and see what’s going on. For example, the most common strategy is to assume you have unlimited memory and see if you can solve the problem with better approach. Usually by removing the memory restriction, you can store almost everything in a hash set or hash table. This may give you great advantage and sometimes can provide a O(n) or even O(1) solution.
I would say you should try this approach even if you are not stuck as you may come up with better solutions.
Interviewers may not know the answer
Believe it or not, interviewers may not have answers to all the questions and sometimes they just want to see how you react under pressure. That being said, you definitely shouldn’t be nervous when you don’t know the answer.
The best tip here is that you should always tell what’s in your mind even if you don’t have any concrete solution. Talk about your random thoughts, your current strategy or just what you are stuck on. What really matters is not the solution, but your analysis process.
By talking proactively, interviewers can better understand what’s in your mind and how you try to address the problem step-by-step. And when you get stuck, interviewers may also provide hint.
Of course, everyone can get stuck during coding interviews. Instead of being nervous, try to enjoy this process as it can be interesting to discuss challenging problems with smart people.
Don’t be disappointed as there’s no golden rule to this problem, however, I hope tips here can at least make you less nervous about coding interviews.