Mohammad Fraz
Founder and CEO @ LearnYard
20% DSA problem-solving patterns that cover 90% of
problems asked in every coding interview..
(From FAANG & every company in between)
Swipe
Most students think they struggle with DSA because their
logic is weak. But in reality, it’s usually their approach
that’s off.
If you want to solve problems quickly and with
consistent accuracy, you need to start recognizing
patterns.
Here’s the thing:
– Without understanding patterns, you’ll crack problems
you’ve seen before but get stuck on anything new.
– Patterns help you spot solutions faster, under
pressure, even for problems that look completely fresh.
– The more you practice thinking in patterns, the less
you’ll fear “unseen” questions.
That’s why I put together this DSA Pattern Cheatsheet, so
you can level up your approach, not just your speed.
Swipe
TWO POINTERS
Usage:
Used when you want to iterate over a sequence with two
indices moving at different speeds or directions.
Great for problems where comparisons or partitions are
needed in arrays or strings.
DS Involved:
Array, String
The Concept:
→ One pointer starts from the beginning, another from
the end or a specific index
→ Move based on conditions (e.g., match, sum, partition)
→ Efficient for avoiding nested loops
Sample Problems:
• Two Sum (Sorted)
• 3Sum
• Remove Duplicates from Sorted Array
• Container With Most Water
Swipe
SLIDING WINDOW
Usage:
Used when we need to process a subset of elements in
an array or string of fixed or variable size.
Common for problems involving sequences, frequency,
or sum within a moving range.
DS Involved:
Array, String, HashTable
Concept:
→ Maintain a start and end pointer
→ Expand or shrink the window based on a condition
→ Track aggregates like sum, max, count inside the
window
Sample Problems:
• Longest Substring Without Repeating Characters
• Maximum Sum of a Subarray of Size K
• Minimum Size Subarray Sum
• Fruits into Baskets
Swipe
PREFIX SUM
Usage:
Used to quickly calculate range-based operations like
sum, average, or frequency.
Avoids repeated work when querying subarrays.
DS Involved:
Array
Concept:
→ Precompute cumulative sums in an array
→ Range sum from index i to j becomes O(1)
→ Can be extended to 2D arrays
Sample Problems:
• Range Sum Query - Immutable
• Subarray Sum Equals K
• Product of Array Except Self
• Find Pivot Index
Swipe
HASHING
Usage:
Provides fast O(1) average-time lookup, ideal for
frequency counting, duplicates, and set operations.
DS Involved:
Array, String, HashMap, HashSet
Concept:
→ Use keys to store elements, counts, or indices
→ Enables quick existence checks
→ Often combined with prefix sum or sliding window
Sample Problems:
• Two Sum
• Group Anagrams
• Valid Anagram
• Longest Consecutive Sequence
Swipe
MONOTONIC STACK
Usage:
Used when you need to find the next/previous greater or
smaller element efficiently.
DS Involved:
Stack, Array
Concept:
→ Maintain a stack in increasing or decreasing order
→ Pop elements until the current condition is satisfied
→ Helps in problems involving spans, histograms, or
limits
Sample Problems:
• Next Greater Element I
• Daily Temperatures
• Largest Rectangle in Histogram
• Online Stock Span
Swipe
STACK – PARENTHESES
Usage:
Used to validate or manipulate nested structures like
brackets or expressions.
Essential in parsing problems or expression balancing.
DS Involved:
Stack, String
Concept:
→ Push opening brackets, pop when matching closers
appear
→ Stack should be empty at the end if valid
→ Track balance, depth, or position with each operation
Sample Problems:
• Valid Parentheses
• Minimum Add to Make Parentheses Valid
• Remove Outermost Parentheses
• Longest Valid Parentheses
Swipe
STACK – DESIGN PROBLEMS
Usage:
Build custom stack behavior like tracking min, supporting
batch operations, or managing multiple stacks.
DS Involved:
Stack, Custom Data Structures
Concept:
→ Wrap standard stack with extra logic or auxiliary
stacks
→ Track metadata (like frequency, min, or capacity)
→ Design with O(1) for push/pop/min whenever possible
Sample Problems:
• Min Stack
• Design a Stack With Increment Operation
• Dinner Plate Stacks
• Maximum Frequency Stack
Swipe
QUEUE – IMPLEMENTATION
Usage:
Helps simulate FIFO behavior using arrays, stacks, or
linked lists.
Foundation for real-world task schedulers, buffers, etc.
DS Involved:
Queue, Array, Stack
Concept:
→ Elements enter from one end and exit from the other
→ May require circular implementation for fixed size
→ Often paired with stack to simulate reversed behavior
Sample Problems:
• Implement Queue using Stacks
• Design Circular Queue
• First Negative Integer in Every Window
• Reverse First K Elements of Queue
Swipe
QUEUE – DEQUE PATTERN
Usage:
Used when you need to push/pop from both ends.
Especially powerful in sliding window max/min problems.
DS Involved:
Deque, Array
Concept:
→ Maintain candidates in increasing/decreasing order
→ Discard obsolete elements as the window slides
→ Keep only useful elements that affect current state
Sample Problems:
• Sliding Window Maximum
• Max Value of Equation
• Jump Game VI
• Shortest Subarray with Sum at Least K
Swipe
BINARY SEARCH
Usage:
Best for searching in sorted arrays or when a monotonic
condition allows for halving the search space.
DS Involved:
Array, Matrix
Concept:
→ Maintain low and high pointers
→ Shrink the search space based on mid-point
evaluation
→ Can also be used on answer space or rotated arrays
Sample Problems:
• Binary Search
• Find Minimum in Rotated Sorted Array
• Search in Rotated Sorted Array
• Koko Eating Bananas
Swipe
MATRIX TRAVERSAL
Usage:
Used to explore or modify 2D grids row-wise, column-
wise, diagonally, or in a spiral order.
DS Involved:
2D Matrix
Concept:
→ Use nested loops or direction vectors to move across
cells
→ Track visited cells or boundaries to avoid overflow
→ Useful in simulation or search across grid layouts
Sample Problems:
• Spiral Matrix
• Matrix Diagonal Sum
• Rotate Image
• Set Matrix Zeroes
Swipe
HASHING + PREFIX SUM
Usage:
Combines hashmaps with cumulative sums to answer
subarray sum queries in constant time.
DS Involved:
HashMap, Array
Concept:
→ Track running sum and its frequency in a hashmap
→ If (curr_sum - target) exists, a valid subarray is found
→ Reduces O(n²) to O(n) in many subarray-related
problems
Sample Problems:
• Subarray Sum Equals K
• Binary Subarrays with Sum
• Count Number of Nice Subarrays
• Number of Submatrices That Sum to Target
Swipe
LINE SWEEP
Usage:
Efficient for processing overlapping intervals or events
sorted by time or position.
DS Involved:
Array, Sorting
Concept:
→ Sort all events by time (start/end or x-coords)
→ Sweep from left to right, updating active intervals
→ Useful in calendar, area, and traffic simulations
Sample Problems:
• My Calendar II
• Car Pooling
• Maximum Population Year
• Rectangle Area II
Swipe
HEAP
Usage:
Used when you need quick access to min/max in a
dynamic dataset — core of priority queues.
DS Involved:
Heap, PriorityQueue
Concept:
→ Maintain a heap to auto-sort elements by value or
custom rules
→ Min-heap = smallest at top, Max-heap = largest at top
→ Great for k-th element, real-time top N, and greedy
choices
Sample Problems:
• Top K Frequent Elements
• Kth Largest Element in an Array
• Last Stone Weight
• IPO
Swipe
LINKEDIN LIST
Usage:
Dynamic linear structure that allows fast
insertion/deletion without shifting elements.
DS Involved:
Linked List
Concept:
→ Nodes point to the next (and optionally previous)
→ Track head, tail, prev, next carefully
→ Used in LRU cache, stacks, and custom data structures
Sample Problems:
• Reverse Linked List
• Merge Two Sorted Lists
• Linked List Cycle
• Remove Nth Node From End of List
Swipe
TRIE
Usage:
Efficient for prefix-based operations — autocomplete,
search, dictionary compression.
DS Involved:
Trie (Prefix Tree)
Concept:
→ Each node stores part of a word and its children
→ Branch as per character match
→ Optimizes repeated string/prefix operations
Sample Problems:
• Implement Trie
• Design Add and Search Words DS
• Replace Words
• Longest Word in Dictionary
Swipe
KADANE’S ALGO
Usage:
Dynamic programming technique to find the max sum
subarray in linear time.
DS Involved:
Array, DP
Concept:
→ At each index, either extend the current subarray or
start fresh
→ Track global max across all positions
→ Can be adapted to products, deletions, or 2D cases
Sample Problems:
• Maximum Subarray
• Maximum Product Subarray
• Maximum Sum of 3 Non-Overlapping Subarrays
• K-Concatenation Maximum Sum
Swipe