Patterns for designing algorithms

Last updated on

These are some patterns commonly occuring while designing algorithms. While, you may primarily devise algorithms to solve technical coding problems for your software interview, they are quite useful in real life as well. Thus, for each question I’ve not only included links to Leetcode questions but also references to real life applications. Also, this is a living document and would keep getting updated.

Sliding window

When you want to maintain a characteristic while looping through an array. For eg. In the longest substring without repeating characters problem, we maintain two pointers i and j which form the sliding window. We maintain the characteristic that the substring between i and j does not contain repeating characters. We keep moving j(right pointer) until we find a repeating character. Then we move i(left pointer) until the substring between i and j does not contain repeating characters. This is the general idea of sliding window.

Sliding window maximum number


Binary Search


Loop invariants

For a binary search algorithm we generally need to prove two loop invariants.

Real-life application

Git bisect

git bisect is a widely used command for identifying regression bugs. You specify a bad commit (typically the current branch’s tip) with the bug and a good commit from the past without the bug. The command then conducts a binary search, guiding you to the midpoint of the specified commits. You confirm whether the midpoint commit is good or bad, informing git accordingly. This reduces the search space through binary search, and the process repeats until the bug’s introduction commit is pinpointed.

Dynamic programming


Steps for solving with dynamic programming.

The ideas here are derived from MIT 6.006 lectures

Tips for dynamic programming algorithms:


🔁 and ❤️ this post on Linkedin | Twitter