Merge Sort Plus Python Decorators That Add a Constant Before Sorting
Company: DRW
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Technical Screen
Write merge sort by hand in Python, then change what the sort receives by wrapping it in a decorator instead of editing the sort itself. The round expected runnable code, and each part builds on the previous one.
### Clarifying Questions
- Should `merge_sort` return a new sorted list, or sort the caller's list in place?
- Must the sort be stable, so that equal elements keep their input order?
- May the decorator modify the caller's list, or must it leave the input untouched?
- Should the decorated function accept extra positional or keyword arguments and pass them through?
### Part 1 — Merge sort
Implement merge sort without calling `sorted` or `list.sort`:
```python
def merge_sort(nums: list[int]) -> list[int]:
```
For example, `merge_sort([5, 2, 4, 2, 1])` returns `[1, 2, 2, 4, 5]`, and `merge_sort([])` returns `[]`.
```hint Ties and leftovers
Decide which side wins when the two front elements are equal, and what must happen when one side runs out before the other.
```
#### What This Part Should Cover
- Base cases, the split, and a merge that handles leftovers on either side
- Stability when equal elements meet
- Time and extra space, and the recursion depth
### Part 2 — A decorator that adds 1 first
Write a decorator `add_one` that, applied to your merge sort, makes every call add 1 to each element of the input before sorting. For example, the decorated sort returns `[2, 3, 4]` for the input `[3, 1, 2]`.
```hint Count the increments
Apply your decorator to the recursive sort and run it on a list of four or more elements. Check that every element was increased exactly once.
```
#### What This Part Should Cover
- What a decorator returns, and which code runs at definition time versus call time
- Transforming the input once per outside call without mutating the caller's list
- Whether recursive calls inside the sort pass through the decorator again
### Part 3 — Add x instead of 1
Change the decorator so the amount is a parameter: decorating with `@add(5)` adds 5 to every element before sorting, so the decorated sort maps `[3, 1, 2]` to `[6, 7, 8]`.
```hint Read the decorator line
`add(5)` is evaluated before anything is decorated. Ask what that call has to return for the `@` line to work.
```
#### What This Part Should Cover
- The extra layer a decorator with arguments needs, and what each layer receives
- How the amount is captured and reused across calls
- Keeping the wrapped function's name and docstring with `functools.wraps`
### What a Strong Answer Covers
- Runnable code for all three parts, checked against small examples including empty and duplicate-heavy lists
- A correct, stable merge sort with stated time and space costs
- A clear account of decoration time versus call time
- The trap of decorating the recursive function itself, and a clean way around it
- The caller's input left unmodified and extra arguments passed through
### Follow-up Questions
- How would you generalize the decorator to apply any element transformation, such as `@preprocess(lambda v: v * 2)`?
- How would you write one decorator that works both bare, as `@add`, and with an argument, as `@add(x=5)`?
- Write merge sort bottom-up without recursion. When is that version preferable?
- Compare merge sort with quicksort and heapsort on stability, worst-case time and extra memory. When would you choose each?
Overview: Implement merge sort by hand in Python, then wrap it in a decorator that adds 1 to every element before sorting and extend it to a parameterized decorator that adds any amount x. Tests stable merging, decoration time versus call time, closures, functools.wraps, and how decorators interact with recursive functions.
Read the full DRW Software Engineer interview experience this question came from