Graduating from Leetcode
By: Hari Ohm Prasath
Tags: Data Structures, Coding, Algorithms, Problem Solving, Leetcode

What is LeetCode.com?

It’s a website where people–mostly software engineers–practice their coding skills. It’s pretty similar to sites like HackerRank & Topcoder which will rank your code written for a particular problem against the ones submitted by other users

People who use them

Leetcode gets primarily used for two use cases:

Solve interesting data structure problems

Prepare and practice for coding interviews 🤔

Purpose of this post

I never had a clear understanding when it comes to data structures and algorithms, so being a software engineer for the last 12 years, I thought it’s not a bad idea to at least start now. So as of Dec 2019, I became a premium member in Leetcode and committed myself to get as much value out of 150$ that I spent on the membership fees. Leetcode is like an ocean; it has closer to 2000+ questions, which keeps growing day by day.

In fact, this was my exact reaction when I open the website for the first time after looking at the number of questions and categories it had

In this post, let me explain some of the methods I developed over some time, which helped me to understand and learn about these data structures & algorithms.

Get the basics right — Don't run too fast

Most people new to Leetcode directly jump into solving the problems without getting a proper understanding of the underlying data structures like a linked list, stack, queue, tree, etc. Trust me this is a bad idea

I would recommend spending some time understanding these data structures, which are the building blocks for solving any problems.

Even if you are not able to go through all the data structures, at least get a strong understanding of the linked list and binary trees. Fortunately, there are some excellent materials created by Standford, which can help you with this:

Linked List:

Binary tree

Method to madness

Leetcode has many categories and a whole lot of questions in each of them, so it’s essential to come up with a method that could help you to understand these concepts. Learning with LeetCode is a long process, and it will take time, so one thing that I clearly understood is you have to keep the enthusiasm and eagerness to learn high if you want to go through the journey.

If you are a beginner, then use the below flowchart to learn things and gradually increase the complexity of the problems that you solve. It also helps in understanding the concepts well so you can reapply those techniques in the subsequent problems.

Sequence for solving problems

Stages are numbers in red circles, and dotted lines are entirely optional for proceeding to the next stage. When you are done with stage 3 depending upon your motivation, you will end up either doing “Monthly coding challenges” or “attending interviews” (both are fun)

Define a process

It’s essential to define a process, so when you get a problem, you know exactly how to solve it, or else it won’t be easy to organize the information that you have learned so far and use them when required. The most fundamental thing to do before writing code is first to understand the algorithms and various techniques.

I came up with a two-step process which helped me in both learning these techniques and coming up with a solution when presented with a problem.

Categorize the problem — Step 1

I would first categorize the problem into one of these buckets:

Arrays & Strings

LinkedList

Trees (BST and BT)

Graph & Trie

Search & Sort

Dynamic programming

Others

Identify the technique — Step 2

Assuming you have already categorized the problem, you can now apply various techniques that you learned on each of these categories to come up with an elegant solution. I would say try to come up with a solution first (maybe even a worst-case scenario), don’t need to be the most optimal one. Because based on my experience, if I target the best optimal solution right away, I generally get stuck, and I lose hope of solving the problem.

Tip: A simple way to reduce the time complexity is to use temporary storage in the form of Queue, Stack, List & Map part of your code

Below diagram explains the process that we talked about