# The Elevator Trace

> Nested floors. One path through.

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given a list of lists (floors), concatenate all inner lists left-to-right to form the forward path; then append the reverse of that same concatenation. Return the full traversal list.

## Worked solution and explanation

### Why this problem exists in real interviews

Nested-list flattening shows up in pipeline glue code: paginated API results, batched event windows, multi-floor sensor traces. Interviewers like this prompt because the surface task is trivial, but the spec hides a small twist (append the reverse) that exposes whether candidates read carefully or pattern-match to itertools.chain and stop.

---

### Break down the requirements

#### Step 1: Flatten left to right, preserving order

Concatenate inner lists in input order. A list comprehension with a nested for is the cleanest expression and is faster than repeated list.extend in a Python loop because the interpreter allocates the result once.

#### Step 2: Append the reverse without mutating

After building the forward path, append its reverse. Use slicing (forward[::-1]) so the forward list itself stays untouched in case the caller still holds a reference, and so the operation reads as one expression.

#### Step 3: Handle empty inputs cleanly

Empty outer list and lists of empty inner lists both collapse to an empty result with no special-case branches. Always returning a list (never None) keeps the caller's downstream loop safe.

---

### The solution

**Flatten then mirror**

```python
def elevator_path(floors: list[list[int]]) -> list[int]:
    forward = [x for floor in floors for x in floor]
    return forward + forward[::-1]
```

> **Cost Analysis**
>
> Time is O(n) where n is the total number of elements across all inner lists. Space is O(n) for the forward path plus O(n) for its reverse, so 2n peak. The list comprehension and the slice are both implemented in C, so the constant factor beats a manual for-loop with extend.

> **Interviewers Watch For**
>
> Whether you reach for itertools.chain.from_iterable and explain the readability tradeoff, whether you remember to return a list (not a generator) since the spec says 'return the full traversal list,' and whether you avoid mutating the inputs. Strong candidates name the asymptotic cost without prompting.

> **Common Pitfall**
>
> Reversing each inner list before concatenating, which mirrors at the wrong granularity and gives floor-level instead of trip-level reversal. Another classic is forward.reverse() which mutates in place and returns None, so forward + forward.reverse() raises a TypeError. Slice-reverse with [::-1] sidesteps both.

---

## Common follow-up questions

- How would the solution change if floors could be arbitrarily nested (lists of lists of lists)? _(discuss recursion or an explicit stack; mention sys.getrecursionlimit for deep inputs.)_
- What if the elevator should pause (insert a sentinel) between floors? _(show how to interleave a separator while keeping the flatten-then-mirror shape.)_
- How would you stream this for a memory-constrained worker where the full path does not fit in RAM? _(talk about generators, two passes (forward then reverse from disk), and why the contract here forces materialization.)_

## Related

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