Leetcode — Yes, Go Ahead and Check the Solution If You Are Stuck
--
It’s safe to say there has been a debate of when to check the solution when you’re stuck on one question. Some believe that you have to struggle through it. Some believe there’s no need to waste time and you should go straight to solution. Some believe the real answer lies somewhere in between.
I believe that practicing leetcode is about pattern recognition. Barring some tricks and special solutions, you should be able to know roughly what approach you should take when seeing the problems. If you can’t do it, then you haven’t seen enough of that type of problem.
Solving the problem is actually the second priority. True, the interviewers expect you to come up with best solution with best time complexity, but if you don’t even know where to begin, you can’t come up with any solution.
This is why I think going to check the solution is okay. Yes, you should attempt to solve the problem first, but once you’re stuck, whether it’s because you can’t think of an approach, or you can’t get some edge cases right, go for the solution.
Now the most important part: Remember the pattern. This is like middle school again where you do math tests. The problems are going to be similar to the ones you’ve done in homework but slightly different. Can you still find out what the problem is asking and come up with a solution using what you’ve learned? When doing homework, you eventually compare your answer with the actually answer, then you learn. It’s all the same with leetcode. Try your best on your first attempt, then go straight to solution and learn from there.
Don’t memorize the problems. Recognize the patterns. If you get the same problems during an interview as your practice problems (funny that a lot of companies actually do this: they don’t try to come up with their own problems. They just go to leetcode/hackerrank/whatever and find one), you’re lucky, but most of the time you won’t. Therefore there’s no point memorizing the problems.
Get to the root, refer to the solutions, and recognize the patterns. Then you’ll truly learn how to deal with different problems.
Good luck!