# Distribute Values Into Container Types

> Round-robin the values. Keep rotating.

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

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given a list of integers (with duplicates) and a list of container-type names (each one of 'set', 'list', 'tuple'), build groups by distinct value ascending. Group g_i is the list of every occurrence of the i-th distinct value (so duplicates show up multiple times). Round-robin assign group g_i to containers[i % len(containers)]. For each group, return {'values': <values from the group, deduplicated and sorted ascending if container is 'set', otherwise in original order>, 'container': <container name>}. Return one dict per group, in group order.

## Worked solution and explanation

### Why this problem exists in real interviews

This is a small ETL choreography: group, sort, distribute, transform. The interviewer is checking that you can build groups by value (preserving duplicates per group), iterate them in a deterministic order, and apply a per-container transform driven by `i % len(containers)`.

---

### Break down the requirements

#### Step 1: Build groups in sorted-key order

Walk `values` once and append each occurrence to `groups[v]`. Then iterate the keys in sorted order so the group sequence is deterministic.

#### Step 2: Rotate through containers by index

For group index `i`, the container is `containers[i % len(containers)]`. This is the round-robin rotation; modulo handles the wrap.

#### Step 3: Apply the per-container transform

Only the `'set'` container changes the values: deduplicate and sort ascending. For `'list'` and `'tuple'`, leave the group's values in their original (input-order) sequence. Wrap the result as `{'values': [...], 'container': <name>}`.

---

### The solution

**Group by value, round-robin assign, transform per container**

```python
def distribute(values: list[int], containers: list[str]) -> list[dict]:
    groups: dict[int, list[int]] = {}
    for v in values:
        groups.setdefault(v, []).append(v)
    out = []
    for i, key in enumerate(sorted(groups)):
        container = containers[i % len(containers)]
        group = groups[key]
        if container == 'set':
            shaped = sorted(set(group))
        else:
            shaped = list(group)
        out.append({'values': shaped, 'container': container})
    return out
```

> **Time and Space Complexity**
>
> **Time:** O(n + g log g) where n = len(values) and g is the number of distinct values (the sort is over distinct keys only).
> 
> **Space:** O(n) for the grouped values plus O(g) for the sorted-key iteration.

> **Interviewers Watch For**
>
> Strong candidates pass `containers` as strings (not actual `set`/`list`/`tuple` objects) because that's what the input shape gives them. They also use `setdefault` rather than an `if v not in groups` check so the group-building stays one-line.

> **Common Pitfall**
>
> Forgetting that `'set'` deduplicates. If group [3, 3] is assigned to a set bucket, the values list collapses to [3]. The other two containers preserve duplicates because the prompt asks for the original group sequence.

---

## Common follow-up questions

- What does the result look like when `len(values)` distinct keys is less than `len(containers)`? Are the unused containers visible anywhere? _(Tests that unused containers are simply never assigned to.)_
- How would you add a fourth container type (say 'frozenset' or 'sorted_list') without re-touching the dispatcher? _(Tests extensibility via a strategy mapping rather than an inline if/elif chain.)_
- What if the values were dicts instead of integers? Which container would break first and how would you guard it? _(Tests awareness that unhashable types cannot be set elements, requiring a fallback.)_

## Related

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