Question Bank
Graded conceptual, complexity, tracing, and proof problems organized across Weeks 1–17. Attempt each problem on paper before revealing the worked solution.
Asymptotic Analysis, Recurrences & Dynamic Arrays
Order the following functions by asymptotic growth rate from slowest to fastest: n log n, 2^n, sqrt(n), n^2, log_2(n).
Reveal Worked Solution
log_2(n) < sqrt(n) < n log n < n^2 < 2^n. For large n, logarithmic terms grow slower than fractional polynomials, which grow slower than linearithmic, quadratic, and exponential functions.Solve the recurrence T(n) = 4T(n/2) + n^2 using the Master Theorem and state the tight bound.
Reveal Worked Solution
a = 4, b = 2, and f(n) = n^2, so n^(log_2 4) = n^2. Because f(n) = Theta(n^2) matches Case 2, the tight bound is T(n) = Theta(n^2 log n).Prove using the potential method that appending n elements to an initially empty dynamic array that doubles capacity on overflow and never shrinks runs in O(1) amortized time per append.
Reveal Worked Solution
Phi(i) = 2*size_i - cap_i (always non-negative when capacity starts at 1 and doubles). On a non-resizing append, actual cost c_i = 1 and Delta Phi = 2, giving amortized cost 3. On overflow when size = cap = k, actual cost is k + 1 (copying k items plus inserting 1), capacity becomes 2k, and Delta Phi = (2(k+1) - 2k) - (2k - k) = 2 - k, so amortized cost is (k + 1) + (2 - k) = 3 = O(1).Sorting Lower Bounds, Stacks, Queues & Linked Structures
Why does Python's list.pop(0) take O(n) time, whereas collections.deque.popleft() takes O(1) time?
Reveal Worked Solution
list is backed by a contiguous pointer array, so removing index 0 shifts all remaining n - 1 references left by one slot. By contrast, deque uses a doubly linked list of fixed-size memory blocks, updating only the head block pointer in O(1).Trace Insertion Sort on [7, 3, 5, 2] after each outer-loop iteration and explain why it runs in O(n + I) time where I is the number of inversions.
Reveal Worked Solution
[7, 3, 5, 2]. After inserting 3: [3, 7, 5, 2] (1 shift). After inserting 5: [3, 5, 7, 2] (1 shift). After inserting 2: [2, 3, 5, 7] (3 shifts). Each inner shift removes exactly one inversion, so total work across all n - 1 passes is proportional to n + I.Explain the decision-tree proof that every deterministic comparison-based sort requires Omega(n log n) comparisons in the worst case, and explain how Radix Sort bypasses this bound.
Reveal Worked Solution
n! input permutations, so its binary decision tree needs at least n! leaves. A binary tree of height h has at most 2^h leaves, giving h >= log_2(n!) = Theta(n log n). Radix Sort avoids pairwise key comparisons by bucketing digits over a bounded alphabet of size k in O(d(n + k)) time.Binary Search Trees, AVL Rotations & Priority Queues
In a 0-indexed array storing a binary max-heap, what are the parent, left-child, and right-child indices of node i?
Reveal Worked Solution
i: parent is (i - 1) // 2 (for i > 0), left child is 2*i + 1, and right child is 2*i + 2.Insert keys 10, 20, 15 into an initially empty AVL tree. Identify the imbalance pattern after inserting 15 and show the resulting balanced tree.
Reveal Worked Solution
15, left child 10, right child 20.Show why Floyd's bottom-up heapify builds a binary heap of n elements in O(n) time rather than O(n log n).
Reveal Worked Solution
h above the leaves, there are at most ceil(n / 2^(h+1)) subtrees, each sifting down at most h levels. Summing over all heights gives sum_{h=0}^{log n} (n / 2^(h+1)) * h = (n/2) * sum_{h>=1} h / 2^(h-1). Because the infinite series converges to 2, total sift work is bounded by O(n).Hash Tables, BFS/DFS, Dijkstra & Minimum Spanning Trees
Why does deleting a key from a linear-probing hash table require placing a tombstone sentinel instead of marking the slot empty?
Reveal Worked Solution
EMPTY breaks the probe chain for keys inserted after a collision past that slot, causing subsequent lookups for those keys to terminate early and return false misses. A tombstone keeps the probe search continuing on lookup and allows reuse on insert.Given a weighted directed graph with edges S->A (1), S->B (2), B->A (-3), trace Dijkstra's algorithm starting at S and show why it returns an incorrect shortest distance.
Reveal Worked Solution
dist[S]=0, pops S, sets dist[A]=1 and dist[B]=2. Next it pops A with distance 1 and finalizes it, never relaxing the later path S->B->A of total weight 2 + (-3) = -1. Thus negative edges violate Dijkstra's greedy finality invariant; use Bellman-Ford.Prove the Cut Property for Minimum Spanning Trees and explain how Kruskal's algorithm uses Union-Find (DSU) to maintain its invariant.
Reveal Worked Solution
(S, V \ S) be any cut and e = (u, v) be a lightest crossing edge. Suppose an MST T omits e; adding e creates a cycle containing at least one other crossing edge e' with w(e') >= w(e). Replacing e' with e yields another spanning tree T' with weight at most w(T), proving e belongs to an MST. Kruskal sorts edges by weight and uses DSU find(u) != find(v) to check in O(alpha(V)) amortized time whether e crosses between distinct components.Dynamic Programming, KMP Tries & NP-Completeness
In the 1D space-optimized 0/1 Knapsack recurrence dp[w] = max(dp[w], val + dp[w - wt]), why must the inner loop iterate w downwards from W to wt?
Reveal Worked Solution
dp[w - wt] still holds the value from the previous item row i - 1. Iterating upwards would allow the newly updated dp[w - wt] from the current item i to be reused multiple times, solving Unbounded Knapsack instead.Compute the KMP prefix function pi (longest proper prefix-suffix lengths) for the pattern P = "ABABAC" and explain its worst-case search complexity.
Reveal Worked Solution
0..5 of ABABAC: pi = [0, 0, 1, 2, 3, 0] (index 4 ABABA shares proper prefix/suffix ABA of length 3; mismatch at C falls back cleanly). Because the fallback pointer increases at most m times total, building pi takes O(m) and scanning a text of length n takes O(n) without moving the text cursor backwards.Define NP-completeness and prove that Vertex Cover is NP-complete assuming Clique is known to be NP-complete.
Reveal Worked Solution
Vertex Cover is in NP because given a candidate vertex subset C of size at most k, a verifier checks every edge (u, v) has u in C or v in C in O(V + E) time. Second, reduce Clique <=p Vertex Cover: given (G, k) with |V| = n, build the complement graph G_bar where edges absent from G are present in G_bar. A set S of size k is a clique in G if and only if V \ S of size n - k touches every edge in G_bar, forming a vertex cover. Construction runs in O(V^2) polynomial time.