How to start Leetcode grind
Published: 2022-02-11 17:55:40

Since you have click on this blog i may assume you are a complete beginner in leetcode and want to start your leetcode journey to get into top tech companies or change companies.

By leetcode grind , I do not recommened only using leetcode but also other websites such as GeeksForGeeks, HackerRank etc. to get more exposure to different questions.

But before you jump into solving questions of any type I recommend you to understand how the Data Structures are implemented and some theory around them.After thats down lets move on to actual coding.

You might have heard of many patterns of programming like Blind75, Sean Prashad, Grokking the coding interview etc which are great(I use them too) but only doing that questions but guarantee you get good in coding.These questions cover all the patterns that are important but only doing them will not guarantee you can do all the similar questions related to them.

For Example, Lets say you are solving questions on binary Trees and you come across the Depth First Search of Binary Tree.Now you know how to do a level order traversal but you might not always be able to solve all the questions around Depth First Search unless you have solved plenty of them.For example, lets say the question is

Q.)Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vz5nbheipvulbcmzje6p.png)

```
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
```
Lets say you know this question requires you to use DFS but how are you going to store the individual values of all the node and sum them to find if they are equal to targetSum.

For a complete beginner it may not come handy unless you have practiced a lot of such questions.

Now in the iterative version of DFS you know that you have to use stack to remember visited nodes and traverse further.So you are aware that stack is used to store the nodes.So instead of storing only the nodes in the stack you can also store a tuple inside the stack along with the node’s sum of all the previous node value.Something like this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77p8skmc793vhgeuecr8.png)

This little trick is hard to come up with your own,but once you know it you can solve many questions regarding the DFS of Binary Tree i.e now you know that you can use the stack itself to store more data about the binary tree.

```
def pathSum(root):
    if not root: return None
    stack = [(root,root.val,[root.val])]
    ans = []
    while stack:
        node,val,li = stack.pop()
        if not node.left and not node.right and val == targetSum:
            ans.append(li)
        if node.right:
            stack.append((node.right,val+node.right.val,li+[node.right.val]))
        if node.left:
            stack.append((node.left,val+node.left.val,li+[node.left.val]))
            
    return ans
```
This trick of storing using tuples in stack will help you solve a lot more questions.

So my point is after reading one pattern, solve as many questions related to it as you can.Another important thing to consider is to not give a question more than 5–7 minutes of your time in the beginning for easy questions.The reason for it is if you can’t come up with a solution for easy question in 5 minutes then maybe you are lacking some concepts regarding it.

One more important thing to remember is spaced repetition.You might not remember everything you do.So it is better to revise them from time to time to completely grasp the concept or the question.

**Some Motivation**
‘‘The reason to win the game is so that you can be free of it.’’ — Naval Ravikant
**HAPPY GRINDING**