Framework for solving Leetcode problems

It’s not my know-how and it’s combined from what I read, heard, and used. I should say that this set of steps are really improved my solving skills already.

In the beginning, it seemed very time-consuming and kinda waste of time, but after some practice, I noticed that my mind flow is better structured. I just follow the steps and see how it helps not only to find the solution but also to reduce my emotional level which distracts me a lot.

🗣️ Use your voice

Reading, thinking, writing loudly helps to practice and better structure your thoughts. In my case, it also helps me to practice speaking English which is a crucial skill for communication. After all, preparation is the best time when you can spend some time to recall “that exact word” you want to use. Also, they say using one more channel for incoming information (reading + speaking, writing + speaking) helps you to process it more efficiently.

🦉 Read the problem carefully until you do not understand it completely

When I just started solving problems I was obsessed to start coding right away. Sometimes it worked, but the more difficult or tricky problem I faced the more I felt upset and demotivated. For me, it’s like an improvisation standup show. You appear in front of the audience and know nothing about the audience, subject, and even the language they speak. It might take a while until you understand the problem, but ignoring it leads to a dead end. You might think you know what you’re solving right now, but eventually, when the solution fails - you start over again spending your time when you already spent time, energy, and got upset emotions.

👶 Come up with the simplest solution. Do not code yet

Practice explaining your simple, not optimal solution using the text editor. Real interviews (which are remote now) are likely will be conducted with google docs or something similar. So it’s important to be able to explain the solution step by step and analyze it in terms of time and space complexity. I usually use an example from the problem description and trying to explain my solution by moving the text cursor. I was surprised that what is clear in my mind - not always easy to speak out clearly. It very helps!

🚀 Optimize your solution

Cracking the coding interview book has such an abbreviation as BUD. It always must be applied during this step. It stands for Bottlenecks, Unnecessary work, Duplicated work. I have nothing to add here. It’s easy to memorize and easy to follow.

To be honest, for me it’s not always clear to find a better solution yet. And if I can’t come up with a better solution - I code my simplest solution from step 3 and looking for alternative solutions in discussion groups by keyword “javascript”. Often happens I’m not familiar with a well-known algorithm or approach. Well, I’m practicing here after all.

✔️ Walk through the solution you’re about to code

Checking all edge cases before you started to code is very important. You must make sure you understand each detail before starting coding.

👀 Code like somebody’s watching it

Finally. Now when we have all cards in our hands we’re ready to start coding. What helps me to write clearer code is voice commenting to an imaginary person.

🧪 Test it

With Leetcode is quite simple, you just click the button, but… I suggest writing custom tests before pressing “Submit”. Write custom tests and click “Run Code” instead. Doing so, I check all edge cases we discussed earlier, and pushing the “Submit” button for me now is like the last resort.

As I mentioned earlier it looks very time-consuming and I thought that it’s unproductive. But practicing convinced me in the opposite. I admit you’ll find it as overkill for trivial problems, but I still think this approach worth spending some time, in the beginning, to be more effective later.

If you find this article useful and would like to share your thoughts or just interested in my weekly updates, feel free to join my Telegram Channel.