Before you solve the case
The first controlled crime scene
Before you write code, learn how to read the witness statement. This opening page tells you what the investigation is asking you to prove.
Case File #001 exists first because every later case depends on this skill: reading the problem statement before choosing a route.
Find the Target Element is intentionally simple. The simplicity lets the learner see the full Correct Code Framework investigation without being distracted by advanced data structures.
How to Analyze a Problem Statement
The Correct Code Framework treats the problem statement as an evidence locker. Read past the narrative first. The structural clues determine which reasoning worlds are lawful.
1. Extract the contract: Identify allowed inputs, the required output, and every time, space, size, or behavior limit.
2. Separate narrative from structure: Convert story words into data structures, data types, collection state, validation keys, boundaries, and guarantees.
3. Test each clue as a legal boundary: A guarantee opens lawful movement; a missing guarantee eliminates routes that depend on it. Unsorted evidence cannot authorize directional elimination.
4. Name the surviving environment before code: Define the Answer Space, map the Problem Space, then derive the Decision Space.
Translate the contract into lawful movement
Formal Translation
Iterate through numbers from left to right. For each index i,
test whether numbers[i] == target. Return i on the first match.
Return -1 only after all candidates have been inspected.
CCF Translation
The Answer Space requires one scalar integer. The Problem Space is an unsorted indexed array and a stable validation key. The Invariant is equality. The lawful Operation is Direct Scan. The Scanner advances one index at a time. No external Recorder is required.
Why no Recorder is lawful
Recorder Decision: Not required. The active candidate can be tested directly against the target. No future candidate needs evidence from a previous Frame, and no complement, frequency, or historical relationship must persist.
Formal Translation
Equality can be evaluated from numbers[i] and target alone, so the scan requires constant auxiliary state.
CCF Translation
The Recorder is lawful only when previous evidence is required to validate the active candidate. Here, each suspect can testify for itself: candidate == target. Nothing from an earlier Frame is needed.
Candidate Qualification
A candidate qualifies only while it is the active element under inspection and its value is compared directly with the target. No other element can prove the current candidate’s match.
Evidence Commitment
Commit the current index only when candidate == target. Commit -1 only after the Scanner exhausts every candidate.
Elimination Map
| Route | Ruling | Why |
|---|---|---|
| Nested loops | Eliminated | The Answer Space asks about one candidate, not a relationship between candidates. |
| HashMap Recorder | Eliminated | No earlier evidence is needed to qualify the current candidate. |
| Sort first | Legal but unnecessary | Sorting changes the environment and costs work without improving direct equality evidence. |
| Binary search | Not yet lawful | The witness statement provides no sorted guarantee. |
| Linear Scanner | Survives | Every unsorted candidate must receive one direct equality test. |
Wrong Path Analysis
A wrong path is not always incorrect. Sorting before searching can still produce an answer, but it adds work and may disturb positional evidence. CCF selects the best lawful route supported by the current evidence.
Transfer Question
Change one clue
What changes if the collection is sorted? What new Recorder becomes lawful if the Answer Space asks how many times the target appears?
Target Element Simulator
Watch the Scanner evaluate one active candidate at a time. The first candidate that satisfies
candidate == target closes the case.
Correctness Ruling
The Scanner inspects each candidate once in order. When candidate == target, the active candidate proves the answer and the first matching index is committed immediately. If the Scanner reaches the end without a match, every candidate has testified and no unchecked evidence remains. Returning -1 is lawful only after Scope Exhaustion.
Common Mistakes
- Opening a Recorder when no prior evidence is required.
- Sorting when the case asks for a direct scan of the given collection.
- Using binary search on unsorted evidence.
- Continuing after the first match when the first index is required.
- Returning the matching value instead of its index.
- Claiming “not found” before Scope Exhaustion.
✓ Case Closed
You solved Case File #001 by reasoning, not guessing.
You read the problem statement like evidence, separated the contract from the story, and derived the legal route before touching the implementation.
- Answer Space — return the first matching index, or
-1. - Problem Space — unsorted integer array
numbersand integertarget. - Invariant —
candidate == target. - Operation — Direct Scan with an equality check.
- Scanner — linear forward scan, one index at a time.
- Output — return the index on first match; return
-1after scope exhaustion.
The Scanner followed the evidence. It was not chosen by preference.