Reported March 2020
Bloombergdynamic programming

Number of Dice Rolls With Target Sum

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Two dice, six faces, target 7, answer 6. That tiny example from the Bloomberg OA reported in March 2020 hides the real shape of the problem: count ordered outcomes, return the result modulo 1,000,000,007. It's a counting DP in disguise. Dice up to 30, faces up to 30, target up to 1000, so brute force dies fast but a table fits easily. If the recurrence doesn't click when the timer starts, StealthCoder is the invisible safety net running on your screen during the live OA. Know the pattern first, though. It's short once you see it.

The problem

Roll dice identical dice, each with faces numbered from 1 through faces. Return the number of ordered outcomes whose sum equals target, modulo 1,000,000,007.

Function
numRollsToTarget(dice: int, faces: int, target: int) → int

Examples
Example 1
dice = 2
faces = 6
target = 7
return = 6
The ordered pairs are (1,6) through (6,1).

Constraints
1 <= dice, faces <= 30.
1 <= target <= 1000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a DP over dice count and running sum. Let dp[i][s] be the number of ways to reach sum s using i dice. Then dp[i][s] is the sum of dp[i-1][s-f] for f from 1 to faces, where s-f is at least 0. Base case is dp[0][0] = 1. Answer is dp[dice][target]. Take the modulo on every addition, not just at the end. The common pitfall is forgetting that outcomes are ordered, so (1,6) and (6,1) both count, which the DP handles naturally. Another miss is not pruning impossible targets: if target is greater than dice*faces or less than dice, return 0. Complexity is dice * target * faces, about 900k operations at the limits, which is fine. You can roll the table into one array to save space. If you blank on the transition during the live OA, StealthCoder can hand you the working recurrence while you keep typing.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Number of Dice Rolls With Target Sum cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of dice rolls with target sum. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Dice Rolls With Target Sum FAQ

How hard is Number of Dice Rolls With Target Sum really?+

It's a medium. The recurrence is simple once you see it, and the constraints are small. People stumble on the modulo placement and on off-by-one bounds for the sum, not on the core idea. If you've done any coin-change style counting DP, this is the same family.

What's the trick to solving it fast?+

Define dp[i][s] as ways to hit sum s with i dice. For each new die, loop faces 1 to f and add dp[i-1][s-f]. Start with dp[0][0] = 1. Mod after every addition. That's the entire solution, and it runs well within the given limits.

Do I need to worry about ordering of dice?+

Yes, and it works in your favor. The problem counts ordered outcomes, so (1,6) and (6,1) are separate. The layered DP treats each die as a distinct step, so ordering is counted automatically. Don't divide by anything or try to dedupe combinations.

Can I reduce the space usage?+

Yes. Each row only depends on the previous row, so keep two arrays of size target+1, or one if you iterate carefully. With these constraints, a full 2D table is fine, so only optimize if you want cleaner code.

How do I prepare for this in 48 hours?+

Write this one from scratch twice without notes. Then do a couple of related counting DPs like coin change ways and climbing stairs with variable steps. Practice the early return for impossible targets and the modulo habit. Bloomberg-style OAs reward clean, bug-free DP over fancy tricks.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it