# The Bonus Round

> Consecutive matching dice rolls trigger a special scoring rule.

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given a list of integer dice rolls, compute the total score. Each roll contributes its face value. Additionally, whenever two consecutive rolls show the same value v, add a bonus of 2*v (on top of counting both rolls normally). Return the final integer score.

## Worked solution and explanation

### Why this problem exists in real interviews

This is a single-pass aggregation with a one-step lookback. Interviewers use it to filter out candidates who reach for nested loops or `zip(rolls, rolls[1:])` when a single sweep with a `prev` variable does the job in O(N) time and O(1) space.

---

### Break down the requirements

#### Step 1: Add every face value once

The base score is `sum(rolls)`. Every roll counts, even rolls that are part of a doubled pair. The bonus is added on top, not in place of, the face values.

#### Step 2: Detect equal consecutive rolls with a single lookback

Track the previous roll. When the current roll matches it, add `2 * v` to the total. No lookahead is needed and no second pass.

#### Step 3: Handle the empty and single-roll cases naturally

Initializing `prev = None` and skipping the bonus when `prev is None` makes the empty list (returns 0) and single roll cases work without special casing.

---

### The solution

**Single pass with one-step lookback**

```python
def dice_score(rolls: list[int]) -> int:
    total = 0
    prev = None
    for v in rolls:
        total += v
        if prev is not None and v == prev:
            total += 2 * v
        prev = v
    return total
```

> **Cost Analysis**
>
> Time: O(N) for N rolls, single pass. Space: O(1) auxiliary; only `total` and `prev` are kept. No intermediate list of pairs.

> **Interviewers Watch For**
>
> Whether you avoid materializing pairs with `zip(rolls, rolls[1:])` (which doubles memory), whether your bonus is `2 * v` and not `v` (a common misread), and whether you handle three-in-a-row correctly. The prompt scores each adjacent equal pair, so `[5, 5, 5]` triggers two bonuses, not one.

> **Common Pitfall**
>
> Reading 'two consecutive rolls show the same value' as 'detect runs' and only awarding one bonus per run. The spec is per adjacent pair, so `[3, 3, 3]` scores 3 + 3 + 3 + 6 + 6 = 21, not 3 + 3 + 3 + 6 = 15.

---

## Common follow-up questions

- How does the answer change if a triple (three equal in a row) gives a triple bonus of `3 * v` instead of two pair bonuses? _(Tests run-length thinking. The candidate has to detect runs of length >= 2 and award a single bonus per run, which is a different shape of loop.)_
- What if the input is a stream you can only iterate once? Does your solution still work? _(Tests whether they used indexing or assumed a list. The single-pass `prev` approach works on any iterable.)_
- How would you parallelize this across N machines for a billion rolls? _(Tests map-reduce thinking. Each chunk computes its own subtotal and remembers its first and last roll; a reducer adds the bonus across chunk boundaries.)_

## Related

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