AnnotationPrep

Python Bug Hunting practice questions

10 questions from the Coding assessment: debugging & verification practice test.

Start practising

Python Bug Hunting practice drill

10 scenarios, graded as you go, with the exact grading principle for every answer. Start free, no sign-up.

Sample scenarios

An LLM writes a Python helper to accumulate items per user:

```python
def add_tag(tag, tags=[]):
tags.append(tag)
return tags
```

Why must you flag this code in a coding RLHF evaluation?

  1. `list.append()` returns `None` instead of the list.
  2. Mutable default argument bug: the same `tags` list is shared across all calls that omit `tags`.
  3. Python does not allow two parameters with similar names.
  4. `tags` must be a tuple to use `.append()`.
Show the answer

B. Mutable default argument bug: the same `tags` list is shared across all calls that omit `tags`.

Default parameter values in Python are evaluated once at function definition time. Using `tags=[]` causes state to leak between independent calls. The golden fix uses `tags=None` and initializes `if tags is None: tags = []`.

Principle: Python Best Practices: Mutable Default Arguments

An LLM attempts to remove all even numbers from a list in-place:

```python
nums = [2, 4, 5, 6, 8, 9]
for n in nums:
if n % 2 == 0:
nums.remove(n)
print(nums)
```

What does this print, and what is the bug?

  1. Prints `[5, 9]`; no bug.
  2. Prints `[4, 5, 8, 9]`; mutating a list while iterating over it skips elements as indices shift.
  3. Raises `RuntimeError: list changed size during iteration`.
  4. Prints `[2, 4, 6, 8]`; `%` checks for odd numbers.
Show the answer

B. Prints `[4, 5, 8, 9]`; mutating a list while iterating over it skips elements as indices shift.

Unlike dictionaries/sets, mutating a Python `list` during a `for` loop does not raise `RuntimeError`—instead, removing `2` shifts `4` to index 0, so the iterator advances to index 1 (`5`) and skips `4` (and later skips `8`). Use a list comprehension: `[n for n in nums if n % 2 != 0]`.

Principle: Python Iteration: Mutating Lists In-Place

An LLM writes a binary search function:

```python
def binary_search(arr, target):
lo, hi = 0, len(arr) - 1
while lo < hi:
mid = (lo + hi) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
```

On which valid input does this fail?

  1. Empty array `[]`.
  2. A 1-element array `[7]` when `target = 7` (returns `-1` because `lo == hi` exits `while lo < hi`).
  3. Any array with negative numbers.
  4. It enters an infinite loop on `[1, 2, 3]`.
Show the answer

B. A 1-element array `[7]` when `target = 7` (returns `-1` because `lo == hi` exits `while lo < hi`).

With `lo, hi = 0, 0`, `while lo < hi` is immediately False, so `binary_search([7], 7)` returns `-1`. Classic off-by-one bug: the condition must be `while lo <= hi:`.

Principle: Algorithms: Binary Search Boundary Conditions

Other topics