Top Problem-Solving Approaches to Crack DSA Interview _ EnjoyAlgorithms
Top Problem-Solving Approaches to Crack DSA Interview _ EnjoyAlgorithms
Open in app
Search
Be part of a better internet. Get 20% off membership for a limited time
In this blog, we have discussed some popular strategies for solving problems in data
structures and algorithms. Mastering and applying these strategies can help you
more effectively tackle problems and crack the coding interview.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 1/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
1. Input-centric strategy: In this approach, we process one input at a time and build
the partial solution with each iteration of the loop.
2. Output-centric strategy: With this approach, we add one output to the solution at
a time and build the partial solution iteratively.
3. Iterative improvement strategy: This involves starting with some easily available
approximations to a solution and then continuously improving upon them until
we reach the final solution.
There are also several approaches based on loops: Using a single loop and variables,
Using nested loops and variables, Incrementing the loop by a constant (more than 1),
Using the loop twice (double traversal), Using a single loop and a prefix array (or
extra memory), etc.
Suggested coding problems to explore
Roman to Integer
Leaders in an array
FizzBuzz problem
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 2/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
Josephus problem
To use binary search, we will need to modify the standard binary search algorithm
based on the specific conditions of the problem. The core idea is to calculate the
mid-index and iterate over the left or right half of the array.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 3/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
By dividing the problem into smaller pieces, it becomes easier to tackle each piece
individually and then combine the solutions to get a final solution to the overall
problem.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 4/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 5/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
indices or pointers to simultaneously iterate over two different input parts, which
allows us to perform fewer operations.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 6/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
This approach can be effective when the problem involves tasks that must be
performed on a contiguous block of a fixed or variable size. It can also be useful for
improving time complexity by converting a nested loop solution into a single loop
solution.
Suggested problems to explore
Longest substring without repeating characters
down into two stages: the transformation stage and the conquering stage.
In the transformation stage, we take the original problem and modify it in some way
that makes it easier to solve. In the conquering stage, we then solve the transformed
problem.
Example problems: Pre-sorting based algorithms (Finding the closest pair of points,
checking whether all the elements in a given array are distinct, etc.)
In some cases, we can use either BFS or DFS regardless of the order of the nodes.
However, in other cases, the choice of traversal method can make a significant
difference in terms of efficiency. It’s important to consider the specific use case when
deciding which traversal method to use.
Preorder traversal: This type of traversal visits the root node first, followed by the
left subtree and then the right subtree. It’s useful when we need to explore all the
tree nodes before looking at any leaves.
Inorder traversal: This type of traversal visits the left subtree first, followed by the
root node and then the right subtree. It’s useful when we are working with a
binary search tree (BST) and we want to generate the node’s data in increasing
order.
Postorder traversal: This type of traversal visits the left subtree first, followed by
the right subtree and then the root node. It’s useful when we need to explore all
the leaf nodes before looking at any internal nodes.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 8/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
To solve tree and problems, sometimes we use these strategies with traversal:
Helper functions are useful when we want to break a complex problem into
smaller, more manageable parts. These helper functions can perform specific
tasks that are needed to solve the overall problem.
Parent pointers can be useful when we need to keep track of the relationship
between a node and its parent in a tree. This can be helpful when we need to
traverse up the tree to find certain information.
Storing additional data inside the nodes can be useful when we want to keep
track of certain information while traversing a tree or graph. For example, we may
want to store whether a node has been visited during a search, or we may want to
store the minimum distance from a particular node to the root.
Data structures like stacks, queues, and priority queues can be useful when we
need to store and access data in a specific order. For example, we can use a stack
to store nodes that we have visited during a depth-first search, or we can use a
queue to store nodes that we need to visit during a breadth-first search.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 9/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
Many coding problems require an efficient way to perform the search, insert and
delete operations. We can perform all these operations using the hash table in
O(1) time average. It’s a kind of time-memory tradeoff, where we use extra space
to store elements in the hash table to improve performance.
Other times, we may need to store data in a stack (last in, first out) or queue (first
in, first out) to solve certain problems.
Other data structures that can be useful in specific cases include Trie, AVL Tree,
and Segment Tree, which can help us perform certain operations efficiently.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 10/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
Valid parentheses
Valid anagram
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 11/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
This can be a very effective way to solve optimization and counting problems. By
storing the results of smaller subproblems and reusing them, we can avoid a lot of
unnecessary computation and improve the efficiency of our solution.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 12/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
In this approach, we take the best available option at each step and add it to our
partially constructed solution, without violating the constraints of the problem. This
can be an effective method in some cases, but it’s important to note that it doesn’t
always work. While it’s generally not too difficult to design a greedy algorithm, it can
be more challenging to prove that it produces an optimal solution.
Suggested problems to explore
Fractional knapsack problem
If the partial solution can be developed further without violating the constraints
of the problem, we do so by taking the first valid option available at the next
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 13/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
stage.
If there are no valid options at the next stage, meaning that the constraints of the
problem would be violated, we backtrack to the previous stage and try the next
option for that stage instead.
Combination sum
Sudoku solver
Bit manipulation involves understanding the binary representation of the input data
and processing it at the level of individual bits. This can lead to more efficient
solutions, as computers can perform bit-wise operations very quickly. In some cases,
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 14/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
bit manipulation can even eliminate the need for extra loops and improve
performance by a significant margin.
Suggested problems to explore
Reverse bits
Hope you enjoyed the blog. Later we will write a separate blog on each problem-
solving approach. Enjoy learning, Enjoy algorithms!
For more content, explore our free DSA course and coding interview blogs. If you
want to learn live, explore these two courses:
Follow
Founder enjoyalgorithms.com | IIT | Super 30 | Educator | A learner who enjoys computer science,
programming, algorithms and problem-solving.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 16/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
Feb 13 771 4
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 17/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 18/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 19/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
Mar 6 5.5K 29
Lists
ChatGPT
21 stories · 690 saves
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 20/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
Jan 10 73
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 21/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
BuketSenturk
DFS vs BFS
Used for traversing or searching through graph and tree structures.
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 22/23
6/27/24, 5:02 PM Top Problem-Solving Approaches to Crack DSA Interview | EnjoyAlgorithms
Feb 5 10
https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d 23/23