Steps of Problem Solving in Data Structures and Algorithms

Every solution starts with a strategy and an algorithm is a strategy for solving a coding problem. So, we must learn to design an efficient algorithm and translate it into the correct code to get the job done.

But there are several problems available in data structures and algorithms, and most of the time, these problems are new to us. So as programmers, we need to develop ourselves as confident problem-solvers who are not intimidated by the difficulty of the given problem. 

Our long-term goal should be simple: Learn to design correct and efficient code within a given time. As we practice more and more, we will gain experience in problem-solving and our work will become easier. Here are some critical skills that we should practice for every DSA problem:

  • Developing an approach to understanding the problem
  • Thinking of a correct basic solution
  • Designing step-by-step pseudocode
  • Analysing the efficiency of a solution
  • Optimizing the solution further
  • Transforming pseudocode into correct code

Now, the critical question would be: Is there a well-defined, guided strategy to approach and solve a coding problem? If yes, then what are the critical steps? Let's think and explore!

Steps of problem-solving in algorithms and data structures

Step 1: Understanding the problem

Solving a problem requires a clear understanding of the problem. Unfortunately, sometimes we read only the first few lines and assume the rest of the problem or ignore this step because we have seen something similar in the past. We should view these as unfair practices and develop a clear approach.

During problem-solving, a small detail can help us design an efficient solution. Sometimes, a small change in the question can alter the solution approach. Taking extra time to understand the problem will give us more confidence later on. The fact is, we never want to realise halfway through that we misunderstood the problem.

It doesn't matter if we have encountered a question before or not; we should read the question several times. So, take a paper and write down every relevant details while going through the problem. Exploring some examples will also help us clarify how many cases our algorithm need to handle. We should also explore scenarios for large input, edge cases and invalid input.

Sometimes, the problem description may be ambiguous or incomplete. So, it is our responsibility to work with the interviewer or problem provider to clarify them. We should try to ask these questions:

  • What are the inputs and outputs?
  • What type of data is available?
  • What is the size or scale of the input?
  • How is the data stored? What is the data structure?
  • Are there any special conditions or orders in the data?
  • What rules exist for working with the data?

Step 2: Thinking of a correct basic solution

The best approach would be to think of a correct solution that comes immediately to our mind. It does not matter even if it is an inefficient approach. Having a correct and inefficient answer is much better than an incorrect solution or delay in finding the solution. This could help us in so many ways:

  • Help us to build good confidence at the start.
  • Provide an excellent point to start a conversation with the interviewer.
  • Sometimes, it provides a hint to improve efficiency by reducing some loops, removing some intermediate steps or performing some operations efficiently.

Here are some examples of brute force patterns: three nested loops, two nested loops, solution using extra memory, solution using sorting, considering all sub-arrays or substrings, exhaustive search, etc.

After thinking and communicating the brute force idea, the interviewer can ask for its time and space complexity. We need to work on paper, analyse each critical operation and write it in the form of Big-O notation. Clear conceptual idea of time and space complexity analysis is critical.

Step 3: Designing efficient solution with pseudocode

This is a stage to use the experience of problem-solving and apply problem-solving strategies. Sometimes moving from a brute force approach to the most efficient approach require several iterations. Each time, we should try to optimize the previous approach and stop when there is no further optimisation possible. Revisiting the problem and looking for additional detail can help a lot in further optimisation. For example:

  • If the input array is sorted or nearly sorted and problem is to search something, we can apply ideas like binary search, two-pointers, single loop, etc.
  • If we need to find something in a window of size k, we can use the sliding window technique.
  • When searching is a critical operation, we can use binary search, BST or hash table.
  • For optimisation problems, we can use dynamic programming or greedy approaches.
  • If we need to find a solution with a given constraint, we can use backtracking.
  • When working with string data, direct address tables, hash tables, or trie can be useful.
  • To frequently access and process max or min elements, we can use a priority queue.
  • For dictionary operations like insert, search, and delete, we can use hash table or BST.
  • If we need to perform both dictionary and priority queue operations, a BST can be useful.
  • For range query operations like range max, range min, or range sum, we can use data structures like segment trees or Fenwick trees.
  • To process binary tree data level by level, we can use BFS or level-order traversal.

The idea would be simple: we should learn the use case of efficient problem-solving patterns. Continuously thinking, analysing, and looking for a better solution is the core idea. 

Here are some best examples of problems where several levels of optimisations are feasible. Practicing such types of coding questions helps a lot in building confidence.

Find equilibrium index of an array

  • Using nested loops: Time = O(n²), Memory = O(1)
  • Using prefix sum array: Time = O(n), Memory = O(n)
  • Using single scan: Time = O(n), Memory = O(1)

Trapping rain water

  • Using nested loops: Time = O(n²), Memory = O(1)
  • Using Dynamic Programming: Time = O(n), Memory = O(n)
  • Using Stack: Time = O(n), Memory = O(n)
  • Using two pointers: Time = O(n), Memory = O(1)

Check for pair with a given sum

  • Using nested loops: Time = O(n²), Memory = O(1)
  • Using sorting and binary search: Time = O(nlogn), Memory = O(1)
  • Using sorting and Two Pointers: Time = O(nlogn), Memory = O(1)
  • Using a Hash Table: Time = O(n), Memory = O(n)

Find the majority element in an array

  • Using two nested loops: Time = O(n²), Memory = O(1)
  • Using Sorting: Time = O(nlogn), Memory = O(1)
  • Using the divide and conquer: Time = O(nlogn), Memory = O(logn)
  • Using a Hash Table: Time = O(n), Memory = O(n)
  • Using Bit Manipulation: Time = O(n), Memory = O(1)
  • Using Randomisation: Time = O(nlogn), Memory = O(1)
    Note: If value of n is very large.
  • Boyer-Moore Voting Algorithm: Time = O(n), Memory = O(1)

Maximum Subarray Sum

  • Using three nested loops: Time = O(n^3), Memory = O(1)
  • Using two nested loops: Time = O(n^2), Memory = O(1)
  • Using divide and conquer: Time = O(nlogn), Memory = O(logn)
  • Using dynamic programming: Time = O(n), Memory = O(n)
  • Kadane algorithm: Time = O(n), Memory = O(1)

Before you jump into the end-to-end code implementation, it’s good practice to write pseudocode on paper. It would be helpful in defining code structure and critical operations. Some programmers skip this step, but writing the final code becomes easier when we have well-designed pseudocode.

Top 10 problem solving approaches in DSA to master coding interview

Step 4: Transforming pseudocode into a clean, correct, and optimised code

Finally, we need to replace each line of pseudocode with actual code in our favorite programming languages like C++, Java, Python, C#, JavaScript, etc. Never forget to test actual code with sample test data and check if the actual output is equal to the expected output. When writing code in your interviews, discuss sample data or test cases with the interviewer.

Simplifying and optimizing the code may require a few iterations of observation. We need to ask these questions once we are done writing the code: 

  • Does this code run for every possible input, including the edge cases?
  • Can we optimize the code further? Can we remove some variables or loop or some extra space?
  • Are we repeating some steps a lot? Can we define it separately using another function?
  • Is the code readable or written with a good coding style?

Enjoy learning, Enjoy coding, Enjoy algorithms!

More from EnjoyAlgorithms

Self-paced Courses and Blogs