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.
Not sure where to begin? Follow this order.
The 12-week roadmap: six phases of two weeks each, each building on the one before.
- 1Arrays and strings
- 2Search and simple structures
- 3Trees and graphs
- 4Ranges, heaps and choices
- 5Optimization
- 6Mocks, review and extras
All 20 patterns
Study in the order of appearance.
Essential patterns
12 patterns · Our picks for what comes up most. Learn these first.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 →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 →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 →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 →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 →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 →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 →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 →Intervals
Use it when: Overlapping ranges and scheduling.
Practice: Merge Intervals, Insert Interval, Meeting Rooms II
Read the guide →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 →Backtracking
Use it when: Generate all combinations, permutations or subsets.
Practice: Subsets, Permutations, N-Queens
Read the guide →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 · Our picks for stronger loops. Add these once the essentials feel comfortable.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 →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 →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 →Topological sort
Use it when: Tasks with dependencies that must run in a valid order.
Practice: Course Schedule, Alien Dictionary
Read the guide →Union-Find
Use it when: Group items into connected sets that merge over time.
Practice: Number of Provinces, Redundant Connection
Read the guide →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 →Tries
Use it when: Many words searched or matched by prefix.
Practice: Implement Trie, Word Search II
Read the guide →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 →Put the patterns into practice
Each pattern has problems with tested solutions, from Easy to Hard.