Recently I've been doing LeetCode's weekly contests and I recommend anyone wanting to practice for interviews do them as well. The contests are great because they mimic the environment of a real interview. You are given a set amount of time (often 90 minutes) to solve several problems that increase in difficulty. The tests do a good job of imitating a high pressure environment rather than practicing problems in a lax setting.
After repeatedly taking these contests I feel that I've nailed down a method that works for me for solving problems and wanted to share. Anyway lets jump into how I solve each problem.
- Take the time to read the problem 2-3 times until I'm confident I understand it. If I have a few confusions I continue to the next step anyway which is...
- Examine the test cases and make sure I understand why the test inputs lead to the test outputs.
- If you're given the expected time complexity you can narrow down approaches they want you to use. For example, if they want you to do an O(log n) search you can first try a binary search.
- At this point I think about how I'm going to approach the problem while looking at the provided code as well as the provided constraints.
- Always try to think of edge cases that could cause problems before you code as well as while you are coding
Thanks for reading! I'd love to hear what works for you when you're solving a coding problem and how what you think I should do differently.