# Full Outer Zip

> Two sides. No value left behind.

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

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given two lists a and b (possibly of different lengths), return a list of 2-element lists [a_i, b_i] pairing by index. When one list is shorter, pad its missing positions with None so the output length equals max(len(a), len(b)).

## Worked solution and explanation

### Why this problem exists in real interviews

Zipping two lists of unequal length with None fill tests your understanding of **iteration beyond the shorter list**. It mirrors a full outer join in SQL and reveals whether you handle boundary conditions cleanly.

---

### Break down the requirements

#### Step 1: Determine the longer length

Use the max of both list lengths to know how many pairs to produce.

#### Step 2: Pair elements by index, filling with None

For each index, take the element from each list if the index is in range, otherwise use None.

---

### The solution

**Index-based pairing with None fill for shorter list**

```python
def zip_longest(a, b):
    result = []
    longer = len(a) if len(a) > len(b) else len(b)
    for i in range(longer):
        val_a = a[i] if i < len(a) else None
        val_b = b[i] if i < len(b) else None
        result.append([val_a, val_b])
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(max(n, m)) where n and m are the lengths of the two lists.
> 
> **Space:** O(max(n, m)) for the result list of pairs.

> **Interviewers Watch For**
>
> Whether you know about `itertools.zip_longest`. Mentioning it shows stdlib awareness, but writing the loop manually demonstrates you understand the mechanics.

> **Common Pitfall**
>
> Using Python's built-in `zip()`, which truncates to the shorter list. This silently drops elements from the longer list.

---

## Common follow-up questions

- How would you generalize this to zip N lists? _(Tests accepting a variable number of lists with `*args` and filling all shorter ones with None.)_
- What if you needed a fill value other than None? _(Tests adding a `fillvalue` parameter, matching the itertools.zip_longest API.)_
- How does this relate to a SQL full outer join? _(Tests understanding that positional zip is like joining on row number, while SQL joins on key equality.)_

## Related

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