# The Pipeline Filter

> In the door as one thing, out the door as another.

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

Domain: Python · Difficulty: easy · Seniority: L4

## Problem

Given a list of integers, using map() and filter() (no list comprehensions or explicit for-loops), return a list containing only the even numbers each doubled.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests knowledge of Python's **functional programming built-ins**: `map()` and `filter()`. Interviewers restrict list comprehensions to check whether candidates know the function-based alternatives that mirror functional pipelines.

---

### Break down the requirements

#### Step 1: Filter for even numbers

Use `filter()` with a predicate that checks `x % 2 == 0`.

#### Step 2: Double each even number

Use `map()` with a function that multiplies by 2.

#### Step 3: Convert to a list and return

Both `map()` and `filter()` return lazy iterators in Python 3, so wrap the result in `list()`.

---

### The solution

**Chained filter and map with lambdas**

```python
def pipeline_filter(nums):
    evens = filter(lambda x: x % 2 == 0, nums)
    doubled = map(lambda x: x * 2, evens)
    result = list(doubled)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) with a single lazy pass through the list.
> 
> **Space:** O(k) where k is the number of even elements in the output.

> **Interviewers Watch For**
>
> Do you chain `filter` before `map` (filter first, then transform) rather than the reverse? Filtering first reduces the number of elements that `map` processes.

> **Common Pitfall**
>
> Forgetting to wrap in `list()`. In Python 3, `map()` and `filter()` return iterators, not lists. Returning the iterator directly would fail equality checks in tests.

---

## Common follow-up questions

- What is the equivalent list comprehension? _(Tests `[x * 2 for x in nums if x % 2 == 0]` and when each style is preferred.)_
- How does filter/map relate to Spark's transformation API? _(Tests awareness that Spark RDDs use the same filter/map/reduce pattern.)_
- What if you needed to apply multiple transformations in sequence? _(Tests chaining multiple map calls or using functools.reduce.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_pipeline_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.