Data structures High interview relevance
Save Mark learned Copy link
Time Index O(1); append O(1) amortized; insert or delete in the middle O(n) Space O(n) When to use: The default sequence: indexed reads and appends at the end.
Why it costs this, code, and pitfalls →
Data structures High interview relevance
Save Mark learned Copy link Compare
Time Lookup, insert, delete O(1) expected; O(n) worst case Space O(n) When to use: Membership tests, counting, de-duplication, and mapping keys to values.
Why it costs this, code, and pitfalls →
Data structures High interview relevance
Save Mark learned Copy link Compare
Time Push, pop, peek O(1) Space O(n) When to use: Undo history, matching brackets, depth-first search, and evaluating expressions.
Why it costs this, code, and pitfalls →
Data structures High interview relevance
Save Mark learned Copy link Compare
Time Enqueue and dequeue O(1) with a deque Space O(n) When to use: Breadth-first search, scheduling, and sliding-window maximum (with a deque).
Why it costs this, code, and pitfalls →
Data structures Medium interview relevance
Save Mark learned Copy link
Time Insert or delete at a known node O(1); search O(n) Space O(n) When to use: Frequent insertions and removals at positions you already hold, such as an LRU cache paired with a hash map.
Why it costs this, code, and pitfalls →
Data structures High interview relevance
Save Mark learned Copy link Compare
Time Peek O(1); push and pop O(log n); build from n items O(n) Space O(n) When to use: Top-k problems, scheduling by priority, Dijkstra's algorithm, and merging sorted lists.
Why it costs this, code, and pitfalls →
Data structures High interview relevance
Save Mark learned Copy link Compare
Time Search, insert, delete O(h): O(log n) balanced, O(n) worst case Space O(n) When to use: Ordered data with inserts and lookups. In practice, prefer a balanced tree or a sorted container.
Why it costs this, code, and pitfalls →
Data structures Medium interview relevance
Save Mark learned Copy link Compare
Time Search, insert, delete O(log n) worst case Space O(n) When to use: You need ordered operations such as range queries, predecessor, and successor, with a worst-case guarantee.
Why it costs this, code, and pitfalls →
Data structures Medium interview relevance
Save Mark learned Copy link
Time Insert and search O(m) for a key of length m Space O(total characters) When to use: Prefix lookup, autocomplete, word search, and storing a dictionary of words.
Why it costs this, code, and pitfalls →
Data structures High interview relevance
Save Mark learned Copy link
Time Amortized O(α(n)) per find or union Space O(n) When to use: Connected components, cycle detection in undirected graphs, and Kruskal's algorithm.
Why it costs this, code, and pitfalls →
Data structures High interview relevance
Save Mark learned Copy link
Time O(n) total Space O(n) When to use: Next greater or smaller element, daily temperatures, and the largest rectangle in a histogram.
Why it costs this, code, and pitfalls →
Sorting Medium interview relevance
Save Mark learned Copy link Compare
Time Θ(n log n) average; Θ(n²) worst case Space O(log n) stack when recursing into the smaller part first; O(n) for a naive version When to use: General in-memory sorting where average speed matters and stability does not.
Why it costs this, code, and pitfalls →
Sorting High interview relevance
Save Mark learned Copy link Compare
Time Θ(n log n) in every case Space O(n) When to use: You need a stable sort with a guaranteed n log n bound, or you are sorting linked lists or external data.
Why it costs this, code, and pitfalls →
Sorting Medium interview relevance
Save Mark learned Copy link Compare
Time Θ(n log n) for distinct keys Space O(1) when done in place When to use: You need a guaranteed n log n bound with O(1) extra space.
Why it costs this, code, and pitfalls →
Sorting Medium interview relevance
Save Mark learned Copy link Compare
Time Θ(n + k) for values in a range of size k Space O(n + k) When to use: Integers, or items mapped to integers, in a small known range.
Why it costs this, code, and pitfalls →
Sorting Medium interview relevance
Save Mark learned Copy link
Time O(n log n) worst case; O(n) on already-sorted input Space O(n) When to use: Almost always, unless the problem asks you to implement a sorting algorithm.
Why it costs this, code, and pitfalls →
Searching High interview relevance
Save Mark learned Copy link Compare
Time O(log n) Space O(1) iterative When to use: Sorted random-access data, or any yes/no condition that flips once, as in search-on-the-answer problems.
Why it costs this, code, and pitfalls →
Searching Medium interview relevance
Save Mark learned Copy link Compare
Time O(n); O(1) if the target is first Space O(1) When to use: Unsorted data, small lists, or a one-off lookup.
Why it costs this, code, and pitfalls →
Graphs and trees High interview relevance
Save Mark learned Copy link Compare
Time O(V + E) Space O(V) When to use: Shortest path in an unweighted graph, level-order traversal, and minimum-steps problems on grids.
Why it costs this, code, and pitfalls →
Graphs and trees High interview relevance
Save Mark learned Copy link Compare
Time O(V + E) Space O(V) When to use: Connected components, cycle detection, topological ordering, and backtracking over graphs and trees.
Why it costs this, code, and pitfalls →
Graphs and trees High interview relevance
Save Mark learned Copy link
Time O(n) Space O(h) for depth-first orders When to use: Visiting every node: copying, serializing, evaluating, or validating a binary search tree with inorder traversal.
Why it costs this, code, and pitfalls →
Graphs and trees High interview relevance
Save Mark learned Copy link Compare
Time O((V + E) log V) with a binary heap Space O(V + E) When to use: Shortest paths from one source when every edge weight is non-negative.
Why it costs this, code, and pitfalls →
Graphs and trees Medium interview relevance
Save Mark learned Copy link Compare
Time O(V · E) Space O(V) When to use: Negative edge weights, or when you must detect negative cycles.
Why it costs this, code, and pitfalls →
Graphs and trees Medium interview relevance
Save Mark learned Copy link Compare
Time O(E log E) Space O(V + E) When to use: A minimum spanning tree from an edge list, especially on sparse graphs.
Why it costs this, code, and pitfalls →
Graphs and trees Medium interview relevance
Save Mark learned Copy link Compare
Time O(E log V) with a binary heap; O(V²) with an array Space O(V + E) When to use: A minimum spanning tree grown from a start vertex. The array version suits dense graphs.
Why it costs this, code, and pitfalls →
Graphs and trees High interview relevance
Save Mark learned Copy link
Time Edge check: matrix O(1), list O(deg(u)) Space List O(V + E); matrix O(V²) When to use: Use an adjacency list for most problems. Use a matrix when the graph is dense or edge checks dominate.
Why it costs this, code, and pitfalls →
Patterns High interview relevance
Save Mark learned Copy link
Time O(n) Space O(1) When to use: Sorted arrays (pair sums), in-place partitioning or de-duplication, and palindrome checks.
Why it costs this, code, and pitfalls →
Patterns High interview relevance
Save Mark learned Copy link
Time O(n) Space O(1) or O(n) When to use: Longest or shortest contiguous subarray or substring that satisfies a constraint.
Why it costs this, code, and pitfalls →
Patterns High interview relevance
Save Mark learned Copy link
Time O(n) to build; O(1) per range sum Space O(n) When to use: Many range-sum queries on a fixed array, or counting subarrays with a given sum (with a hash map).
Why it costs this, code, and pitfalls →
Patterns High interview relevance
Save Mark learned Copy link
Time Expected O(n) total Space O(n) When to use: Anagrams, duplicates, majority element, two-sum, and top-k frequent items.
Why it costs this, code, and pitfalls →
Patterns High interview relevance
Save Mark learned Copy link
Time O(2ⁿ) subsets; O(n · 2ⁿ) including copying each one Space O(n) recursion, plus stored output When to use: Enumerating combinations: subsets, combinations, N-Queens, Sudoku. Prune as soon as a partial answer is invalid.
Why it costs this, code, and pitfalls →
Patterns Medium interview relevance
Save Mark learned Copy link
Time O(n · n!) to generate all Space O(n) recursion, plus stored output When to use: Listing every ordering of a collection. Only practical for small n, roughly up to 10 or 11.
Why it costs this, code, and pitfalls →
Patterns High interview relevance
Save Mark learned Copy link
Time Number of states × transitions per state Space Depends on memoization or tabulation When to use: Overlapping subproblems with optimal substructure: counting ways, minimizing cost, knapsack, and edit distance.
Why it costs this, code, and pitfalls →
Patterns High interview relevance
Save Mark learned Copy link
Time O(n log n) Space O(n) When to use: Overlapping meetings, ranges, and calendar problems.
Why it costs this, code, and pitfalls →