# The Op Dispatcher

> Name the operation, apply it everywhere.

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

Domain: Python · Difficulty: easy · Seniority: L4

## Problem

Given a list of record dicts (each with 'value') and an op name ('double', 'square', 'abs', 'upper'), apply the op to each record's 'value' and add a 'result' field. Return the new list.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **dispatch pattern implementation** using dictionaries or conditionals. It checks whether candidates can map operation names to functions and apply them to structured data, a common pattern in plugin systems and ETL transforms.

---

### Break down the requirements

#### Step 1: Define the operation dispatch map

Map each operation name to its corresponding function: 'double' to `x*2`, 'square' to `x**2`, etc.

#### Step 2: Iterate through records and apply the operation

For each dict, look up the `'value'` field, apply the named operation, and store the result under a `'result'` key.

#### Step 3: Return new dicts with the added key

Create a copy of each dict with the `'result'` field added.

---

### The solution

**Dict-based dispatch with per-record application**

```python
def dispatch(records, op_name):
    ops = {
        'double': lambda x: x * 2,
        'square': lambda x: x ** 2,
        'abs': lambda x: abs(x),
        'upper': lambda x: str(x).upper(),
        'lower': lambda x: str(x).lower(),
    }
    op_func = ops[op_name]
    result = []
    for record in records:
        new_record = dict(record)
        new_record['result'] = op_func(record['value'])
        result.append(new_record)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the number of records. Each operation is O(1).
> 
> **Space:** O(n) for the new list of records.

> **Interviewers Watch For**
>
> Do you use a dispatch dictionary instead of a chain of if/elif? The dict approach is cleaner, more extensible, and avoids O(k) condition checking where k is the number of operations.

> **Common Pitfall**
>
> Mutating the original records instead of creating copies. Using `dict(record)` creates a shallow copy, preserving the original data.

---

## Common follow-up questions

- What if the operation name is not in the dispatch map? _(Tests raising a KeyError or returning a default.)_
- How would you support user-defined operations? _(Tests accepting callables as parameters or a registry pattern.)_
- What if operations could chain (e.g., double then square)? _(Tests function composition or pipeline patterns.)_

## Related

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