The Elevator Trace

A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
medium
Seniority
L3
Asked in
Wealthfront

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.

Nested floors. One path through.

Example

Input:

floors = [[1,2,3],[4,5,6],[7,8,9]]

Output:

[1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1]

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
def elevator_path(floors: list[list[int]]) -> list[int]:
    forward = [x for floor in floors for x in floor]
    return forward + forward[::-1]

Common follow-up questions