Professional Documents
Culture Documents
Search Write
Get unlimited access to the best of Medium for less than $1/week. Become a member
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.
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.
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.
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.
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.
Steps:
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.
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.
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.
6 min read · Aug 27, 2023 6 min read · Sep 15, 2023
362 6 30
Pushan Mukhopadhyay Pushan Mukhopadhyay
3 min read · Aug 30, 2023 3 min read · Jan 31, 2024
50 1 8
320 7 8.2K 54
Lists
294 1 5.9K 86