Data structures
| Structure | Access | Search | Insert | Delete | Space | Notes |
|---|---|---|---|---|---|---|
| Array / Python list | O(1) | O(n) | O(1)* | O(n) | O(n) | Index by position. Append is amortized O(1); insert or delete at the front shifts everything. |
| Hash map / dict / Map | n/a | O(1) | O(1) | O(1) | O(1) | Average case. Worst case O(n) with bad hashing. Keys must be hashable. |
| Hash set / set / Set | n/a | O(1) | O(1) | O(1) | O(1) | Membership in constant time. No order guarantees in Python sets. |
| Stack (list / array) | O(1) top | O(n) | O(1) | O(1) pop | O(n) | push and pop at the end only. |
| Queue / deque | O(1) ends | O(n) | O(1) | O(1) | O(n) | Use collections.deque in Python. Array.shift() in JS is O(n). |
| Linked list | O(n) | O(n) | O(1)† | O(1)† | O(n) | † when you already hold the node. Great for splicing, bad for indexing. |
| Binary heap | O(1) min | O(n) | O(log n) | O(log n) pop | O(n) | heapq is a min-heap. Build from a list in O(n) with heapify. |
| Balanced BST | O(log n) | O(log n) | O(log n) | O(log n) | O(n) | Sorted order. Python has none built in (use bisect on a sorted list or sortedcontainers). |
| Trie | n/a | O(k) | O(k) | O(k) | O(total chars) | k = word length. Prefix queries. |
| Graph (adjacency list) | n/a | O(deg) | O(1) | O(deg) | O(V + E) | BFS and DFS both run in O(V + E). |
Sorting
| Algorithm | Average | Worst | Extra space | Stable |
|---|---|---|---|---|
| Merge sort | O(n log n) | O(n log n) | O(n) | Yes |
| Quicksort | O(n log n) | O(n²) | O(log n) | No |
| Heap sort | O(n log n) | O(n log n) | O(1) | No |
| Timsort (Python, V8) | O(n log n) | O(n log n) | O(n) | Yes |
| Insertion sort | O(n²) | O(n²) | O(1) | Yes |
| Counting sort | O(n + k) | O(n + k) | O(k) | Yes |
Read the constraints
Input size tells you which complexity the interviewer expects. A computer does roughly 10⁸ simple operations per second.
| Input size | Target complexity | Typical approach |
|---|---|---|
n ≤ 10 | O(n!) or O(2ⁿ · n) | Backtracking over every permutation |
n ≤ 20 | O(2ⁿ) | Subsets, bitmask DP |
n ≤ 500 | O(n³) | Triple loops, Floyd–Warshall |
n ≤ 5,000 | O(n²) | Nested loops, 2D DP |
n ≤ 10⁶ | O(n log n) or O(n) | Sorting, heaps, two pointers, hashing |
n ≤ 10⁹+ | O(log n) or O(1) | Binary search, math |
Patterns and their giveaways
| Pattern | You will hear | Idea |
|---|---|---|
| Arrays & Hashing | "Have I seen this before?" · duplicates · frequency · grouping | Count, group, and look things up in O(1). |
| Two Pointers | Sorted input · pairs that sum to a target · palindromes · in-place | Walk inward or in step to avoid nested loops. |
| Sliding Window | Longest / shortest contiguous subarray or substring with a rule | Grow and shrink a range across the input. |
| Stack | Matching brackets · undo · next greater element · evaluate expressions | Last in, first out, for nesting and "next greater". |
| Binary Search | Sorted data · "minimum x such that…" · rotated arrays · O(log n) asked | Halve the search space every step. |
| Linked List | Reverse · merge · find the middle · detect a cycle | Rewire pointers, use fast and slow runners. |
| Trees | Hierarchies · depth · paths · BST ordering | Recursion with DFS, levels with BFS. |
| Heap / Priority Queue | Top k · k-th largest · merge sorted streams · scheduling | Always grab the smallest or largest fast. |
| Backtracking | All subsets / permutations / combinations · puzzles | Try, recurse, undo. Explore every choice. |
| Graphs | Grids · networks · dependencies · shortest path · islands | BFS, DFS, topological sort, union-find. |
| Dynamic Programming | Count the ways · min/max cost · "can you reach…" · overlapping choices | Reuse answers to smaller subproblems. |
| Greedy | Jump games · scheduling · "maximum number of…" | Take the best local choice when it is provably safe. |
| Intervals | Meetings · overlapping ranges · insert a range | Sort by start, then merge or sweep. |
| Bit Manipulation & Math | "Without extra space" · single number · powers of two · digits | XOR tricks, masks, and number properties. |
Before you code
- Restate the problem and confirm the input and output types.
- Walk one small example by hand, then one edge case: empty, single item, duplicates, negatives.
- Say the brute force and its complexity out loud.
- Name the pattern you think fits and why.
- Agree on the plan, then code it while narrating.
- Trace your code on the example, fix bugs, and state time and space complexity.