The Face That Breaks the Bank
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
- L4
- Asked in
- Optiver
Problem
An analyst replaying a dice game's log, where rolls holds one list of face values (1 to 6) per round, credits every roll with its own face value toward that face's total for the whole game. She wants the face with the highest total, but only if that total is strictly above threshold, with the lower face winning a tie and an empty dict coming back when no face clears the bar.
Roll enough dice and one number always runs away with it.
Example
Input:
rolls = [[6,4,6,2,1,6],[6,3,4,6,2,6]], threshold = 20
Output:
{"6":36}Worked solution and explanation
Why this problem exists in real interviews
This is a value-weighted argmax wearing a dice costume. Anyone can tally how often each face lands, but the winner is decided by face value times frequency, so a face that shows up constantly but is low can lose to a rarer high face. Track the wrong quantity and you crown the wrong face; miss the strict cutoff and you return a face that should have lost.
Break down the requirements
Step 1: Track cumulative sums per face value
Use a dict to accumulate totals for each dice face. Every time a face value f shows up in a round, add f to totals[f] (so a face that lands six times contributes 6*6 = 36). This is where a frequency count quietly diverges from the right answer: you want the summed value, not the occurrence count.
Step 2: Process all rounds
Iterate every round and every value within it, folding each value into its face's running total. There is no early exit: you must see all rounds before you know which face is heaviest.
Step 3: Select the dominant face above the threshold
Scan totals.items() once, keeping the best face whose total strictly exceeds the threshold. On a tie for the highest total, prefer the smaller face value with an explicit face < best_face check. If no face clears the threshold, return an empty dict; otherwise return {str(face): total}.
The solution
def dice_score_accumulator(rolls: list[list[int]], threshold: int) -> dict:
totals: dict[int, int] = {}
for round_vals in rolls:
for face in round_vals:
totals[face] = totals.get(face, 0) + face
best_face = None
best_total = 0
for face, total in totals.items():
if total <= threshold:
continue
if best_face is None or total > best_total or (total == best_total and face < best_face):
best_face = face
best_total = total
if best_face is None:
return {}
return {str(best_face): best_total}Common follow-up questions
- What if no face ever exceeds the threshold? (Tests returning an empty-dict sentinel for the no-winner case.)
- What if you needed the lightest face above the threshold instead of the heaviest? (Tests changing the selection criterion while keeping the accumulation.)
- How would you parallelize the accumulation across rounds? (Tests prefix-sum / reduce approaches for parallel cumulative computation.)