The Bonus Round
A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L3
- Asked in
- Optiver
Problem
A game designer reviewing play-tests has each round's rolls listed in the order the dice landed. What does a round score when every roll counts its face value and each roll that matches the one landing just before it earns an extra bonus of twice that value?
Consecutive matching dice rolls trigger a special scoring rule.
Example
Input:
rolls = [1,2,2,3]
Output:
12
Worked solution and explanation
What this really is
This is an adjacency check dressed up as a dice game. The skill is comparing each roll to the one immediately before it, never to the round as a whole. Everyone gets the face-value sum right. What separates candidates is whether the bonus follows position or frequency. Reach for Counter or a set and [6, 1, 6] scores 25 instead of 13, because two sixes with a one between them are not a streak. Award one bonus per repeated value and [4, 4, 4] scores 20 instead of 28, because a run of three holds two back-to-back pairs.
Building it
Step 1: Count every face value
Every roll adds its face, including both rolls of a matching pair. The bonus sits on top of the face values, never in place of them, so [2, 2] scores 2 + 2 + 4.
Step 2: Compare each roll with the one before it
Carry prev through a single pass. When roll == prev, add 2 * roll. Then move prev forward to the current roll every time, matched or not, so the next comparison is always against its true neighbour.
Step 3: Let the starting value handle the edges
Starting prev at None means the first roll has nothing to match, because no integer equals None. An empty round never enters the loop and returns 0, and a single roll returns its own face, with no special case for either.
def dice_score(rolls: list[int]) -> int:
total = 0
prev = None
for roll in rolls:
total += roll
# The first roll is compared against None, so it never earns a bonus.
if roll == prev:
total += 2 * roll
prev = roll
return totalCommon follow-up questions
- How does the answer change if three equal rolls in a row pay a single triple bonus of `3 * v` instead of two pair bonuses? (Tests run-length thinking: the bonus now attaches to each maximal run rather than each pair, so the loop tracks run length, not just the previous roll.)
- What if the input is a stream you can only iterate once? Does your solution still work? (Tests whether they relied on indexing or slicing. Carrying `prev` works on any iterable.)
- How would you parallelize this across N machines for a billion rolls? (Tests map-reduce thinking: each chunk returns its subtotal plus its first and last roll, and the reducer adds the bonus for matches across chunk boundaries.)