Back to videos
Correct Code Video
Case Files Core Skill

Case File 002: Two Sum

Separate scanner movement, recorder state, and the answer contract.

Lesson Briefing

What this video teaches

This video walks through Case File 002: Two Sum, the first Correct Code Framework investigation where a Recorder becomes necessary. The problem asks the learner to find two different numbers in an unsorted array that add up to a target and return their indices.

The lesson begins by treating the problem statement as evidence. The input is an array of integers and a target. Each number becomes a candidate, and for every candidate there is a missing partner called the complement. The central invariant is candidate + complement == target. The investigation is not complete until the learner can prove that a candidate and its complement form the lawful answer pair.

Unlike Case File 001, this problem cannot be solved by direct scan alone. A sequential Scanner can move through the array, but the missing complement may already be behind the current candidate. That creates the need for a Recorder. The Recorder stores prior values with their indices so the current candidate can ask whether its complement has already been seen.

The video emphasizes the correct order of operations: calculate the complement, check the Recorder first, return the pair if the complement exists, and only then record the current candidate. This prevents using the same element twice and preserves the answer contract.

The walkthrough shows how proof comes before transcription. Once the learner proves the Scanner movement, Recorder state, complement lookup, and return condition, the code becomes a direct translation of the investigation. The final CCF takeaway is that the correct tool is not chosen by name; it is chosen because it satisfies the problem’s constraints.