How to Develop Algorithmic Thinking in DSA?

Algorithmic thinking has recently become a buzzword in interviews! It is a way of solving problems by clearly defining a sequence of logical steps. This can help us apply problem-solving strategies.

Let's understand the importance of algorithmic thinking from a simple perspective. If we observe, two skills are important for solving algorithmic problems:

  1. Thought process and steps to design an efficient solution.
  2. Converting the solution approach into the correct code.

Some programmers struggle with the first skill, which requires algorithmic thinking. During interviews, some employers are more interested in how a candidate approaches a problem than in the implementation itself. So, demonstrating good algorithmic thinking can help impress a potential employer. The critical question is: How do we develop algorithmic thinking? There are several ways to do it. Let's explore!

Developing Algorithmic Thinking via Solving Puzzles

Solving algorithmic puzzles is a good activity for developing algorithmic thinking. It will help us to improve our ability to break down complex problems and develop solutions. For example, we can learn several problem-solving strategies through puzzles:

  • Decrease and conquer (Josephus problem)
  • Divide and conquer (Finding a fake among eight coins)
  • Dynamic programming (Egg dropping puzzle)
  • Backtracking (8-queen puzzle)
  • Greedy approach (Fractional knapsack problem)
  • String manipulation (Fizz buzz problem)
  • Mathematical approach (The water jug puzzle)

Here are some more examples of such types of puzzles:

Puzzle based on the properties of numbers theory

Bulbs in a circle puzzle

Puzzle based on properties of graph

The Icosian game puzzle

Puzzle based on incremental approach

Rectangle disection puzzle

Puzzle based on graph-based transformation

Chessboard transformation puzzle

Puzzle based on dynamic programming

Finding maximum sum puzzle

Puzzle based on incremental approach

Square build up puzzle

Puzzle based on idea of backtracking

Eight queen on a chessboard puzzle

Puzzle based on iterative elimination

Finding celebrity puzzle

Puzzle based on properties of the graph

The Konigberg bridge puzzle

Puzzle based on idea of elimination

Single elimination tournament puzzle

Puzzle based on idea of transformation

Row and column exchange puzzle

Puzzle based on mathematical counting

Mental arithmatic puzzle

Some critical ideas related to solving algorithmic puzzles:

  • Every puzzle has an input. This input can be either specific (find a fake coin among 8 coins using a balance) or general (find a fake coin among N coins using a balance).
  • When working on a specific instance of a puzzle, it's important to focus on the particular instance given to you. This is because other instances of the puzzle may not have the same solution.
  • Even if a puzzle is presented in its general form, it can be helpful to try solving a few small instances first. This can provide valuable insights and help you better understand the solution for the general case.

Developing Algorithmic Thinking via Practicing Steps of Problem-Solving

To solve any problem, it's important to follow a series of well-defined steps. Practising these steps can help us save time and identify patterns that can be applied to multiple coding problems.

Step of problem-solving in data structure and algorithms

Step 1: Understanding the problem

To effectively solve a problem, it's important to clearly understand what is being asked. You should draw a visualisation and ask various critical questions.

  • Do we understand every word used in the problem?
  • What data or information is provided as input?
  • What data or results are requested as output?

It's important to understand the computational and non-computational details of the problem like data structures, input distribution, specific constraints, mathematical properties, etc.

Step 2: Selecting concepts and solution strategies

After analysing the problem, we need to identify concepts and approaches to solve it. We need to think around these questions:

  • Will we need to use all of the information provided in the problem?
  • Can we eliminate any unnecessary information?
  • Which conceptual idea is related to the given problem?
  • Which approach would be more appropriate to process the input data and obtain an efficient solution?
  • Have we solved any similar problems in the past? If so, how can we use that experience? Identifying concepts and approaches used to solve similar problems can save a lot of time and effort.

Step 3: Formulation of a solution strategy and pseudocode

To design a good solution, it can be helpful to first explore a handwritten approach by working through several examples and developing a general strategy. This requires thinking carefully about each step and identifying the actions common to all the examples.

Two types of thinking are important at this stage:

  • Iterative thinking: Solving the problem incrementally by building a partial solution.
  • Recursive thinking: Solving the problem using smaller sub-problems.

We need to describe on paper the operations required to transform the given input into the desired output. Then, we can write the steps in simple English and translate them into pseudocode or a flowchart. This helps clarify the logic and makes it easier to write the final code correctly.

Step 4: Correct Programming Implementation

Once you have a pseudocode solution, you can implement it in a programming language like C++, Java, or Python. During this process, it's important to carefully select programming elements that will help create effective code. This can include elements like solution function, helper functions, loops, base cases for recursive code, variables, pointers, extra memory, etc.

It’s also important to pay attention to memory management, preprocessing and details that can affect the code efficiency. Remember to follow a good coding style to make your code easy to read and understand.

Step 5: Verifying correctness and optimizing further

Once you have implemented your solution, it's important to test it for bugs, analyse its time and space complexity and think about ways to optimize it further. Here are some ideas that can be helpful:

  • Manually trace the input-output pattern by performing a hand trace or drawing a plot to describe the behavior of variables or critical steps in the solution.
  • Identify input cases that cause the algorithm to produce incorrect output. If there is an error or bug, identify and correct the steps responsible for it. Make sure the solution handles all boundary cases.
  • Analyze the solution’s efficiency in relation to the input size.
  • Try to optimize the solution by identifying key details of the problem. If possible, attempt to solve it using different approaches. Continue optimizing until no further improvements are possible. Sometimes, you can also optimize the code by reducing intermediate steps in the pseudocode.

Here are some popular coding questions to practice the steps of problem-solving. These questions can be solved using four or more approaches.

Algorithmic Thinking via Understanding Real-life Applications

Many applications use efficient strategies to deliver a great user experience and performance. Understanding these strategies is an important step in developing algorithmic thinking. Here are some good examples of these strategies:

  • Caching strategies such as LRU (Least Recently Used) and LFU (Least Frequently Used) cache.
  • Scheduling strategies such as round-robin scheduling and priority scheduling.
  • Hashing strategies can be used for tasks such as hash-based load balancing, data encryption in cryptography, symbol table creation in a compiler, and spelling checks.
  • Sorting strategies include comparison-based sorting and linear time sorting.

Enjoy learning, Enjoy thinking!

Self-paced Courses and Blogs