Several Coding Patterns for Solving Data Structures and Algorithms Problems during Interviews
These are my notes in Javascript from a course that categorizes coding interview problems into a set of 16 patterns.
Additional Resources
Here are a few other resources that I found helpful when learning Data Structures and Algorithms using JavaScript
- Run JS - A JavaScript playground for your desktop
- Big O CheatSheet - Reference for Big-O complexities of common algorithms
- Blind 75 - For additional practice, the Blind75 list of interview questions is
🔥 . I would approach the questions in this order or this order or create a custom Blind75 study plan - Edabit is a great resource if you need additional Javascript practice before you start using leetcode.
- LeetCode of course
😋 - Pramp - for mock interviews
- FreeCodeCamp's Algorithms - Extra practice, esp. the Sort Algorithms section.
- Build 15 JavaScript Projects - Vanilla JavaScript Course - for extra Vanilla JavaScript/DOM Manipulation practice.
🌟 Challenge Questions
Blind 75
👩🏽🦯 Questions from😐 📖 Questions tagged FaceBook/Meta
🌴 Questions tagged Amazon
🔎 Questions tagged Google
Pattern 1: Sliding Window
In many problems dealing with an array (or a LinkedList), we are asked to find or calculate something among all the contiguous subarrays (or sublists) of a given size. For example, take a look at this problem:
Given an array, find the average of all contiguous subarrays of size
K
in it.
Lets understand this problem with a real input:
Array: [1, 3, 2, 6, -1, 4, 1, 8, 2], K=5
A brute-force algorithm will calculate the sum of every 5-element contiguous subarray of the given array and divide the sum by 5 to find the average.
The efficient way to solve this problem would be to visualize each contiguous subarray as a sliding window of 5
elements. This means that we will slide the window by one element when we move on to the next subarray. To reuse the sum from the previous subarray, we will subtract the element going out of the window and add the element now being included in the sliding window. This will save us from going through the whole subarray to find the sum and, as a result, the algorithm complexity will reduce to O(N)
.
Pattern 2: Two Pointer
In problems where we deal with sorted arrays (or LinkedLists) and need to find a set of elements that fulfill certain constraints, the Two Pointers approach becomes quite useful. The set of elements could be a pair, a triplet or even a subarray. For example, take a look at the following problem:
Given an array of sorted
numbers
and atarget
sum, find a pair in the array whose sum is equal to the giventarget
.
To solve this problem, we can consider each element one by one (pointed out by the first pointer) and iterate through the remaining elements (pointed out by the second pointer) to find a pair with the given sum. The time complexity of this algorithm will be O(N^2)
where n
is the number of elements in the input array.
Given that the input array is sorted, an efficient way would be to start with one pointer in the beginning and another pointer at the end. At every step, we will see if the numbers pointed by the two pointers add up to the target sum. If they do not, we will do one of two things:
- If the sum of the two numbers pointed by the two pointers is greater than the target sum, this means that we need a pair with a smaller sum. So, to try more pairs, we can decrement the end-pointer.
- If the sum of the two numbers pointed by the two pointers is smaller than the target sum, this means that we need a pair with a larger sum. So, to try more pairs, we can increment the start-pointer.
Pattern 3: Fast & Slow pointers
The Fast & Slow pointer approach, also known as the Hare & Tortoise algorithm, is a pointer algorithm that uses two pointers which move through the array (or sequence/LinkedList) at different speeds. This approach is quite useful when dealing with cyclic LinkedLists or arrays.
By moving at different speeds (say, in a cyclic LinkedList), the algorithm proves that the two pointers are bound to meet. The fast pointer should catch the slow pointer once both the pointers are in a cyclic loop.
One of the famous problems solved using this technique was Finding a cycle in a LinkedList. Lets jump onto this problem to understand the Fast & Slow pattern.
Pattern 4: Merge Intervals
This pattern describes an efficient technique to deal with overlapping intervals. In a lot of problems involving intervals, we either need to find overlapping intervals or merge intervals if they overlap.
Given two intervals (a
and b
), there will be six distinct ways the two intervals can relate to each other:
a
andb
do not overlapa
andb
overlap,b
ends aftera
a
completely overlapsb
a
andb
overlap,a
ends afterb
b
completly overlapsa
a
andb
do not overlap
Understanding the above six cases will help us in solving all intervals related problems.
Pattern 5: Cyclic Sort
This pattern describes an interesting approach to deal with problems involving arrays containing numbers in a given range. For example, take the following problem:
You are given an unsorted array containing numbers taken from the range
1
ton
. The array can have duplicates, which means that some numbers will be missing. Find all the missing numbers.
To efficiently solve this problem, we can use the fact that the input array contains numbers in the range of 1
to n
.
For example, to efficiently sort the array, we can try placing each number in its correct place, i.e., placing 1
at index 0
, placing 2
at index 1
, and so on. Once we are done with the sorting, we can iterate the array to find all indices that are missing the correct numbers. These will be our required numbers.
Pattern 6: In-place Reversal of a LinkedList
In a lot of problems, we are asked to reverse the links between a set of nodes of a LinkedList. Often, the constraint is that we need to do this in-place, i.e., using the existing node objects and without using extra memory.
in-place Reversal of a LinkedList pattern describes an efficient way to solve the above problem.
Pattern 7: Tree Breadth First Search
This pattern is based on the Breadth First Search (BFS) technique to traverse a tree.
Any problem involving the traversal of a tree in a level-by-level order can be efficiently solved using this approach. We will use a Queue to keep track of all the nodes of a level before we jump onto the next level. This also means that the space complexity of the algorithm will be O(W)
, where W
is the maximum number of nodes on any level.
Pattern 8: Depth First Search (DFS)
This pattern is based on the Depth First Search (DFS) technique to traverse a tree.
We will be using recursion (or we can also use a stack for the iterative approach) to keep track of all the previous (parent) nodes while traversing. This also means that the space complexity of the algorithm will be O(H)
, where H
is the maximum height of the tree.
Pattern 9: Two Heaps
In many problems, where we are given a set of elements such that we can divide them into two parts. To solve the problem, we are interested in knowing the smallest element in one part and the biggest element in the other part. This pattern is an efficient approach to solve such problems.
This pattern uses two Heaps to solve these problems; A Min Heap to find the smallest element and a Max Heap to find the biggest element.
Pattern 10: Subsets
A huge number of coding interview problems involve dealing with Permutations and Combinations of a given set of elements. This pattern describes an efficient Breadth First Search (BFS) approach to handle all these problems.
Pattern 11: Modified Binary Search
As we know, whenever we are given a sorted Array or LinkedList or Matrix, and we are asked to find a certain element, the best algorithm we can use is the Binary Search.
Pattern 12: Bitwise XOR
XOR is a logical bitwise operator that returns 0
(false) if both bits are the same and returns 1
(true) otherwise. In other words, it only returns 1
if exactly one bit is set to 1
out of the two bits in comparison.
Pattern 13: Top 'K' Elements
Any problem that asks us to find the top/smallest/frequent K
elements among a given set falls under this pattern.
Pattern 14: K-way merge
This pattern helps us solve problems that involve a list of sorted arrays.
Whenever we are given K
sorted arrays, we can use a Heap to efficiently perform a sorted traversal of all the elements of all arrays. We can push the smallest (first) element of each sorted array in a Min Heap to get the overall minimum. While inserting elements to the Min Heap we keep track of which array the element came from. We can, then, remove the top element from the heap to get the smallest element and push the next element from the same array, to which this smallest element belonged, to the heap. We can repeat this process to make a sorted traversal of all elements.
Pattern 15: 0/1 Knapsack (Dynamic Programming)
0/1 Knapsack pattern is based on the famous problem with the same name which is efficiently solved using Dynamic Programming (DP).
In this pattern, we will go through a set of problems to develop an understanding of DP. We will always start with a brute-force recursive solution to see the overlapping subproblems, i.e., realizing that we are solving the same problems repeatedly.
After the recursive solution, we will modify our algorithm to apply advanced techniques of Memoization and Bottom-Up Dynamic Programming to develop a complete understanding of this pattern.
Pattern 16: 🔎 Topological Sort (Graph)
Topological Sort is used to find a linear ordering of elements that have dependencies on each other. For example, if event B
is dependent on event A
, A
comes before B
in topological ordering.