# Even Filter

> Only the even ones survive.

Canonical URL: <https://datadriven.io/problems/even_filter>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list of integers, return a new list containing only the even numbers in their original order.

## Worked solution and explanation

### Why this problem exists in real interviews

Filter-a-list-by-predicate is the warm-up interviewers open with to check Python fluency before the harder problem. The right answer is one expression; the wrong answer is a loop with an accumulator list and an if. What they actually grade is whether you reach for a list comprehension or filter(), whether you preserve input order, and whether you avoid mutating the input.

---

### Break down the requirements

#### Step 1: Use a list comprehension with a predicate

[x for x in nums if x % 2 == 0] is the Pythonic one-liner. It preserves input order by construction (iteration is left to right), allocates the result once, and runs the predicate in the interpreter loop without the overhead of a named function.

#### Step 2: Use the modulo test, not bitwise tricks

x % 2 == 0 reads as English and handles negative integers and zero correctly (0 % 2 == 0, -2 % 2 == 0 in Python). Bitwise x & 1 == 0 also works but sacrifices readability for zero speedup on interview-sized inputs.

#### Step 3: Return a new list, never mutate the input

The spec says 'return a new list,' so produce a fresh object. A list comprehension already does this. Using nums.remove(x) in a loop would mutate and also be O(n^2) due to repeated shifts; both are wrong.

---

### The solution

**One-line comprehension**

```python
def even_filter(nums: list) -> list:
    return [x for x in nums if x % 2 == 0]
```

> **Cost Analysis**
>
> Time is O(n) with one pass over nums. Space is O(k) where k is the number of even numbers in the output. List comprehensions are implemented in C-level bytecode and beat an equivalent for-loop with .append() because the interpreter preallocates capacity and skips method-lookup overhead per iteration.

> **Interviewers Watch For**
>
> Whether you write the comprehension without hesitating, whether you handle negatives and zero correctly (some candidates write x > 0 and x % 2 == 0 by accident), and whether you return a list (not a filter object). Strong candidates name O(n) time and O(k) space without prompting and mention that filter(lambda x: x%2==0, nums) also works but returns an iterator in Python 3.

> **Common Pitfall**
>
> Writing filter(...) and forgetting to wrap it in list(). In Python 3, filter returns a lazy iterator, so the return value is truthy but iterating it once exhausts it, and equality checks against a list expected value fail. Another miss is using nums.copy() then calling nums.remove(x) for each odd, which mutates a copy but runs O(n^2).

---

## Common follow-up questions

- How would you split nums into (evens, odds) in a single pass? _(one loop with two appends, or two comprehensions if readability matters more than a single scan; discuss the tradeoff.)_
- What changes if nums is a generator that cannot be iterated twice? _(a single comprehension consumes the generator correctly; calling len() on it first would exhaust it. Discuss defensive list() if needed.)_
- How would you parallelize this for a huge array on a multicore machine? _(numpy vectorization (nums[nums % 2 == 0]) or multiprocessing.Pool with chunks; note that the GIL blocks thread-level parallelism for pure-Python predicates.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/even_filter)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.