ChatGPT coding interview practice: explain the invariant before asking for the answer

Ask ChatGPT to play the interviewer, give one hint at a time and wait while you explain your algorithm. For a coding exercise, rehearse the input contract, a small trace and why the loop remains correct. Reading an optimized answer first makes it difficult to tell which reasoning you can reproduce yourself.

Use this for permitted preparation. Follow the assessment's rules during an actual interview or take-home exercise.

Clarify a two-sum contract

This original practice exercise asks for indices of two distinct entries whose integer values add to a target. Return any valid pair, with the earlier index first, or None if no pair exists. Inputs are a finite list of integers and an integer target. The input must remain unchanged.

For [4, 4] and target 8, the result is (0, 1). Equal values are allowed because they occupy different positions. For [4] and target 8, the result is None: one entry cannot serve twice.

Before coding, describe the simple baseline: inspect every pair with the second index greater than the first. That gives a direct correctness argument and O(n²) time. Then ask what information would let you avoid repeating the inner search.

State what the map contains

Before processing index j, the dictionary contains values from indices strictly before j, with one earlier index for each value. Look for target - value in that dictionary. A match therefore refers to a different, earlier entry. Only after that check do you store the current entry.

An authored Python implementation is:

def two_sum(values, target):
    seen = {}
    for j, value in enumerate(values):
        needed = target - value
        if needed in seen:
            return seen[needed], j
        if value not in seen:
            seen[value] = j
    return None

The Python dictionary reference documents key membership and mapping operations. This algorithm uses those operations to remember an earlier index. Its usual expected O(n) time analysis assumes average constant-time dictionary operations; it uses O(n) additional space in the worst case.

Inserting before looking can violate the invariant. On the one-element list [4], it could find the just-inserted 4 and incorrectly return the same index twice. A duplicate-value example checks that subtle ordering decision.

Trace before running

Input and target Expected result What it checks
[4, 4], 8 (0, 1) Two equal values at different indices
[4], 8 None No self-reuse
[5, -2, 7], 3 (0, 1) Negative values
[], 3 None Empty input

For the first row, j=0 finds no earlier 4 and stores index 0. At j=1, the needed 4 is already mapped to 0, so the function returns (0,1). Explain that trace aloud before you execute it.

Copy an interviewer-mode prompt

Help me prepare for an algorithm interview using the two-sum problem.
Language: Python. Contract: finite integer list and integer target;
return any two distinct indices, earlier first, or None. Preserve input.
Ask me to restate the contract and give a baseline approach first.
Wait for my answer. Then ask for time/space costs and one improvement.
Give only one hint when I request it; do not reveal a full solution.
Ask what is true of the map before each iteration and challenge me
with [4,4] target8 and [4] target8. Wait for my trace before feedback.
After I submit code, use its exact lines to explain any defect.
Separate predicted results from execution. Do not invent employer
questions, scoring rules, interview outcomes or a successful run.

OpenAI's prompting documentation supports defining the task and turn-taking constraints. If the chat reveals the solution early, start a fresh variant and ask it to wait after the first question.

Reimplement the function from the invariant without looking at the sample. Run the agreed examples and check that the input list stays unchanged. The unit-test guide covers preserving that contract in assertions. Finish with a reason you can explain for the lookup order, rather than a code pattern you can only recall visually.

Keep reading