How to Practice Data Structures & Algorithms?

How to Practice Data Structures & Algorithms?
How to Practice Data Structures & Algorithms?

Introduction

Proficiency in the practices of Data structures and algorithms is the key to ace any coding test/interview but what matters the most the way one learns Data structures. An efficient learning approach combined with consistency will help you master DSA in no time.

Methods to Practice Data Structures & Algorithms

If you’ve just begun learning Data structures, the very first and important thing to keep in mind is the fact that you are in the learning phase and you can’t solve problems in the first go. 

  1. Pick up a language and know all the ins and outs of it. Have a stronghold on the syntax.
  2. Keep the right learning attitude. You need not search for advanced company-specific questions, rather start from the basic problems.
  3. Focus on developing problem-solving skills and logic building capacity. The best way to do so is to first solve the complete problem on pen and paper. Avoid jumping straightway to IDE in order to code.
  4. Learn to use the inbuilt debugger of IDEs like DEV, Visual studio. It’s going to save a lot of your time if you practice fixing the error with the help of debuggers.

Ways to increase your Problem Solving Ability

Practice everyday because consistency is the key to ace problem solving techniques. The right pattern to follow while practicing is:

DSA largely deals with two factors: Implementation and application. Hence, it is important for you to understand the backend working of every data structure along with its application, that is how you can modify the data structure to solve another problem. 

For instance, if you are learning stack, pay attention to the time complexity of push and pop operation and study in-depth about how these operations work internally. 

Apart from the code, following are the areas where one can focus:

  1. push(), pop(), isEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.
  2. There are two methods to implement a stack: 
    • Using array: All the operations regarding the stack including insertion, deletion, addition etc. are performed using arrays. 
    • Using a linked list: We store the information in the form of nodes and follow the stack rules. Here, all the major stack operations are performed using the head of a linked list.
  1. Pros and cons of array implementation:
    1. Pros: It is fairly easy to implement as compared to linked lists as memory is saved as pointers are not involved. 
    2. Cons: It is not dynamic. It doesn’t grow and shrink depending on needs at runtime.
  1. Pros and cons of Linked list implementation:
    • Pros: The linked list implementation of the stack can grow and shrink according to the needs at runtime. 
    • Cons: It requires extra memory due to the involvement of pointers.

Such detailed analysis of data structure will help you realise how, when and under what constraints you can apply these and solve problems. When you analyze a problem in depth, it means:

  • You can code it quickly and with higher accuracy.
  • You can code it with correct syntax, which means you are good at the language basics.
  • You can write clean code in one go because you know how the backend of the data structure works.
  • You can apply the same code to a new problem quickly because you can relate it to similar problems. 

Number of problems one should one practice

Ideally, 30 questions per topic is sufficient to attain a good grasp in the subject. The ideal flow of course should be:

  • 10 easy problems
  • 15 medium level
  • 5 difficult problems

Understanding problems & solutions

It’s well and good if you are able to solve the problem in the first go but if you aren’t, make sure that you spend at least two hours on each question. 

Let’s say you started working on a problem in the morning, then think for two hours and proceed to another question if you’re not able to crack it. Later at night spend another two hours on the same question and proceed further if you’re still unable to crack it. The next day, you may directly look up the solution, understand it and code it yourself.

blog banner 1

The important point to note here is you have to go back and revise that same question within 20 days so that it stays fresh in your memory. If you’re still unable to crack it then repeat the cycle all over again. 

Practice company-specific interview problems

Once you have solved 30 questions per topic and approximately 300 questions altogether, you will get a stronghold of DSA and will be able to solve 90% of the problems yourself. If not, then practice 10-15 more questions by yourself. Once you feel that you are confident and can solve most of the problems yourself then you may look for company-specific problems and start practising them.

Frequently Asked Questions

Where to practice Data structure and algorithms?

Follow anyone online platform. CodeStudio, Codechef and Leetcode are few options you can refer to. Always start from basic data structures like array, hashmap and then gradually move to advance data structure like tree and graph. Make sure to practice all the algorithms related to a particular data structure. Some of popular algorithm combinations to practice:

1. Depth First Search
2. Recursion + Memoisation
3. Hash Table + Linked List combination
4. Binary search tree modifications

I can’t develop logic for the problem and end up wasting a lot of time. How can I fix this?

Keep thinking and spend at least two hours on every problem. The most important thing to note is you have to develop an efficient revision strategy to keep revising the logic. Spare a few hours every weekend to revise the problems done throughout the week. If you’re unable to solve, look up the solution, understand it and then code it yourself. Keep a note of all the questions that you couldn’t solve in the first go and revisit them again.

What time should I see the solution?

Do not see the solution unless you have spent at least two hours on the problem and never copy the solution code. Understand the logic and then code it yourself.

Which is the best language to practice DSA?

C++ and Java is highly recommended and highly preferred by the recruiters. But since language has very little to do with Data structure, you can pick any language of your choice but make sure it is an OOP language.

Conclusion

It’s not about the number of questions but the quality of learning from questions that makes someone a great programmer. Hence, practice a total of 300 questions that is 30 questions per topic and stay consistent with your practice for at least 5-6 months. Never memorise the code. Understand the logic, practice the same code at regular intervals of 20 days. Even if you have solved the problem always look into all the different approaches to solve similar problems.

For more details, you can head to the Coding Ninjas page where you can have a chat with our counsellor who will guide you further.