Define what correct code must do
A correctness contract describes the allowed inputs and the result a function must return for each one. It gives you something to compare with generated code. Without that agreement, an assistant can solve a slightly different problem and still produce a convincing explanation.
Suppose you need to detect repeated IDs in an imported list. The input is a finite list of integers in any order. Return true when two different positions contain the same integer, and false otherwise. These are Boolean values, meaning yes and no. An empty list and a list containing one value have no repeated positions, so both return false.
You can ask an assistant to write the function, but give it the contract before accepting an implementation. The following prompt is a complete starting request. The code and responses in this lesson are teaching examples, rather than a record of a live model session.
Write a JavaScript function hasDuplicate(values).
Input: a finite array of integers, in any order.
Return true exactly when two different positions have equal values.
Return false for empty and one-element arrays.
Do not change the input array.
Explain which earlier values each step compares with.Imagine the assistant returns this implementation. In JavaScript, array positions start at zero. The loop begins at position 1 and compares each value with the value immediately before it. The === operator tests equality, and return ends the function.
function hasDuplicate(values) {
for (let i = 1; i < values.length; i++) {
if (values[i] === values[i - 1]) return true;
}
return false;
}A test calls a function on a chosen input and compares its actual result with the expected result. Derive the expectation from the contract, before reading the generated explanation. For [4, 4], the expected result is true because positions 0 and 1 contain equal values. The proposal returns true too. That example checks adjacent duplicates; it says little about duplicates elsewhere.
Find and explain a failing input
A counterexample is an allowed input on which the actual result differs from the contract. A claim that a function works on every allowed input is false as soon as one counterexample exists. A successful test has a narrower meaning: the function returned the right result for that tested input.
Read the code for the assumption it makes. This implementation remembers only the immediate neighbor. The contract permits equal values anywhere in the list. Put a different value between two equal ones, then work through the comparisons.
Input: [4, 9, 4]
Contract: true, because positions 0 and 2 both contain 4.
i = 1: compare 9 with 4 -> different; continue.
i = 2: compare 4 with 9 -> different; continue.
End of loop: return false.
Mismatch: expected true, received false.A trace records what happens during one execution. This trace explains the failure more precisely than “the code has an edge case.” Position 2 must be compared with position 0, but the loop never makes that comparison. You can now give the assistant a repair request tied to observed behavior.
hasDuplicate([4, 9, 4]) returns false, but the contract requires true.
Your loop compares only neighboring positions.
Repair it so a value can match any earlier position.
Preserve the input and the empty-array behavior.
Explain what information the scan retains and why it is sufficient.Keep the failing input small enough to inspect. Removing the middle 9 produces [4, 4], which this proposal handles correctly. Removing either 4 removes the duplicate. The three-element example isolates the faulty assumption without unrelated data. Reducing a failure this way is called shrinking.
Repair the scan and reuse the method
A set stores distinct values and lets you ask whether a value is already present. Keep a set of values from the part of the array already processed. Before each new value, check that set. If the value is present, an earlier position contains the same value; otherwise add it and continue.
function hasDuplicate(values) {
const seen = new Set();
for (const value of values) {
if (seen.has(value)) return true;
seen.add(value);
}
return false;
}On [4, 9, 4], the set is empty before the first value, then contains 4, then contains 4 and 9. The final 4 is already present, so the function returns true. The statement “the set contains exactly the distinct values already processed” is an invariant, a fact maintained throughout the scan. It explains why separated duplicates are covered. If the scan finishes, each membership check found a new value, so there was no duplicate.
Turn the original failure into a regression test, a saved check that catches the same defect if a later change brings it back. Also retain cases the original handled correctly. These expected results follow from the contract; they are not supplied by the repaired function.
Input Expected Adjacent-only Repaired
[] false false false
[4] false false false
[4, 9] false false false
[4, 4] true true true
[4, 9, 4] true false trueThe set trades memory for retaining history: it can hold as many values as the input contains. Each element is visited once; the cost of membership checks depends on the set implementation. If the task restricts memory or forbids this data structure, include that requirement before asking for a repair.
For larger input ranges, property-based testing generates cases to check a stated rule. For example, compare a duplicate detector against a simple reference that checks every pair of positions. The reference is slower but easy to inspect on small lists. Hypothesis documents generated inputs and shrinking; neither removes the need to choose the right input domain and property.
Transfer the method to a generated sorting function. If the contract says “return all input values in increasing order,” [2, 2] must produce [2, 2]. A proposal returning [2] is ordered but has removed a value. Your next prompt should name that lost repetition and require preserving how many times each value occurs. The relevant question changes from remembering earlier IDs to retaining the input’s contents. In the lab below, use the duplicate contract to construct and explain your own counterexample.
Try the skill
Contract: return true if any two positions contain the same integer. Values may appear in any order. Find an input where this generated JavaScript function returns the wrong answer.
function hasDuplicate(values) {
for (let i = 1; i < values.length; i++) {
if (values[i] === values[i - 1]) return true;
}
return false;
}