You are on page 1of 12

Open in app

Search Write

Get unlimited access to the best of Medium for less than $1/week. Become a member

Best way to solve DSA problems!!-


Become a pro
Pushan Mukhopadhyay · Follow
5 min read · Sep 20, 2023

559 6

Solving a DSA (Data Structures and Algorithms) problem can be pretty tough.
This article should help you understand how to go about solving a DSA
problem on websites like Leetcode, InterviewBit, and Geeksforgeeks.

These are the steps that you should follow:

Disclaimer: It is a pretty big article. Make sure to bookmark it and reference it


from time to time.
Understand the question completely
Look at the question and try to understand it completely. Make sure to run
through the examples to verify that your understanding of the problem is
correct. How to do that? The input should give the required output. You
should be able to verify that.
Write down stuff on paper if required. You need to be completely sure that
you understand the problem before moving ahead.

Get an estimate of the required complexity


Look at the constraints and time limit. This should give you a rough idea of
the expected time and space complexity. Use this step to reject the solutions
that will not pass the limits. With some practice, you will be able to get an
estimate within seconds of glancing at the constraints and limits.

Example
For an estimate, ~108 operations will take:

1s in C/C++

2s in Java

4s in Python

Most DSA problems will have the above time limits as well.

Learn how to estimate the required time complexity.

Example: For the above constraints and limits, anything worse than O(n)
won’t pass.
Come up with edge cases
This is one of the most important stages. Most people miss this and get WA
(wrong answers).

In most problems, you would be provided with sample input and output with
which you can test your solution. These tests would most likely not contain
the edge cases. Edge cases are the boundary cases that might need additional
handling. Before jumping on to any solution, write down the edge cases that
your solution should work on.

What are the edge/boundary cases and how to find them?


Boundary cases are the test cases that you can create based on the start and
end boundary of the different constraint variables.
Example

In the above constraints, the boundary cases are:

T (No. of test cases per run): 0, 1, and 1000

N (No. of array elements per test): 1, 2, and 10000

Array element value: -100000 and 100000. Should also test on -1, 0, and 1.

Create test cases (input and expected output) based on these values.

Note: Test cases mean both input and expected output.

There are different types of cases that you can create. It will be based on the
use case. Learn the patterns and take care of them as you miss them on
different problems.

Examples: Let’s say that the array elements can contain duplicates. Make
sure to take a small test case with duplicates.

Note: It is not practical to create tests for large input sizes (T or N values) so
avoid those if they are very big. Creating tests for large array element values
is easy and so definitely create those.

Come up with a brute-force solution


Come up with a solution that solves this problem correctly. It should not be
the most optimal one. This solution is the one that you can come up with in
less than a minute.

When you start practicing DSA, it would most likely be an exponential or


polynomial solution. With sufficient practice, your brute force solution
would also be pretty optimal and close to the required solution.

This is generally not the solution you are going to code.

Steps:

Try to come up with a correct solution as soon as possible.

Dry run your solution on the sample test cases to verify if your solution is
correct.
Verify if your solution is optimal based on the time and memory limits.
Do this by computing the approx number of operations based on the
constraints. If it will pass then this is the required solution.

Even if it is passing, give a minute to see if you can think of a more


optimal solution.

Optimize your current solution (Repeat until required optimal


solution)
Optimize your current solution if it is not going to pass based on the
constraints and limits. This is one of the hardest parts of solving the
problem. It gets easier with practice and comfort in the required DSA.

If you’ve already computed the estimated time complexity, you can easily
accept/reject the optimization ideas that come up. You would generally come
up with an optimal solution after multiple iterations.

For every optimization that you come up with, compute the time and space
complexity of your solution and see if it would pass. Make sure that every
optimization is sending you closer to a solution that will pass the limits.

Data Structures Coding Problem Solving Programming Algorithms


Written by Pushan Mukhopadhyay Follow

499 Followers

I am a Tech lover and love to explore new technologies. I am also a Coder. I will share
whatever I know and going to learn about this in future.

More from Pushan Mukhopadhyay

Pushan Mukhopadhyay Pushan Mukhopadhyay

How to solve any recursion Let’s solve BCKTRACKING


problem !!!! Master of solving… problems like a pro — — Part 1
In this article, I am going to share some tips Backtracking is a technique for solving
for solving recursion problems in easy 4… problems by exploring all possible solutions.

6 min read · Aug 27, 2023 6 min read · Sep 15, 2023

362 6 30
Pushan Mukhopadhyay Pushan Mukhopadhyay

Best algo for competitive 7 Must Know JavaScript Tips &


programming updated list [2023] Tricks 🎈
In this article, we will discuss the top Let’s have a look at 7 valuable tips and tricks
important algorithms for competitive… found in the most popular language in the…

3 min read · Aug 30, 2023 3 min read · Jan 31, 2024

50 1 8

See all from Pushan Mukhopadhyay

Recommended from Medium


Ravi Lamkoti Arslan Ahmad in Level Up Coding

My Google Interview Experience Don’t Just LeetCode; Follow the


Coding Patterns Instead
What if you don’t like to practice 100s of
coding questions before the interview?

5 min read · Feb 1, 2024 5 min read · Sep 16, 2022

320 7 8.2K 54

Lists

General Coding Knowledge Stories to Help You Grow as a


20 stories · 912 saves Software Developer
19 stories · 805 saves

Coding & Development ChatGPT


11 stories · 445 saves 21 stories · 459 saves

Shuang Li Oliver Foster in Stackademic

Cracking coding interviews from What’s the Difference Between


FAANG and Open secrets localhost and 127.0.0.1?
(This post is going to cover all the secrets My article is open to everyone; non-member
from the coding interview, please make a… readers can click this link to read the full text.

7 min read · Sep 18, 2023 · 8 min read · Feb 1, 2024


420 5 504 6

Alexander Nguyen in Supportive Software Engineer Artturi Jalli

Resources that got me into I Built an App in 6 Hours that Makes


Amazon, Microsoft, and Google $1,500/Mo
Quick Context Copy my strategy!

· 7 min read · 3 days ago · 3 min read · Jan 23, 2024

294 1 5.9K 86

See more recommendations

You might also like