Code Room
CodingHardcod-g396
Subject Combinatorial countingLevel Senior–Staff~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Compute C(n, r) = n! / (r! * (n-r)!) modulo a prime p, for potentially large n. Return 0 if r < 0 or r > n. Constraints: 0 <= n <= 100000, p is prime with p > n (so all factorials are invertible mod p). You must never compute the full factorials as big integers proportional to n; work entirely under the modulus.

Implement
ncr_mod_p(n: int, r: int, p: int) → int
Examples
in[5,2,1000000007]out10
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.