To be a good software engineer is to be able to solve problems, that’s where Data Structures and Algorithms come into play. You need to be good at solving DSA problems because that tests out your ability of problem-solving in interviews and in your job role. You might have seen people grinding on leetcode, or geeksforgeeks in order to improve their ability and do good in their job.
That’s what we are going to look at in this article. We will discuss the steps you should follow in order to solve DSA problems effectively so that you can adapt to this methodology of efficiently tackling every DSA question that comes your way.
- Make your programming language concepts clear.
- Do not skim the question, READ the question.
- Use a Pen and Paper before touching the keyboard.
- Always break down the solution to reach the end answer.
- Try to find a Brute-Force solution first.
- Figure out the Time and Space Complexity of the Brute-Force Solution.
- Optimize the Code to ensure a better Time and Space Complexity.
Clear Up the Concepts of Programming Language
Before solving any kind of Data Structure and Algorithms problem, be knowledgeable about the concepts of the programming language you’re going to be using for solving DSA problems. This is because there are going to be instances where you will need to use more than the basics of programming language.
For example, you should know how to use classes as it is one of the most common things one should be aware of. If you know the ins and outs of programming language beforehand, coding the solutions to the Data Structure and Algorithms question becomes less painful. If you don’t, you will find yourself in difficulty while writing the solution code for the problems.
I would suggest you find the programming language you’re most suitable for coding in and know intermediate to advanced concepts in it.
Read the Question Dedicatedly
Reading the DSA Question with a focus is a must if you want to develop the intuition for its solution. What I used to do or some people I saw do is that they skim the DSA Question thinking that they’ll just read what the question wants us to find, but to be honest that approach is not going to take you anywhere.
Try to read the words one by one consciously, it is not necessary that you will understand everything in 1 read, you can always go back in re-read the question. Be fully in charge of the understanding of the question like:
- What is the ideology behind the question?
- What are the input and output going to be?
- Are there any constraints, if yes what are they?
These are some of the things you should be able to answer if you have read the question well. Don’t just skim the question in a hurry, thinking that you know everything or even if you have read the question before. Give a little time to read the question, it’s the first step towards solving the DSA question effectively. If you don’t understand the question first, you’ll never be able to come up with a solution for that.
Always use a Pen and Paper first
Creativity is born on pen and paper. After you have understood the question, it’s time to draw out the solution for the same. Pick a pen and paper to write out the algorithm for the question. Research has also proven that writing can provide mental clarity, better process information, and organize thoughts better.
By using pen and paper to write down your approach, your ideas to approach the solution will become clear, and a great way to build a solid understanding. You can take a sample input given in the question or any of your choice, then start solving the question step-by-step. It can be written as a pseudocode if you want.
Breaking Down the Problem
To solve the DSA question effectively, you need to break the problem into sub-problems. This stands as a nice approach in order to solve questions as you don’t have to face a very big chunk of the question but can focus solely on smaller parts and eventually solve the whole question.
You should then focus on each sub-problem individually and if a sub-problem seems too complex don’t be shy to break it down more. It’s all a part of creating a less complex problem to face and eventually being able to bring a solution to the table. You can create a separate function for each sub-problem in your code, making it more manageable and easy to handle.
Later on, once you have broken down the problem and found a solution to it, you can merge them to create a solution to the original problem. It all might sound intimidating at first but believe me it isn’t that hectic once you get a good hand at it, you might face a little bit of resistance at first but it will get better as you solve more and more problems.
Find a Brute-Force Solution, Time and Space Complexity
There is a mistake that lots of programmers make while solving DSA questions. They try to figure out the best solution in one go and underestimate the problem, this is where things get complex. Focusing on finding a Brute-Force Solution first is the best step you can take while solving a DSA problem.
It is not necessary to know the optimal solution at first sight, it is simply an impractical thing to think about. We as humans try to improve things with time by looking at the flaws in the current system and how can be improved. what can be made better? what can be omitted?
Similarly, you just have to focus on finding a brute-force solution to the DSA problem first. Even in interviews, the interviewer doesn’t expect you to come up with the best approach just after reading the question. Hence, it makes things a little bit easier to move on.
Once, you are done with finding the Brute-Force Solution. Find out the Time and Space Complexity of your Brute-Force solution, that’s what we need to improve.
Optimize the Complexities of Solution
The last step in effectively solving DSA problems is to optimize it. Yes, optimization is the backbone of software engineering. You cannot just build things and leave it as it is. You have to constantly look for places where you can do better, for better speed, and for better memory management.
In the previous step, you have found out the complexity of your solution in terms of time and space which are addressed with BigO Notations. So let’s say your previous solution’s time complexity was
O(N2), to optimize it you should derive a solution with
O(N) time complexity. The same applies to the space complexity.
Find out the spots where you are using unnecessary loops or Data Structures, if they can be removed or optimized then make sure to follow up on that.
Quick Summary – Solve DSA Problems Effectively
To summarize all that we have discussed, solving a DSA problem is not difficult once you have the right approach and a clear understanding of what you need to do. The steps we discussed like Clearing the basics of language, Using a Pen and Paper, Breaking down the problem into Subproblems, Finding a Brute-Force Solution, and Optimizing the Solution will make your work much easier than just rushing into coding a solution.
Try to adopt these steps next time you solve Data Structures and Algorithms problems online and see if you can reap benefits from them.