# Flatten the Feed

> Nested lists, all the way down.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list nested to arbitrary depth, return a single flat list of all leaf values in left-to-right order. Strings are treated as leaves.

## Worked solution and explanation

### Why this problem exists in real interviews

Recursively flattening arbitrarily nested lists is a classic **recursion** problem. It tests type checking, base case identification, and whether you can build a clean recursive solution that handles any depth.

---

### Break down the requirements

#### Step 1: Check each element's type

Use `isinstance(item, list)` to distinguish nested lists from leaf elements.

#### Step 2: Recurse into lists, collect leaves

For list elements, recursively flatten. For non-list elements, add to the result.

#### Step 3: Preserve left-to-right order

Elements should appear in the same order as a depth-first traversal of the nested structure.

---

### The solution

**Recursive depth-first flattening**

```python
def flatten(nested):
    result = []
    for item in nested:
        if isinstance(item, list):
            flattened = flatten(item)
            for val in flattened:
                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 once.
> 
> **Space:** O(n + d) where n is the output size and d is the max nesting depth for the recursion stack.

> **Interviewers Watch For**
>
> Whether you handle the empty list case naturally (the loop simply does not execute) and whether you mention the recursion depth limit for extreme nesting.

> **Common Pitfall**
>
> Treating strings as lists. A string is iterable in Python and would be recursed into character by character. Always check `isinstance(item, list)` specifically.

---

## Common follow-up questions

- How would you flatten iteratively using a stack? _(Tests replacing recursion with an explicit stack for O(1) call-stack usage.)_
- What if the input can contain dicts or tuples? _(Tests broadening the type check or using a more general iterable detection.)_
- What if you needed to preserve nesting depth as metadata? _(Tests returning `(value, depth)` pairs instead of raw values.)_

## Related

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