Code RoomStars and bars
MediumPrep Room Coding #1210

Stars and bars

CodingAlgorithms & data structuresMid–Senior~22 min

Count the number of ways to distribute n identical items into k distinct bins where each bin gets zero or more items (i.e. the number of non-negative integer solutions to x1+...+xk = n). This is the stars-and-bars count C(n+k-1, k-1), taken modulo 10^9+7. Constraints: 0 <= n <= 100000, 1 <= k <= 100000.

Implement
stars_and_bars(n: int, k: int) → int
Examples
in[3,2]out4
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 22 min
InputExpectedGot
[3,2]4not run yetsample