Case-based lecture: you attempt, we discuss, then we reveal.
cs-073, cs-074, cs-075, cs-076Is 'Level' a palindrome? Compare characters from both ends, ignoring case.
word = "Level"
lo, hi = 0, len(word) - 1
ok = True
while lo < hi:
if word[lo].lower() != word[hi].lower():
ok = False
break
lo += 1
hi -= 1
print(ok)Expected: True ('Level' is a palindrome, case-insensitively)
Cost: O(n) time but only n/2 comparisons, O(1) extra space - the reverse-compare alternative also reads O(n) but copies the string.
Sorted values 1, 3, 4, 6, 8. Find a pair summing to 10, or report none.
a = [1, 3, 4, 6, 8]
lo, hi = 0, len(a) - 1
found = None
while lo < hi:
s = a[lo] + a[hi]
if s == 10:
found = (a[lo], a[hi])
break
if s < 10:
lo += 1
else:
hi -= 1
print(found)Expected: pair found: 4 + 6 = 10
Cost: O(n) after sorting - each round removes one element from consideration for good; the all-pairs alternative is O(n^2).
Rearrange 3, 1, 2, 4 so all even numbers precede all odd numbers. Order within each group does not matter.
a = [3, 1, 2, 4]
lo, hi = 0, len(a) - 1
while lo < hi:
if a[lo] % 2 == 0:
lo += 1
else:
a[lo], a[hi] = a[hi], a[lo]
hi -= 1
print(a)Expected: [4, 2, 1, 3] - all evens before all odds
Cost: O(n) time, O(1) space, single pass - each step settles one element permanently.
Check a palindrome with two pointers from both ends. State the invariant ('everything outside the pointers is verified') and why it ends correctly.
**Input:** "Never odd or even" -> palindrome; "hello" -> not**Output:** True / False via two pointers; invariant: everything outside [l, r] already verifiedSolve 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-073 — not embedded in this deck.
In a sorted list, find two elements summing to a target. The classic two-pointer; focus on the movement rule as an invariant, not a memorised trick.
**Input:** sorted [1, 2, 4, 7, 11], target 15 -> 4+11; target 20 -> none**Output:** pair found (indices 3,4); none for 20; movement rule: sum small -> left++, sum big -> rightSolve 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-074 — not embedded in this deck.
Rearrange a list so evens precede odds, in-place. Two pointers from both ends with a loop invariant stated before code. Termination argument required.
**Input:** [3, 1, 6, 2, 5] -> evens first: [6, 2, 3, 1, 5] (order within groups free)**Output:** in-place two-pointer partition; invariant: [0,l) even, (r,n-1] odd; terminates because the gap shrinksSolve 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-075 — not embedded in this deck.
Detect a cycle in a sequence given only a 'next' function (Floyd's idea). The invariant is about distance; the aha is why they must meet. Proof-shaped thinking.
**Input:** next(i) cycles: 1->2->3->4->2 (cycle of length 3); slow/fast pointers from 1**Output:** they meet inside the cycle; meeting guaranteed because the gap closes by 1 each stepSolve 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-076 — not embedded in this deck.