Lecture 21 — Binary Search Everywhere

Position in the course

This is lecture 21 of 32. It belongs to the course arc described in the course outline and uses the standard interactive case method: display, attempt (5 minutes), discuss, reveal, name the technique.

Learning objectives

By the end of this lecture students can do all three under time pressure, which is what the four cases exercise.

Prerequisite knowledge

Before this lecture: Lectures 17 and 19 — scans and invariants

Assumed fluencies:

Preparation task: Play the number-guessing game optimally on 1–100; count your guesses.

Detailed topic outline

Core (mandatory):

  1. binary search on arrays
  2. the boundary family (first true/first false)
  3. invariants of lo/hi
  4. search on the answer

Extension (optional enrichment):

  1. monotonic predicates
  2. logarithmic cost, counted

Case sequence

CaseTitleDifficulty
cs-081Guess the Number, FormallyBeginner
cs-082The First Bad VersionFoundational
cs-083Book AllocationIntermediate
cs-084The Rotten TimelineExpert

Display each case full-screen from the student page (cases cs-081–cs-084); instructor pages cs-081–cs-084 under case-studies/instructor/ hold the full solutions, reveal guides, and discussion prompts.

Timing plan (120 minutes)

MinutesSegment
0–10Framing: where this fits; recall of the previous lecture
10–30Case 1 (cs-081): 5-minute attempt, then discussion and reveal
30–50Case 2 (cs-082): same rhythm
50–70Case 3 (cs-083): same rhythm, harder case
70–75Break
75–100Case 4 (cs-084): same rhythm, hardest case
100–115Synthesis: name the techniques; misconceptions check
115–120Exit questions, homework, next-lecture preview

Timing flexes with the room: if a case ignites the class, let the discussion run and compress the synthesis; if the attempts stall, harvest partial attempts rather than extending the clock.

Worked examples

Instructor demonstration to open the lecture: Guess-the-number (cs-081) played against the class: 7 questions guaranteed; then a wrong guess strategy that needs 100.

Classroom activities

#ActivityMinutesFormat and prompt
1Halving relay61–1000 guessed by relay; the question count is graphed against 1000's log2.
2Boundary drill7Five first-true boundaries written as lo/hi tables; common off-by-one exhibited and fixed.
3Monotone or not5Six predicates voted monotone/non-monotone; binary search eligibility is the lesson.

Board plan

Instructor explanation notes

Reading the room. Binary search generalises beyond arrays: any monotone predicate is searchable. The boundary family (first true) prevents the classic off-by-ones; drill it.

Running the reveal. Search-on-the-answer (cs-083) is the lecture's stretch; the feasibility predicate is the bridge from this lecture to optimisation in lecture 30.

Lecture focus. One sentence to repeat verbatim: Halve, and halve again; the predicate must be monotone.

Common misconceptions

Each case's instructor page lists the specific wrong turn to expect; the reveal should address it by name.

Student practice exercises

Core:

  1. Play optimal guessing (cs-081) on 1–512; write your question sequence.
  2. Build the lo/hi table for first-bad-version (cs-082) on 16 versions with the bad set {9..16}.
  3. Trace the feasibility predicate (cs-083) for maxLoad = 7 and maxLoad = 8 on provided books.

Enrichment (optional):

Formative assessment

Exit questions (choose one; answer on a card in 60 seconds):

  1. Why must the predicate be monotone for binary search? Expected: so that one test discards half the space forever
  1. What is searched in search-on-the-answer? Expected: the feasibility boundary over candidate answers, not the items
  1. How many halvings for 1,000,000? Expected: about 20 — log2 of a million

Cards are skimmed before the next lecture; three answers become the framing. Persistent wrong answers route students to the review stations of lecture 16 or to office hours.

Homework and preparation

Learning-outcome mapping

Differentiation