# The Slow Leak

> Nested iterators. One flat stream.

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given a possibly-nested list, return a single flat list of all leaf values in order. Nesting is arbitrarily deep. Strings are leaves, not iterated character-by-character.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **recursive data traversal** and understanding of **iterator protocol**. Flattening nested structures is a common ETL pattern, and it probes whether a candidate can distinguish leaf values from nested containers.

---

### Break down the requirements

#### Step 1: Determine if an element is iterable or a leaf value

Check if an element has an `__iter__` method (but exclude strings, which are iterable but should be treated as leaves).

#### Step 2: Recursively flatten nested iterators

If an element is iterable, recurse into it. Otherwise, append it to the result list.

#### Step 3: Return the flat list preserving order

Depth-first traversal naturally preserves the left-to-right order of leaf values.

---

### The solution

**Recursive flattening with type guard**

```python
def flatten_iter(it) -> list:
    result = []
    for item in it:
        if hasattr(item, '__iter__') and not isinstance(item, str):
            nested = flatten_iter(item)
            for val in nested:
                result.append(val)
        else:
            result.append(item)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the total number of leaf elements. Each leaf is visited exactly once.
> 
> **Space:** O(d + n) where d is the maximum nesting depth (recursion stack) and n is the result list size.

> **Interviewers Watch For**
>
> The string guard is critical. Strings are iterable in Python, so without `not isinstance(item, str)`, the function would recurse into each character infinitely.

> **Common Pitfall**
>
> Using `isinstance(item, list)` instead of checking `__iter__`. This misses tuples, generators, and other iterable types that the problem says can appear.

---

## Common follow-up questions

- How would you implement this iteratively to avoid stack overflow? _(Tests using an explicit stack for deep nesting.)_
- What if you wanted a lazy generator instead of a list? _(Tests yielding values with `yield` and `yield from`.)_
- What if some nested iterators are infinite? _(Tests awareness that eagerly collecting from infinite iterators will hang, requiring lazy evaluation.)_

## Related

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