Foundations to FrontierPart IV: Algorithms and systemsBezhig

Cumulative checkpoint: algorithms and data structures

6 exercises, each with a complete solution.

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.

CA.01CSolution

A dynamic array doubles capacity when full. Explain why one append can cost Θ(n)\Theta(n) while a sequence of nn appends costs O(n)O(n) total copying work. Why does this not mean every append has a worst-case constant bound?

CA.02CSolution

Trace lower bound for target 55 in [1,2,5,5,9,12][1,2,5,5,9,12] using half-open bounds and midpoint floor. State the invariant and final index.

CA.03TSolution

For coin denominations 1,4,61,4,6 and amount 88, compare the largest-coin-first choice with an optimal solution. Give a dynamic-programming recurrence and explain how its subproblem represents the original objective.

CA.04CSolution

In a directed weighted graph with edges S→A:4S\to A:4, S→B:1S\to B:1, B→A:1B\to A:1, A→G:1A\to G:1, and B→G:8B\to G:8, find the shortest path and explain why settling A at its first discovery would fail.

CA.05TSolution

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.

CA.06TSolution

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 nn elements, so its individual cost can be linear. Across growth capacities 1,2,4,…1,2,4,\ldots, the total copies before reaching nn elements form a geometric sum less than 2n2n, up to the chosen initial-capacity convention. Add the nn 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.01
CA.02Show solution

Maintain the partition outside the search interval

Start lo=0,hi=6lo=0,hi=6. Midpoint 3 has value 5, so set hi=3hi=3. Midpoint 1 has value 2, so set lo=2lo=2. Midpoint 2 has value 5, so set hi=2hi=2. Return 2. All indices below lolo have values less than the target, and all indices at or above hihi 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.02
CA.03Show solution

Optimal substructure concerns the remaining amount

Greedy picks 6+1+16+1+1, using three coins. The optimal choice 4+44+4 uses two. Let D(0)=0D(0)=0 and D(a)=1+min⁡c≤aD(a−c)D(a)=1+\min_{c\le a}D(a-c) over allowed coins with reachable remainders; unreachable states have infinity. Any nonempty solution ends with some coin cc, leaving amount a−ca-c. 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.03
CA.04Show solution

Discovery is a tentative bound

The shortest route is S→B→A→GS\to B\to A\to G with cost 1+1+1=31+1+1=3. A is first discovered through S→AS\to A 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.04
CA.05Show solution

Expected behavior depends on hashing assumptions

With nn keys in one chain, lookup can inspect all nn entries, and insertion that first checks for an existing key can also take Θ(n)\Theta(n). 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.05
CA.06Show solution

The movement model determines the lower bound

From (0,0)(0,0) to (1,1)(1,1), one diagonal costs 1 while Manhattan distance is 2, an overestimate. With unit-cost eight-neighbor moves, Chebyshev distance max⁡(∣Δx∣,∣Δy∣)\max(|\Delta x|,|\Delta y|) 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.06

Retention 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.