Cumulative checkpoint: algorithms and data structures
Use after C06–C08 and J02’s first independent attempt. For every complexity claim, identify the operation counted, input size, and worst-case/expected/amortized meaning. Allow roughly 60–90 minutes or split the set across two sessions without viewing solutions in between.
A dynamic array doubles capacity when full. Explain why one append can cost while a sequence of appends costs total copying work. Why does this not mean every append has a worst-case constant bound?
Trace lower bound for target in using half-open bounds and midpoint floor. State the invariant and final index.
For coin denominations and amount , compare the largest-coin-first choice with an optimal solution. Give a dynamic-programming recurrence and explain how its subproblem represents the original objective.
In a directed weighted graph with edges , , , , and , find the shortest path and explain why settling A at its first discovery would fail.
A hash table uses chaining but every supplied key hashes to one bucket. Analyze lookup and insertion-with-duplicate-check in the worst case. Explain why resizing alone may not fix this family of inputs.
A grid gains unit-cost diagonal moves, but its A* implementation keeps Manhattan distance. Give a concrete overestimate and propose a valid lower-bound heuristic for the new unobstructed move model. State what walls do to admissibility.
Optional small hints
CA.01: sum the previous capacities geometrically. CA.03: consider the last coin of an optimal solution. CA.06: one diagonal can reduce both coordinate gaps at once.
Complete solutions: CA#
Compare the first step where your work differs. A corrected answer is not yet an independent success.
CA.01Show solution
Amortized work is a bound on a sequence
A resizing append copies the existing elements, so its individual cost can be linear. Across growth capacities , the total copies before reaching elements form a geometric sum less than , up to the chosen initial-capacity convention. Add the new-element writes to obtain linear total work, hence constant amortized cost per append. This redistributes expensive steps across the sequence; it does not erase the latency spike of a particular resize or establish real-time behavior.
Back to CA.01CA.02Show solution
Maintain the partition outside the search interval
Start . Midpoint 3 has value 5, so set . Midpoint 1 has value 2, so set . Midpoint 2 has value 5, so set . Return 2. All indices below have values less than the target, and all indices at or above have values at least the target. The uncertain interval shrinks until empty. Returning index 3 would find a matching value but violate the first-not-less-than contract.
Back to CA.02CA.03Show solution
Optimal substructure concerns the remaining amount
Greedy picks , using three coins. The optimal choice uses two. Let and over allowed coins with reachable remainders; unreachable states have infinity. Any nonempty solution ends with some coin , leaving amount . If the remaining solution were not optimal, replacing it would improve the whole solution, so minimizing over possible last coins is valid. The recurrence optimizes coin count, not coin value or a greedy local preference.
Back to CA.03CA.04Show solution
Discovery is a tentative bound
The shortest route is with cost . A is first discovered through at cost 4, but after settling B its cost improves to 2. Permanently fixing A on discovery would miss this improvement and could report a longer path. Nonnegative-edge Dijkstra reasoning applies to the minimum valid frontier distance, not to the first time a vertex is encountered.
Back to CA.04CA.05Show solution
Expected behavior depends on hashing assumptions
With keys in one chain, lookup can inspect all entries, and insertion that first checks for an existing key can also take . A sequence of such insertions can have quadratic total comparison work. Increasing bucket count does not help if the hash function or adversarial key family still maps every key to the same bucket. A better distribution, keyed hash strategy in suitable settings, or a different worst-case data structure may be needed. Expected constant-time language must state its assumptions.
Back to CA.05CA.06Show solution
The movement model determines the lower bound
From to , one diagonal costs 1 while Manhattan distance is 2, an overestimate. With unit-cost eight-neighbor moves, Chebyshev distance is a lower bound: each move changes each coordinate by at most one, and diagonal moves can reduce both gaps together. Obstacles can only remove routes or make them longer, so the unobstructed lower bound remains admissible. Different diagonal costs require another derivation rather than blindly reusing this formula.
Back to CA.06Retention and readiness#
Repair complexity labels in C06–C07 and frontier/heuristic reasoning in C08/J02. Readiness means selecting a method for an unlabeled problem and explaining a tempting alternative’s failure with a counterexample.