Search, Sorting, Trees & Heaps

Ordered structure turns brute force into logarithmic search, efficient selection and hierarchical state. The gain is reliable only when boundaries and representation invariants are explicit.

Section 2 of 5 · 8 units · about 35 hours

0/8

By the end you can Derive one binary-search boundary without patching it by examples, then solve one unseen tree and one heap or interval problem with adversarial tests and a worst-case defense.

Units

Build it

Mini project · about 10 to 14 hours

Mutable-priority heap, a derived boundary and a room scheduler

Build an indexed heap with update and remove, checked against a brute-force oracle. Derive one binary-search boundary from its predicate, then solve an unseen tree problem and a two-heap scheduling problem with adversarial tests and a worst-case defense.

  1. 1Implement an indexed min-heap with push, pop, peek, update and remove over a position map. State both invariants, heap order and index agreement, with the cost of each operation.
  2. 2Test it with a Hypothesis state machine against a scanning oracle, checking both invariants after every step. Seed one defect, such as an update that only sifts one way, and keep the shrunk history.
  3. 3Solve LC 1011 Capacity To Ship Packages Within D Days with tags hidden. Write the feasibility predicate, prove it monotone and derive the bounds and updates from the loop invariant before running any example.
  4. 4Solve LC 236 Lowest Common Ancestor of a Binary Tree recursively, then add an iterative version with a parent map, since deep paths exceed the default recursion limit. Test both against an ancestor-set oracle on random trees.
  5. 5Solve LC 2402 Meeting Rooms III with two heaps, freeing rooms by number and busy rooms by end time. Write adversarial tests for delays, ties and back-to-back meetings, and compare against a brute-force simulator on small random inputs.
  6. 6Write the worst-case notes: the time bound for LC 2402, and why delayed end times overflow fixed-width integers, naming the largest end time the constraints allow.
What you hand in
  • The indexed heap module with invariants, cost notes, the state machine and the seeded-defect history.
  • The LC 1011 boundary derivation with the monotonicity proof, written before code.
  • LC 236 and LC 2402 solutions with adversarial and differential tests and worst-case notes.
How to check it
How it's graded

Indexed heap

Pass: The state machine agrees with the oracle with both invariants checked after every step, and the seeded defect fails with a shrunk history.

Great: Edge positions such as the root and the last slot are covered, with operation costs checked against the stated bounds.

Boundary derivation

Pass: The invariant and updates are written before any example runs, and the judge accepts the derived solution.

Great: A property test checks the answer is the smallest feasible capacity: it holds, and one less fails.

Tree worst case

Pass: Both versions agree with the oracle, and the iterative version handles a deep path the recursive one cannot.

Great: The write-up shows the recursive failure on the deep tree and states the space of each version.

Room scheduler

Pass: LC 2402 is accepted, and the simulator agrees on small random inputs with ties and back-to-back meetings.

Great: The tie rules are stated as heap invariants, with the largest end time derived from the constraints.

Resources

  • DSA Handbook - search, trees and heapsHandbook Academy

    Study Part 2 Search and Sort, Part 5 Linked Lists, and Part 6 Trees and Heaps. Include canonical and answer-space binary search, comparison and linear-time sorts, quickselect, sentinels, cycles, heaps, BSTs, balanced-tree vocabulary, tries and segment trees.

  • AlgoMaster DSA - ordered structures sliceAlgoMaster

    Attempt Koko Eating Bananas, Validate Binary Search Tree and Find Median from Data Stream cold. Use Merge Intervals only if interval modeling remains weak, and include duplicate-heavy and skewed inputs.

  • Algorithmica - search and sorting performanceSergey Slotin

    Use the binary-search, sorting, branch prediction and cache chapters to explain why two algorithms with the same asymptotic bound cross over on real workloads.

  • DSA Handbook Patterns - quicksort vs mergesort vs heapsortHandbook Academy

    Use The decision, Algorithms compared and Side-by-side to pick a comparison sort from the binding constraint; problem details stay with their units.

  • DSA Handbook Patterns - quickselect vs heap for top-KHandbook Academy

    Use The decision through Side-by-side for the streaming, sorted-output and mutation routing; problem details stay with their units.

  • DSA Handbook Patterns - two-heap median archetypesHandbook Academy

    Use Streaming median and Variants side-by-side for the two-heap invariants; skip sliding-window median and sketches.

  • DSA Handbook Patterns - merge intervals archetypesHandbook Academy

    Use The decision, Variants and the LC 56 and LC 57 signature problems for merge and insert implementation, and the Meeting Rooms II archetype for the min-heap form.

  • DSA Handbook Patterns - sweep line archetypesHandbook Academy

    Use The decision and the 1D event sweep variant for +1/-1 concurrency counting with explicit tie-breaks; skyline and rectangle area are recognition only.

  • NeetCode - unseen ordered problemsNeetCode

    Use one unseen tree problem and one heap or interval problem as the final transfer check, with solution material closed until submission.

  • AlgorithmsJeff Erickson

    Use Chapter 1 (mergesort, quicksort, recursion trees and linear-time selection) when deriving merge, partition and selection algorithms, and lecture note 12 Lower Bounds for the comparison-sorting decision-tree argument. This is the proof reference when the handbook explanation is too brief.