Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Pattern Index
DSA

Pattern Index

Explore the major DSA problem-solving patterns and learn when each pattern should be considered.

Pattern Recognition

Most candidates try to solve problems.

Top candidates first identify the pattern, then apply a known solution framework.

LeetCode is not 2000 random problems.

It is ~40 repeatable patterns disguised with different stories.

If you master pattern recognition, you stop guessing and start diagnosing.


The Core Idea

Every problem can be reduced to:

  1. Input Type
  2. Constraint Signals
  3. What Is Being Optimized
  4. Implicit Data Structure
  5. Traversal Strategy
  6. State Relationship

Pattern recognition = mapping these signals → known algorithm families.


Step 1 — Identify the Input Type

The fastest first filter.

Input TypeLikely Patterns
ArrayTwo pointers, Sliding window, Prefix sum, Binary search
StringSliding window, Hashing, KMP, Trie
Linked ListFast/slow pointers, Reverse, Cycle detection
TreeDFS, BFS, Tree DP
GraphDFS, BFS, Topological sort, Union-Find
MatrixBFS, DFS, Multi-source BFS
StreamHeap, Two heaps
Interval listGreedy, Sorting, Merge intervals

If you identify input correctly, you eliminate 70% of wrong approaches.


Step 2 — Look for Optimization Keywords

The question almost always tells you the pattern.

Sliding Window Signals

  • “longest substring”
  • “smallest window”
  • “at most k”
  • “contiguous subarray”

Think:

Expand → Shrink → Maintain constraint.


Two Pointer Signals

  • “sorted array”
  • “palindrome”
  • “reverse”
  • “pair sum”

Think:

Left pointer + Right pointer.


Binary Search Signals

  • “sorted”
  • “minimum maximum”
  • “search space”
  • “answer range 1 to 10^9”

If answer space is monotonic → binary search on answer.


Heap Signals

  • “k largest”
  • “top k”
  • “merge k”
  • “median in stream”

Think:

PriorityQueue.


Graph Signals

  • “dependencies”
  • “shortest path”
  • “minimum steps”
  • “can we reach”
  • “cycle detection”

Think:

BFS, DFS, Dijkstra, Union-Find.


DP Signals

  • “maximum/minimum”
  • “number of ways”
  • “subsequence”
  • “partition”
  • “can we form”

Ask:

Does smaller state combine to form larger state?


Step 3 — Check Constraint Size

Constraints tell you allowed time complexity.

n SizeAllowed Complexity
n ≤ 20Exponential possible
n ≤ 100O(n³) sometimes ok
n ≤ 10⁴O(n²) borderline
n ≤ 10⁵O(n log n) or O(n)
n ≤ 10⁶O(n) only

If n = 10^5 → brute force is wrong.

Let constraints eliminate bad approaches.


Step 4 — Identify Relationship Type

Ask:

Is this about:

  • Contiguous elements?
  • Subsequences?
  • Connectivity?
  • Ordering?
  • Frequency?
  • Partitioning?
  • Range queries?
  • Greedy local choice?

Each maps to pattern families.


Pattern Categories (Complete Map)


1 Array Patterns

  • Two Pointers
  • Sliding Window
  • Prefix Sum
  • Kadane
  • Monotonic Stack
  • Binary Search
  • Greedy Sorting

2 String Patterns

  • Sliding Window
  • Hashing
  • KMP
  • Trie
  • Z Algorithm
  • DP on Strings

3 Linked List Patterns

  • Reverse
  • Fast/Slow pointer
  • Cycle detection
  • Merge lists

4 Tree Patterns

  • DFS (Pre/In/Post)
  • BFS (Level order)
  • Tree height
  • Diameter
  • LCA
  • Tree DP

5 Graph Patterns

  • DFS
  • BFS
  • Topological Sort
  • Dijkstra
  • Union-Find
  • Multi-source BFS

6 DP Families

Linear DP

One dimension.

2D Grid DP

Paths in matrix.

Subsequence DP

LIS, LCS.

Interval DP

Matrix Chain Multiplication.

Partition DP

Subset sum.

Bitmask DP

Small n state compression.


7 Greedy Patterns

  • Interval scheduling
  • Jump game
  • Activity selection
  • Merge intervals

Greedy works when:

Local optimal leads to global optimal.


8 Bit Manipulation

  • XOR tricks
  • Subset generation
  • Masking states
  • Counting bits

Signals:

  • n ≤ 20
  • subset
  • power of 2
  • parity

Step 5 — Translate Story → Mathematical Model

Example:

“You are climbing stairs…”

Translation: Count number of ways → Fibonacci → DP.


Example:

“Minimum steps to reach target”

Translation: Shortest path → BFS.


Example:

“Merge overlapping intervals”

Translation: Sort + Greedy merge.


Pattern Recognition Drill Framework

When reading a question, ask in order:

  1. What is input type?
  2. What is n?
  3. What is being optimized?
  4. Is order important?
  5. Is contiguity required?
  6. Does smaller solution build larger?
  7. Is graph implicitly hidden?
  8. Is answer monotonic?

This mental checklist becomes automatic after practice.


Example Walkthrough

Problem:

“Find longest substring without repeating characters.”

  • Input: String
  • Keyword: longest substring
  • Constraint: contiguous
  • No duplicates

Pattern: Sliding Window + HashMap.


Problem:

“Find number of islands.”

  • Input: grid
  • Connectivity
  • Count components

Pattern: DFS or BFS on matrix.


Problem:

“Course Schedule.”

  • Dependencies
  • Can finish?

Pattern: Topological Sort / Cycle detection in directed graph.


Advanced Pattern Recognition Signals


Multi-Source BFS

Signals:

  • Multiple starting points
  • Spread outward
  • Minimum time

Example pattern: Rotting oranges.


Binary Search on Answer

Signals:

  • Minimize maximum
  • Maximize minimum
  • Feasible check function

Example: Split array largest sum.


Monotonic Stack

Signals:

  • Next greater
  • Next smaller
  • Stock span
  • Histogram

Two Heaps

Signals:

  • Median
  • Streaming input

Union-Find

Signals:

  • Dynamic connectivity
  • Detect cycle
  • Merge components

Common Pattern Recognition Mistakes

  1. Jumping to DP when greedy works.
  2. Missing that array is sorted → two pointers possible.
  3. Not checking constraints.
  4. Ignoring monotonicity → missing binary search.
  5. Confusing substring vs subsequence.

How to Master Pattern Recognition

  1. Solve by category, not randomly.
  2. After solving, ask:
    • What was the pattern?
    • What were the signals?
  3. Maintain a pattern notebook.
  4. Redo same problem after 2 weeks.
  5. Practice identifying pattern without coding.

Final Principle

Interview success is not about intelligence.

It is about:

  • Structured thinking
  • Constraint analysis
  • Mapping problem → pattern quickly
  • Executing cleanly

When you stop “trying approaches” and start recognizing structures,
you move from average candidate → strong candidate.


My Private Notes

Notes are auto-saved locally to this device.