What must memory answer?
Unsorted [8,2,6,9,5], target 7.
At the final 5, which question about earlier input finds the answer?
Presenter notes
Has 2 appeared, and at which original index? It appeared at index 1; pair [1,4] is valid.
1 / 11
Choose the stored value
A set answers existence.
A value→index map supplies a witness.
Why must lookup precede insertion on [4,4], target 8?
Presenter notes
The first 4 has no earlier partner. Inserting it first could pair it with itself; the second 4 legitimately finds index 0. Resource counting and grouping need different meanings of stored information.
2 / 11
Ransom Note — predict before coding
Availability is a multiset budget. Seeing a letter once is not enough when the note demands several copies.
Decision: Can note "aa" be constructed from magazine "ab"?
Presenter notes
Model answer: No. The first a consumes the only available a; the second request sees a zero remaining budget.
Correctness argument: After counting the magazine, each coordinate is the number of unused copies of one lowercase letter. For each next note byte, a zero budget proves that no unused magazine occurrence can supply it, so failure is necessary. Otherwise decrementing consumes one available copy and preserves the remaining-budget interpretation. The note order imposes no restriction on which magazine occurrence is used, so satisfying every byte is sufficient. After the entire note is processed, an explicit allocation of distinct magazine occurrences exists for all requested letters. Unused magazine copies do not affect validity.
Debugging prompt: Test an empty note, abundant irrelevant letters, and exactly one missing repeated letter. This is containment of counts, not equality of the two multisets.
3 / 11
Isomorphic Strings — predict before coding
A consistent forward mapping is not enough: two different source bytes must not collapse to one target byte. The reverse mapping enforces injectivity.
Decision: Why is s="ab", t="aa" false even though a forward map can store a→a and b→a?
Presenter notes
Model answer: The mapping is not one-to-one: two source bytes would share the same target a. The reverse map detects this collision.
Correctness argument: For every processed position, the forward and reverse maps agree with that byte pair. If neither byte has a mapping, adding both directions extends a one-to-one correspondence. If either direction already disagrees, no single bijection can explain both the earlier pair and this one. Otherwise the pair is consistent and leaves the invariant unchanged. At the end, the maps witness a replacement of every source byte producing the target, and the reverse map proves no two sources share a target. Conversely, any valid bijection would make every checked pair agree, so rejection cannot discard a valid isomorphism.
Debugging prompt: Use repeated patterns such as egg/add and ab/aa. Sentinel -1 distinguishes an unmapped byte from byte value zero; a zero-filled table without a presence marker is unsafe.
4 / 11
Word Pattern — predict before coding
This is a bijection between pattern bytes and whole tokens. Tokenization is part of the contract, not a harmless implementation detail.
Decision: For pattern "abba", why does "dog dog dog dog" fail?
Presenter notes
Model answer: Pattern a and pattern b would both map to dog. Consistent repetition in the forward map does not remove the reverse-map collision.
Correctness argument: First require one token for every pattern position. During the scan, the character-to-word map prevents a repeated pattern byte from changing its associated word, while the word-to-character map prevents different pattern bytes from sharing a word. A new pair is added only when both existing constraints are satisfied. Induction gives a consistent bijection for all processed positions. After the final position, that bijection explains the entire token sequence, so acceptance is sufficient. Any conflicting pair would violate the definition of a single one-to-one pattern mapping, so rejection is necessary.
Debugging prompt: Test word-count mismatch and reverse-map collisions independently. The existing implementation uses strings.Split(s," "), not strings.Fields; do not silently change whitespace or empty-token behavior.
5 / 11
Valid Anagram — predict before coding
Anagrams are exactly equality of frequency vectors. The lowercase alphabet allows a fixed-size difference vector instead of sorting or a general hash map.
Decision: Are "aabb" and "abbb" anagrams, and which counts disprove it?
Presenter notes
Model answer: No. The first has two a and two b; the second has one a and three b. The count differences are +1 for a and -1 for b.
Correctness argument: After processing equal-length prefixes of s and t, each coordinate holds the number of that letter in the source prefix minus its number in the target prefix. Adding the next source letter and subtracting the next target letter preserves this invariant. At termination, all coordinates are zero exactly when every letter has equal multiplicity in the complete strings. Equal multisets can be rearranged into one another, while any nonzero coordinate proves a surplus or deficit that no rearrangement can fix. An initial length mismatch immediately rules out equal multisets.
Debugging prompt: Test empty strings, equal lengths with unequal counts, and reordered repeated letters. Merely checking that both strings contain the same distinct letters is not sufficient.
6 / 11
Group Anagrams — predict before coding
A key should represent the equivalence class, not an accidental property such as total letter sum. Correct canonicalization makes grouping straightforward.
Decision: Why must the two empty strings in ["", "", "a"] remain two entries in one group?
Presenter notes
Model answer: The input is a list of occurrences, not a set. Both empty-string occurrences share the all-zero key and both must be retained.
Correctness argument: The 26 counts uniquely describe a lowercase string's multiset. The reference uses the complete [26]int array as its key, so equality checks every coordinate; it does not compress counts into a checksum or ambiguous string. Two strings have equal keys exactly when every count agrees, which is exactly anagram equivalence. Appending each input occurrence to its key's group therefore places all and only mutual anagrams together. Every occurrence is appended once, including duplicate and empty strings, so the output preserves input multiplicity. Iterating the group map may reorder groups but cannot change their membership.
Debugging prompt: Test repeated inputs, empty strings, and keys whose digit counts could collide without separators. Normalize group order for comparison; do not drop duplicate strings.
7 / 11
Two Sum — predict before coding
The map is a history of earlier positions. Looking up the complement before inserting the current value prevents one occurrence from pairing with itself.
Decision: Why does [3,3], target 6 require lookup before insertion?
Presenter notes
Model answer: At index 0 no partner exists yet. After storing its 3, index 1 finds that earlier occurrence and returns [0,1]. Inserting first can incorrectly pair index 0 with itself.
Correctness argument: Before index i, seen contains a usable earlier index for every distinct value in nums[:i]. If target-nums[i] is present, that earlier index and i are distinct and their values sum to target, so the returned pair is valid. If absent, no earlier element can complete a pair ending at i. Inserting the current value then establishes the invariant for the next position. Any promised solution has a later endpoint, and its earlier value is present when that endpoint is processed, so the scan must find it. Replacing an earlier stored index for the same value preserves the existence of a usable distinct earlier partner.
Debugging prompt: Check index distinctness, bounds, and sums. This version returns zero-based original indices and does not sort or mutate nums; TwoSumII has a different index convention.
8 / 11
Happy Number — predict before coding
Repeated digit-squaring forms a deterministic functional graph. A visited set or two-speed traversal detects whether its path reaches 1 or enters another cycle.
Decision: What are the first successors of 19 until 1 is reached?
Presenter notes
Model answer: 19→82→68→100→1. Each transition squares and sums digits, rather than squaring the entire number.
Correctness argument: Every state has exactly one successor: the sum of its squared decimal digits. For the stated input bound, the first successor is at most 10*81=810, and subsequent states remain in a finite range. Thus the sequence must eventually repeat or reach 1. The slow pointer advances once and the fast pointer twice; once both are in a cycle, their relative position advances by one modulo the cycle length, so they eventually meet. If fast reaches 1, repeated successors stay 1 and the number is happy. If the pointers meet away from 1, determinism traps the sequence in that non-1 cycle, proving unhappiness.
Debugging prompt: Use 1, a known non-1 cycle such as 2, and a large input. If using a set instead, record the whole integer state, not the multiset of its digits.
9 / 11
Longest Consecutive Sequence — predict before coding
A set removes duplicates, but linear time also requires iterating distinct starts. Walking from every copy of the same minimum value repeats a whole run.
Decision: For [1,1,1,2,3,4], how many times should the length-four run be expanded?
Presenter notes
Model answer: Once. The duplicate 1 occurrences must not cause three separate scans of 1,2,3,4.
Correctness argument: Partition the distinct input values into maximal consecutive runs. A value is a run start exactly when its predecessor is absent. Starting a forward walk only at such a value visits all and only the members of its run, so its measured length is correct. Distinct runs are disjoint and every run has exactly one start. Iterating set keys, rather than the original duplicate-containing slice, ensures each run is walked only once. The largest measured run is therefore the requested answer, with at most a constant number of expected-time set probes per distinct value plus initial input insertion.
Debugging prompt: An adversarial input contains many copies of a run minimum followed by one long consecutive run. Random small arrays rarely reveal this complexity bug. A sorted-distinct oracle provides an independent correctness check.
10 / 11
Reconstruct and transfer
Compare containment with equality.
Explain a reverse-map collision.
Prove linear run expansion by charging each distinct value once.
Presenter notes
Discuss the limits of transferring a pattern. Use the module lesson and problem statements for complete traces, contracts, and model answers.
11 / 11