DEV Community

DFS With Memo
DFS With Memo

Posted on • Updated on

Leetcode survival guide

This week I have crossed 500 Leetcode problems. In celebration of that I wanted to write a post reflecting how I survived through 500 Leetcode problems which include some of the most mind bending data structures (union find) and algorithms (topological sort, kadane's algorithm and more)

If you have stumbled upon this article you might have started your Leetcode journey or considering to solve problems on Leetcode to prepare for interviews for big tech companies. In this post, I will cover some FAQs, Dos and Don'ts and lists of problems that you can solve to become better at problem solving and crack interviews at big tech companies for software engineering and machine learning roles.

Table Of Contents

Guide

Most people confuse simplicity with the absence of stuff. Simplicity is the absence of all unwanted stuff, its the removal of all complications and it has a focus on the most important detail. Here, I present to you a simple guide to help you prepare for your big tech interviews.

However, focusing on simplicity will make you lose on the behind the scenes stuff, the motivation, how it was achieved, what mistakes were made, what were the leanings etc, so even though my guide is "simple", you should read on to figure out why the guide is as it is.

The guide I have written below, is for people who are at least familiar with a programming language and know their basic data structures(arrays, linked lists, stacks, trees, graphs etc).

If you are a complete beginner or unsure, this is for you

Guide

First of all, I would settle on a List of problems and start solving on Leetcode.

I would focus on solving problems only from Leetcode, ignoring any other platforms(Competitive Programming sites such as codeforces, spoj, google code jam etc).

My aim is to clear online assesments/interviews not win competitive programming contests. Though competitive programming is a valid route, it's not the focus of this guide. The preparation, I feel is completely different.

Since Neetcode 150 is my favorite list, I would go with that section by section. In my first pass of the list

  • I would focus on the easy and medium problems, moving from top to bottom.
  • Skip backtracking, 1-D and 2-D DP sections and move to graphs.
  • Once I solve all the easy and medium problems in graphs(both sections), I would visit the DP sections and backtracking.
  • If I am preparing for a Google interview, I won't skip the math section.

EDIT: Neetcode has moved graphs over DP so now there is no need to skip any section and you can do the list in order

Once that's done the next step is situation based.

  1. If there is an interview coming up, I would focus on solving company tagged recent frequently asked problems.
  2. If I have time on my hands, I would pick up another list and solve the problems from there.

That's about it really. Have faith in yourself to become better with regular practice and you will surely succeed.

Below, I have few sections introspecting what I did well in the past couple of months, what I didn't(so you can avoid the pitfalls), FAQs and obligatory learning resources.

Things I did well

Consistent practice.

You need to solve around a hundred high quality problems to have a chance of solving new problems. When you are starting out, a single problem can take easily between 2-3 hours of your time even if you managed to come up with a solution of your own. It's crucial to solve consistently so that the numbers rack up. If you solve a single problem every day of the month you would end up solving 30/31 problems.

Solving previously asked company specific problems

When I had to prepare for a big tech interview, I dug up questions from forum posts on Leetcode and geeksforgeeks which were asked by that specific company. In my opinion, apart from knowing the common algorithms, patterns, and data structures, it is crucial that you solve previously asked problems by the company you are going to interview at.

Keeping track of the problems I solved.

Many use an excel sheet to track the problems they have solved, I personally use Notion. This helps me revisit problems that I had problems with or quickly look up a one line hint to solve the problem.

In case I had to look up the solution to a problem, I would try to explain the solution concisely in my own words.

Things that I didn't do well

Solving problems randomly

Originally, I started by filtering problems by topic and solving 5-10 problems from each section. In that time frame, I got an opportunity to interview at Amazon for a SDE-2 role. I was completely lost to say the least. I wasn't improving and I was sure that I won't be able to clear the online assessment.

I stumbled upon the list of problems mentioned in cracking the coding interview on Leetcode. I felt as if my problem solving abilities changed overnight. I was able to apply the patterns I learn in one problem in the next or at least consider different patterns to solve a single problem.

In the end, I was unable to clear the online assessment. I didn't solve enough high quality problems. It was a learning experience on itself nonetheless. From then on, my preparation mostly involved solving from popular Lists

Not starting immediately

I wasted one semester solving problems from platforms such as hackerrank, codesignal etc. To be honest, I didn't get anywhere. I also believed that I would be able to clear an interview in a big tech company without solving Leetcode. I still ended up in a decent company but it wasn't my first option.

FAQ

I don't have a CS background and so I am unfamiliar with even basic data structures and algorithms. I don't know how to do. How do I prepare so that I can solve Leetcode problems?

It can be pretty overwhelming if you are a new beginner. I would advise you to take it slow.

First off, learn python. You will only need to know the utmost basic stuff, nothing fancy. You won't need a book, an online tutorial should suffice. To solve Leetcode style problems, I would say understanding data types is one of the topics folks easily ignore. Understand intuitively data types and how they can be combined.

Next you have to learn and understand data structures/algorithms/complexity analysis. Pick out something from the Learning List and from Problems list. Learn a topic and try to solve a problem from that topic. This is where you have to mentally brace yourself like stepping into a cold shower, it will be a shock. You won't be able to solve even an "easy" problem.

Play around with the problem a bit by applying the concept you know. It's perfectly fine if you are unable to come up with the right solution because there might be something in there that you haven't learnt yet. Now's the opportunity to learn and add it to your belt. If needed write/type out some notes to reinforce your learning. Move to the next problem. Over time you will surely get better.

I have solved 50 problems on Leetcode and yet I am unable to come up with answers of my own, am I doing something wrong?

Not quite, 50 problems are too few to get a feel for all the different types of problems that can be asked during an interview. To get good in solving problems, you need consistent and deliberate practice. Pick a topic say array and solve high quality problems.

High quality problems are those that have been frequently asked by companies in the past/those that have good likes to dislike ratio. To make it easier to find high quality problems, follow a high quality list(see below)

How many problems should I solve to get good?

It depends on your comfort level but if this is the first time you are solving these type of problems, I would say about 100-150 problems (5-10 problems from the popular topics). Once you hit that target, solve company tagged questions.

In my book, to be best prepared for interviews, you need to solve the recently most asked problems for the company you interview with. For that you will require Leetcode Premium(abbreviated as LC Premium).

If you don't have LC Premium, you can scour the discuss section to find questions asked in interviews of a specific company. That's how I prepared for an interview and it worked.

Is LC Premium worth it?

I am not sponsored by Leetcode, it's my personal opinion that it's worth it. The main feature is that you get to know how frequently a question has been asked in a company in the past 0-6 months which I believe is critical to preparing.

You don't need LC Premium when you are starting out but considering getting it as your interviews draw near and you have exhausted your problems to solve.

Which is the best drawing tablet for Leetcode?

I have seen this question pop up more times that I would like. My personal opinion is that you don't need a drawing/writing tablet. I cannot think when staring at a screen so I write stuff down on pen and paper. It's a more traditional way but I can't recommend anything better.

Notebooks can be expensive. If you are a college student, your department might have a stationary room. If you want to buy a notebook, search for Filler paper.

Which programming language should I use?

If you are a college student, I would advise Python. It's one of the most popular languages. Because of how concise the language is, it's easy to write complicated logic with very less code. If you are an experienced professional, I would say try solving a couple of problems in the language you use daily. If it doesn't work out consider switching to Python.

Do I need to know the programming language in-depth?

No. You have to be familiar with the data structures that are available in the language of your choice. Here's a list

  1. Lists/Dynamic arrays
  2. Stacks
  3. Queues
  4. HashMaps/Dictionary
  5. Sets
  6. Heaps
  7. A data structure that can maintain sorted order and you make queries such as check if a value less than x exist, check if a value greater than x exists. In Python you can use bisect_left and bisect_right, in Java there is TreeMap/TreeSets.

I spent hours stuck on a problem and I am unable to come up with a solution. What should I do to get better?

The discuss section for a problem is a gold mine. Most problems have detailed explanations for the problem. You should spend around 15 minutes trying to figure out an approach for the problem. If you find a high level approach, such as you figure out that you need to apply binary search, take another 15 minutes to hash out the algorithm. If you are able to do so, code it up. If not look at the solution. Don't spend more than half an hour playing with the problem.

I would also advise against getting frustrated. I know it's a difficult thing but be patient. Work your brain but not too much as there is a lot of problems and patterns to know and spending your precious time on a single problem is not worth it.

Should I focus on space complexity?

Not unless there is a follow up question. I personally try to optimize for space because saving space is directly related to practical real world performance. There is a huge cost for allocating memory on a heap which if possible should be avoided.

I have a week to prepare for interviews and I don't know which topic to focus on.

If you are interviewing for a big tech company and you only have a week to prepare, I would suggest solving the graph problems from the Blind75 list.

A common reaction would be to solve dynamic programming(DP) questions but in recent times, I have seen that graphs are more popular. If you have some advanced knowledge or over time you accumulate problem solving experience, you will come to realize that DP is at its core a DFS with caching.

If you have time remaining focus on array problems that involve searching, sorting, modifying an array

Dos and Don'ts

If you are stuck you might be tempted to pass the code through a debugger.I would highly discourage that.

Instead, go through your code step by step without a debugger. If you need to write stuff down, that's okay. If the input size is too large, think of ways you can reproduce the issue on a smaller data set.

Take enough breaks

Leetcode burnout is real. Consistent practice is better than burst practice. Even if you can't dedicate 4-5 hours every day on Leetcode, dedicate one hour every day. If it's not possible to solve every day that's okay, start with 2-3 days a week(maybe weekends) and free up time on weekdays. Try to make solving Leetcode problems the first thing you do in the day, not the last.

Don't ignore the discuss section

Leetcode's run time is very erratic but if your solution is consistently slower than 20% of all solutions, its time to see the answers in the discuss section. Most likely, someone has done it in a better way. Minor optimizations/tweaks can also go a long way.

Resources

Solve

My personal recommendation,Neetcode's 150 problem list
You can also aim for the 75 problems list on the same website. I would recommend solving the graph problems first before going to the dynamic programming section

Sean Prasad's leetcode patterns list, you can filter by company Sean Prasad's Leetcode patterns

Another popular list Grokking the coding interview problems

There's an Online course for Grokking the coding interview

The OG list for problem solving Cracking the coding interview 6th edition Leetcode problems

If you need additional practice - Love Babbar's 450 problems DSA list

Learn DSA

For all the knowledge you need on data structures, William Fiset's data structure course - https://www.youtube.com/playlist?list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu (I would recommend watching till video number 37)

For most of the common algorithms, Algorithms playlist course by Abdul Bari -
https://www.youtube.com/playlist?list=PLDN4rrl48XKpZkf03iYFl-O29szjTrs_O (I would recommend watching the first 26 videos to learn about complexity analysis)

For all the graph algorithms you would require for interviews , Graph playlists by William Fiset - https://www.youtube.com/playlist?list=PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P

The tech interview handbook which contains a comprehensive list of topics and problems to practice - https://www.techinterviewhandbook.org/algorithms/study-cheatsheet/

Top comments (0)