Topics  /  DSA patterns
Coding & DSA

Data Structures and Algorithms

A small set of patterns shows up again and again in coding interviews. Start with the essentials, then add the next-level ones. Each guide shows how to spot the pattern and how to code it.

spot_the_pattern.md
Sorted array, find a pairTwo pointers
Longest substring with a conditionSliding window
Cycle in a linked listLinked lists
Sorted input, search for a valueBinary search
Generate all combinationsBacktracking

Not sure where to begin? Follow this order.

Each step builds on the one before it.

  1. 1Hash maps
  2. 2Two pointers
  3. 3Sliding window
  4. 4Binary search
  5. 5Trees and graphs
  6. 6Dynamic programming
View the full roadmap →

All 20 patterns

Study in the order of appearance.

Essential patterns

12 patterns · Asked most often. Learn these first.
01Start here

Hash maps and counting

Use it when: You need instant lookup, counting or grouping.

Practice: Two Sum, Group Anagrams, Top K Frequent Elements

Read the guide →
02

Two pointers

Use it when: A sorted array, or a pair or triplet that meets a condition.

Practice: Two Sum II, 3Sum, Container With Most Water

Read the guide →
03

Sliding window

Use it when: A contiguous subarray or substring with a max, min or exact condition.

Practice: Longest Substring Without Repeating Characters, Minimum Size Subarray Sum

Read the guide →
04

Binary search

Use it when: Sorted input, a rotated array, or a yes or no check that flips once (search the answer).

Practice: Search in Rotated Sorted Array, Koko Eating Bananas, Median of Two Sorted Arrays

Read the guide →
05

Linked lists

Use it when: Cycles, the middle node, or reversing and merging lists in place.

Practice: Linked List Cycle, Reverse Linked List, Merge Two Sorted Lists

Read the guide →
06

Stacks and monotonic stack

Use it when: Matching brackets, or the next greater or smaller element.

Practice: Valid Parentheses, Daily Temperatures, Largest Rectangle in Histogram

Read the guide →
07

Trees and recursion

Use it when: Anything on a tree: depth, paths, ancestors, validity.

Practice: Maximum Depth, Lowest Common Ancestor, Validate Binary Search Tree

Read the guide →
08

Graphs and grids (BFS and DFS)

Use it when: Connected regions, shortest path with equal steps, reachability.

Practice: Number of Islands, Rotting Oranges, Clone Graph

Read the guide →
09

Intervals

Use it when: Overlapping ranges and scheduling.

Practice: Merge Intervals, Insert Interval, Meeting Rooms II

Read the guide →
10

Heap and top K

Use it when: The kth largest, the most frequent, or merging sorted streams.

Practice: Kth Largest Element, Merge k Sorted Lists, Find Median from Data Stream

Read the guide →
11

Backtracking

Use it when: Generate all combinations, permutations or subsets.

Practice: Subsets, Permutations, N-Queens

Read the guide →
12

Dynamic programming

Use it when: Overlapping subproblems and an optimal substructure.

Practice: Climbing Stairs, Coin Change, Longest Common Subsequence, Edit Distance

Read the guide →

Next-level patterns

8 patterns · Common in stronger loops. Add these once the essentials feel comfortable.
13

Prefix sums

Use it when: Many range-sum queries, or subarrays that add up to k.

Practice: Subarray Sum Equals K, Range Sum Query

Read the guide →
14

Sorting and quickselect

Use it when: Order the data first, or find the kth element without a full sort.

Practice: Sort Colors, Kth Largest Element, Merge Intervals

Read the guide →
15

Greedy

Use it when: A locally best choice at each step leads to the best overall answer.

Practice: Jump Game, Gas Station, Assign Cookies

Read the guide →
16

Topological sort

Use it when: Tasks with dependencies that must run in a valid order.

Practice: Course Schedule, Alien Dictionary

Read the guide →
17

Union-Find

Use it when: Group items into connected sets that merge over time.

Practice: Number of Provinces, Redundant Connection

Read the guide →
18

Shortest paths (Dijkstra)

Use it when: The shortest path in a graph with weighted edges.

Practice: Network Delay Time, Cheapest Flights Within K Stops

Read the guide →
19

Tries

Use it when: Many words searched or matched by prefix.

Practice: Implement Trie, Word Search II

Read the guide →
20

Design a data structure

Use it when: Build a class with fast operations, such as a cache or an iterator.

Practice: LRU Cache, Min Stack, Time Based Key-Value Store

Read the guide →
Practice

Put the patterns into practice

Each pattern has problems with tested solutions, from Easy to Hard.

Two SumEasy
3SumMedium
Median of Two Sorted ArraysHard