20 Coding Patterns
to
Master MAANG Interviews
DesignGurus.org
Usage: This algorithmic technique is used when
we need to handle the input data in a specific
window size.
DesignGurus.org
Sliding Window
1.
DS Involved: Array, String, HashTable
Longest Substring with 'K' Distinct Characters
Fruits into Baskets
Sample Problems:
Usage: This pattern describes all the efficient
ways of traversing a matrix (or 2D array).
DesignGurus.org
2. Islands (Matrix Traversal)
DS Involved: Matrix, Queue
Number of Islands
Flood Fill
Cycle in a Matrix
Sample Problems:
Usage: This technique uses two pointers to
iterate input data. Generally, both pointers
move in the opposite direction at a constant
interval.
DesignGurus.org
3. Two Pointers
DS Involved: Array, String, LinkedList
Squaring a Sorted Array
Dutch National Flag Problem
Minimum Window Sort
Sample Problems:
Usage: Also known as Hare & Tortoise
algorithm. This technique uses two pointers
that traverse the input data at different speeds.
DesignGurus.org
4. Fast & Slow Pointers
DS Involved: Array, String, LinkedList
Middle of the LinkedList
Happy Number
Cycle in a Circular Array
Sample Problems:
Usage: This technique is used to deal with
overlapping intervals.
DesignGurus.org
5. Merge Intervals
DS Involved: Array, Heap
Conflicting Appointments
Minimum Meeting Rooms
Sample Problems:
Usage: Use this technique to solve array
problems where the input data lies within a
fixed range.
DesignGurus.org
6. Cyclic Sort
DS Involved: Array
Find all Missing Numbers
Find all Duplicate Numbers
Find the First K Missing Positive Numbers
Sample Problems:
Usage: This technique describes an efficient
way 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.
DesignGurus.org
7. In-place Reversal of a LinkedList
DS Involved: Array
Reverse every K-element Sub-list
Rotate a LinkedList
Sample Problems:
Usage: This technique is used to solve
problems involving traversing trees or graphs in
a breadth-first search manner.
DesignGurus.org
8. Breadth-First Search
DS Involved: Tree, Graph, Matrix, Queue
Binary Tree Level Order Traversal
Minimum Depth of a Binary Tree
Connect Level Order Siblings
Sample Problems:
Usage: This technique is used to solve
problems involving traversing trees or graphs in
a depth-first search manner.
DesignGurus.org
9. Tree Depth First Search
DS Involved: Tree, Graph, Matrix
Path With Given Sequence
Count Paths for a Sum
Sample Problems:
Usage: In many problems, we are given a set of
elements that can be divided into two parts. We
are interested in knowing the smallest element
in one part and the biggest element in the other
part. As the name suggests, this technique uses
a Min-Heap to find the smallest element and a
Max-Heap to find the biggest element.
DesignGurus.org
10. Two Heaps
DS Involved: Heap, Array
Find the Median of a Number Stream
Next Interval
Sample Problems:
DesignGurus.org
Usage: Use this technique when the problem
asks to deal with permutations or combinations
of a set of elements.
11. Subsets
DS Involved: Queue, Array, String
String Permutations by changing case
Unique Generalized Abbreviations
Sample Problems:
DesignGurus.org
Usage: Use this technique to search a sorted
set of elements efficiently.
12. Modified Binary Search
DS Involved: Array
Ceiling of a Number
Bitonic Array Maximum
Sample Problems:
DesignGurus.org
Usage: This technique uses the XOR operator
to manipulate bits to solve problems.
13. Bitwise XOR
DS Involved: Array, Bits
Two Single Numbers
Flip and Invert an Image
Sample Problems:
DesignGurus.org
Usage: This technique is used to find
top/smallest/frequently occurring ‘K’ elements in
a set.
14. Top ‘K’ Elements
DS Involved: Array, Heap, Queue
‘K’ Closest Points to the Origin
Maximum Distinct Elements
Sample Problems:
DesignGurus.org
Usage: This technique helps us solve problems
that involve a list of sorted arrays.
15. K-way Merge
DS Involved: Array, Queue, Heap
Kth Smallest Number in M Sorted Lists
Kth Smallest Number in a Sorted Matrix
Sample Problems:
DesignGurus.org
Usage: Use this technique to find a linear
ordering of elements that have dependencies
on each other.
16. Topological Sort
DS Involved: Array, HashTable, Queue
Tasks Scheduling
Alien Dictionary
Sample Problems:
DesignGurus.org
Usage: This technique is used to solve
optimization problems. Use this technique to
select elements that give maximum profit from
a given set with a limitation on capacity and that
each element can only be picked once.
17. 0/1 Knapsack
DS Involved: Array, HashTable
Equal Subset Sum Partition
Minimum Subset Sum Difference
Sample Problems:
DesignGurus.org
Usage: Use this technique to solve problems
that follow the Fibonacci numbers sequence,
i.e., every subsequent number is calculated
from the last few numbers.
18. Fibonacci Numbers
DS Involved: Array, HashTable
Staircase
House Thief
Sample Problems:
DesignGurus.org
Usage: This technique is used to solve
optimization problems related to palindromic
sequences or strings.
19. Palindromic Subsequence
DS Involved: Array, HashTable
Longest Palindromic Subsequence
Minimum Deletions in a String to make it a
Palindrome
Sample Problems:
DesignGurus.org
Usage: Use this technique to find the optimal
part of a string/sequence or set of
strings/sequences.
20. Longest Common Substring
DS Involved: Array, HashTable
Maximum Sum Increasing Subsequence
Edit Distance
Sample Problems:
➡Follow these techniques to distinguish
yourself from others! A number of these
approaches are discussed in "Grokking the
Coding Interview" and "Grokking
Dynamic Programming"
DesignGurus.org

