CODING CHALLENGE · N°67

Spot the Bug in AI Code

Easy AI EngineeringCode ReviewDebugging

An assistant wrote this history-trimmer to fit a chat inside the context window. It looks right, the types check, it passes the obvious case — and it silently drops the system prompt the moment the conversation gets long. Reviewing AI code means catching exactly this: confident, plausible, and wrong on the case that matters. Find the bug and fix it.

The problem

The starter code is an AI-written clip_history(history, max_turns). history[0] is the system message and must always survive; the rest are conversation turns, of which only the most recent max_turns should be kept. The current code returns history[-max_turns:] — which counts the system message as a turn and drops it entirely once the history is longer than the budget. Fix it so the system message is always first, followed by at most the last max_turns non-system messages. Do not mutate the input.

EXAMPLE 1
Input history=[sys, u1, u2], max_turns=5
Output [sys, u1, u2]
short history fits under the budget — unchanged
EXAMPLE 2
Input history=[sys, u1, u2, u3, u4, u5], max_turns=2
Output [sys, u4, u5]
the buggy version returns [u4, u5] — the system prompt is gone
EXAMPLE 3
Input history=[sys, u1, u2], max_turns=0
Output [sys]
no turns budgeted — only the system message remains
CONSTRAINTS
  • The system message (history[0]) is always the first element of the result, regardless of max_turns.
  • Keep at most the last max_turns of the non-system messages (history[1:]); max_turns=0 keeps none.
  • If the whole history already fits (max_turns >= len(history) - 1), return it unchanged.
  • Do not mutate the input history — build and return a fresh list.
SOLVE IT YOURSELF

Your turn — write it

Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.

YOUR TASK

The AI-written clip_history below drops the system prompt on long conversations. Find the one-line bug and fix it so the system message always survives and only the last max_turns turns are kept.

HINTS — 4 IDEAS
  1. Run it first: try max_turns=2 on a history of 6 — watch the system message vanish. That is the bug.
  2. The system message must not be counted against the turn budget. Separate it out: system = history[0], rest = history[1:].
  3. Slice the REST, not the whole history: rest[-max_turns:] keeps the last max_turns turns.
  4. Guard max_turns == 0 — an empty slice, so the result is just [system].
CPython · WebAssembly

Explore the topic

See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.