Case-based lecture: you attempt, we discuss, then we reveal.
cs-081, cs-082, cs-083, cs-084The catalogue from lecture 17 is now sorted by code: CS1, CS3, DS2. Find 'DS2' in at most two comparisons.
codes = ["CS1", "CS3", "DS2"]
lo, hi = 0, len(codes) - 1
probes = 0
found = None
while lo <= hi:
m = (lo + hi) // 2
probes += 1
if codes[m] == "DS2":
found = m
break
if codes[m] < "DS2":
lo = m + 1
else:
hi = m - 1
print(found, probes)Expected: found at index 2 after 2 probes
Cost: O(log n): a million sorted entries need 20 probes. Linear scan needed a million. That is the sort of factor that changes what is feasible.
Commits 1..10; every commit from some point on is bad. Badness probe answers True/False. Locate the first bad commit in as few probes as possible.
bad_from = 7
def is_bad(m):
return m >= bad_from
lo, hi = 1, 10
while lo < hi:
m = (lo + hi) // 2
if is_bad(m):
hi = m
else:
lo = m + 1
print(lo)Expected: first bad commit: 7
Cost: O(log n) probes - the classic 'binary search on the answer' template that returns in lecture 30 (optimisation formulations).
Book pages 10, 20, 30, 40 and 2 students; each student gets a contiguous run. Minimise the largest share.
pages = [10, 20, 30, 40]
def feasible(cap, students_max=2):
students, load = 1, 0
for p in pages:
if load + p > cap:
students += 1
load = 0
load += p
return students <= students_max
lo, hi = max(pages), sum(pages)
while lo < hi:
mid = (lo + hi) // 2
if feasible(mid):
hi = mid
else:
lo = mid + 1
print(lo)Expected: minimum largest share: 60
Cost: O(n log(sum)) - the log factor from the cap search, the linear factor from each feasibility sweep.
The guessing game as binary search: guarantee the number of questions. Halving as an idea before any code.
**Input:** secret in 1..100; each guess answered 'high/low/correct'**Output:** 7 guesses always suffice: ceil(log2 101); 6 for most, 7 worst caseSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-081 — not embedded in this deck.
Versions 1..n, one onward is 'bad'. Find the first bad one with minimal checks. The canonical boundary-finding binary search.
**Input:** n = 12 versions, first bad = 8; is_bad calls counted**Output:** first bad = 8 found with 4 calls (vs 8 linear)Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-082 — not embedded in this deck.
Split pages across k readers so the busiest reads as few as possible. Binary search the answer, feasibility check inside. Search-on-answer as a pattern.
**Input:** pages [10, 20, 30, 40], k = 2 readers**Output:** best split: {10,20,30 | 40} max load 60 - vs {10,20 | 30,40} = 70; answer 60Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-083 — not embedded in this deck.
An event switches state once and stays switched (monotone); find when, given a testable 'state at time t'. Generalising binary search to monotone predicates.
**Input:** a service is healthy before 14:37 and degraded after; 'state at time t' is testable**Output:** find switch minute with ~log2(minutes) tests; predicate: state(t) == degradedSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-084 — not embedded in this deck.