Case-based lecture: you attempt, we discuss, then we reveal.
cs-041, cs-042, cs-043, cs-044Queue [A, B, C, D, E], k = 2 (last 2 to front). What do k = 0, 5, 7 give?
Expected: 0 unchanged | 2 [D,E,A,B,C] | 5 unchanged | 7 [D,E,A,B,C]
Cost: O(n); the in-place three-reversal rotation (O(1) extra space) is the enrichment extension.
[3, 1, 3, 2, 1, 3] → order-preserving dedupe, and the sorted-unique variant. Reference: [3, 1, 2] and [1, 2, 3].
Expected: [3, 1, 2] [1, 2, 3]
Cost: O(n) with a seen-set; the naive 'not in keep' is O(n^2) - measurable at n = 10,000.
[1, 2, 3, 4, 5]: rotate left by 2 → [3,4,5,1,2]; right by 2 → [4,5,1,2,3].
Expected: [3, 4, 5, 1, 2] | [4, 5, 1, 2, 3]
Cost: O(n); in-place three-reversal is O(n) time, O(1) extra space.
grid = [[0]*3]*2; grid[0][0] = 9. What does grid hold? Then the correct construction - and the same assignment there?
a = [1, 2]
b = a
b.append(3)
print(a, b is a)Expected: [[9, 0, 0], [9, 0, 0]] then [[9, 0, 0], [0, 0, 0]]
Cost: Constant per assignment; the defect class returns as cs-047's swap and cs-027's ghost duplicate.
Move the last k people of a queue to the front, preserving order. Slicing plus concatenation; discuss negative indices.
**Input:** queue [A, B, C, D, E], k = 2 (last 2 to front)**Output:** [D, E, A, B, C]; k = 0 -> same; k = 5 -> same; k = 7 -> same (wrap)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-041 — not embedded in this deck.
Remove duplicates three ways: preserving order with a seen-set, via sorting, and the naive quadratic way. Compare and test all three.
**Input:** [3, 1, 3, 2, 1, 3]**Output:** order-keeping: [3, 1, 2]; sort-based: [1, 2, 3]; naive O(n^2): same as order-keepingSolve 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-042 — not embedded in this deck.
Rotate a list by k positions (left or right) without extra library calls. Handle k larger than the length; connect rotation to slicing.
**Input:** [1, 2, 3, 4, 5], rotate left by 2; rotate right by 2**Output:** left 2: [3, 4, 5, 1, 2]; right 2: [4, 5, 1, 2, 3]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-043 — not embedded in this deck.
A matrix is built as [[0]*3]*2 and rows mutate together. Explain why, then build it correctly. Aliasing as the deepest beginner trap.
**Input:** grid = [[0]*3]*2; grid[0][0] = 9**Output:** both rows become [9, 0, 0] - the two rows are one object; fix: [[0]*3 for _ in range(2)]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-044 — not embedded in this deck.