# Into One Stream

> Some arrive alone, some in batches. Line them all up.

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

Domain: Python · Difficulty: easy · Seniority: L5

## Problem

A log collector hands you a buffer where each slot is either a single event id or a batch of ids grouped in a nested list. Return every id from `items` in one flat list, keeping their left-to-right order.

## Worked solution and explanation

### What this really is

This is type-driven dispatch wearing a data-cleaning costume. Each slot is either a scalar id or a batch of ids, and the whole game is deciding, per element, whether to unpack it or drop it in as-is. Anyone can write the loop. The tell is how you merge a batch: most candidates reach for `sum(items, [])` or a comprehension that assumes every element is a list, and it detonates the moment it meets a bare integer, because you cannot concatenate an int. Get the branch wrong and you either raise TypeError on the first scalar or silently lose the singletons.

---

### Break down the requirements

#### Step 1: Iterate through the top-level list

Walk the top-level list once. Every element is either an integer or a one-level nested list of integers, so a single linear pass is enough.

#### Step 2: Extend for lists, append for scalars

If the element is a list, spread its items into the result. Otherwise the element is a scalar, so drop it in directly. That two-way branch is the entire problem.

---

### The solution

**Single-level flatten with type branching**

```python
def flatten_the_nest(items):
    result = []
    for item in items:
        if isinstance(item, list):
            result.extend(item)
        else:
            result.append(item)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the total number of ids after flattening. Each element is touched once. **Space:** O(n) for the result list.

> **Interviewers Watch For**
>
> Prefer `isinstance(item, list)` over `type(item) == list`. The isinstance form accepts list subclasses and reads as the Pythonic intent: 'is this a list-like batch?'

> **Common Pitfall**
>
> The one-shot flatten. `sum(items, [])` and `[x for sub in items for x in sub]` both assume every element is already a list, so the first bare integer raises TypeError. The branch is not optional here: the input is genuinely mixed, and only per-element inspection survives it.

---

## Common follow-up questions

- What if a batch could arrive as a tuple as well as a list? _(Tests broadening the type check to accept more than one container type.)_
- How would you handle None values mixed into the buffer? _(Tests whether None should be included as a leaf or filtered out.)_
- What changes if batches could themselves contain batches, to arbitrary depth? _(Tests generalizing the single branch into recursion or a depth parameter.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/into_one_stream)
- [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). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.