coding patterns.pdf

  • 1.
    20 Coding Patterns to MasterMAANG Interviews DesignGurus.org
  • 2.
    Usage: This algorithmictechnique is used when we need to handle the input data in a specific window size. DesignGurus.org Sliding Window 1. DS Involved: Array, String, HashTable Longest Substring with 'K' Distinct Characters Fruits into Baskets Sample Problems:
  • 3.
    Usage: This patterndescribes all the efficient ways of traversing a matrix (or 2D array). DesignGurus.org 2. Islands (Matrix Traversal) DS Involved: Matrix, Queue Number of Islands Flood Fill Cycle in a Matrix Sample Problems:
  • 4.
    Usage: This techniqueuses two pointers to iterate input data. Generally, both pointers move in the opposite direction at a constant interval. DesignGurus.org 3. Two Pointers DS Involved: Array, String, LinkedList Squaring a Sorted Array Dutch National Flag Problem Minimum Window Sort Sample Problems:
  • 5.
    Usage: Also knownas Hare & Tortoise algorithm. This technique uses two pointers that traverse the input data at different speeds. DesignGurus.org 4. Fast & Slow Pointers DS Involved: Array, String, LinkedList Middle of the LinkedList Happy Number Cycle in a Circular Array Sample Problems:
  • 6.
    Usage: This techniqueis used to deal with overlapping intervals. DesignGurus.org 5. Merge Intervals DS Involved: Array, Heap Conflicting Appointments Minimum Meeting Rooms Sample Problems:
  • 7.
    Usage: Use thistechnique to solve array problems where the input data lies within a fixed range. DesignGurus.org 6. Cyclic Sort DS Involved: Array Find all Missing Numbers Find all Duplicate Numbers Find the First K Missing Positive Numbers Sample Problems:
  • 8.
    Usage: This techniquedescribes an efficient way 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. DesignGurus.org 7. In-place Reversal of a LinkedList DS Involved: Array Reverse every K-element Sub-list Rotate a LinkedList Sample Problems:
  • 9.
    Usage: This techniqueis used to solve problems involving traversing trees or graphs in a breadth-first search manner. DesignGurus.org 8. Breadth-First Search DS Involved: Tree, Graph, Matrix, Queue Binary Tree Level Order Traversal Minimum Depth of a Binary Tree Connect Level Order Siblings Sample Problems:
  • 10.
    Usage: This techniqueis used to solve problems involving traversing trees or graphs in a depth-first search manner. DesignGurus.org 9. Tree Depth First Search DS Involved: Tree, Graph, Matrix Path With Given Sequence Count Paths for a Sum Sample Problems:
  • 11.
    Usage: In manyproblems, we are given a set of elements that can be divided into two parts. We are interested in knowing the smallest element in one part and the biggest element in the other part. As the name suggests, this technique uses a Min-Heap to find the smallest element and a Max-Heap to find the biggest element. DesignGurus.org 10. Two Heaps DS Involved: Heap, Array Find the Median of a Number Stream Next Interval Sample Problems:
  • 12.
    DesignGurus.org Usage: Use thistechnique when the problem asks to deal with permutations or combinations of a set of elements. 11. Subsets DS Involved: Queue, Array, String String Permutations by changing case Unique Generalized Abbreviations Sample Problems:
  • 13.
    DesignGurus.org Usage: Use thistechnique to search a sorted set of elements efficiently. 12. Modified Binary Search DS Involved: Array Ceiling of a Number Bitonic Array Maximum Sample Problems:
  • 14.
    DesignGurus.org Usage: This techniqueuses the XOR operator to manipulate bits to solve problems. 13. Bitwise XOR DS Involved: Array, Bits Two Single Numbers Flip and Invert an Image Sample Problems:
  • 15.
    DesignGurus.org Usage: This techniqueis used to find top/smallest/frequently occurring ‘K’ elements in a set. 14. Top ‘K’ Elements DS Involved: Array, Heap, Queue ‘K’ Closest Points to the Origin Maximum Distinct Elements Sample Problems:
  • 16.
    DesignGurus.org Usage: This techniquehelps us solve problems that involve a list of sorted arrays. 15. K-way Merge DS Involved: Array, Queue, Heap Kth Smallest Number in M Sorted Lists Kth Smallest Number in a Sorted Matrix Sample Problems:
  • 17.
    DesignGurus.org Usage: Use thistechnique to find a linear ordering of elements that have dependencies on each other. 16. Topological Sort DS Involved: Array, HashTable, Queue Tasks Scheduling Alien Dictionary Sample Problems:
  • 18.
    DesignGurus.org Usage: This techniqueis used to solve optimization problems. Use this technique to select elements that give maximum profit from a given set with a limitation on capacity and that each element can only be picked once. 17. 0/1 Knapsack DS Involved: Array, HashTable Equal Subset Sum Partition Minimum Subset Sum Difference Sample Problems:
  • 19.
    DesignGurus.org Usage: Use thistechnique to solve problems that follow the Fibonacci numbers sequence, i.e., every subsequent number is calculated from the last few numbers. 18. Fibonacci Numbers DS Involved: Array, HashTable Staircase House Thief Sample Problems:
  • 20.
    DesignGurus.org Usage: This techniqueis used to solve optimization problems related to palindromic sequences or strings. 19. Palindromic Subsequence DS Involved: Array, HashTable Longest Palindromic Subsequence Minimum Deletions in a String to make it a Palindrome Sample Problems:
  • 21.
    DesignGurus.org Usage: Use thistechnique to find the optimal part of a string/sequence or set of strings/sequences. 20. Longest Common Substring DS Involved: Array, HashTable Maximum Sum Increasing Subsequence Edit Distance Sample Problems:
  • 22.
    ➡Follow these techniquesto distinguish yourself from others! A number of these approaches are discussed in "Grokking the Coding Interview" and "Grokking Dynamic Programming" DesignGurus.org