Code Room
CodingMediumcod-g644
Subject Combinatorial countingLevel Mid–Senior~22 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.