Why must you flag this code in a coding RLHF evaluation?
`list.append()` returns `None` instead of the list.
Mutable default argument bug: the same `tags` list is shared across all calls that omit `tags`.
Python does not allow two parameters with similar names.
`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?
Prints `[5, 9]`; no bug.
Prints `[4, 5, 8, 9]`; mutating a list while iterating over it skips elements as indices shift.
Raises `RuntimeError: list changed size during iteration`.
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]`.
```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?
Empty array `[]`.
A 1-element array `[7]` when `target = 7` (returns `-1` because `lo == hi` exits `while lo < hi`).
Any array with negative numbers.
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:`.