Code Room
CodingHardcod-g641
Subject Modular arithmeticLevel Senior–Staff~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Compute the n-th Catalan number modulo a prime p, where C_n = (2n choose n) / (n + 1). Use factorials and modular inverses (Fermat) so the division by (n+1) is done mod p. Constraints: 0 <= n <= 100000, p is a prime up to 10^9+7 with p > n+1. Return C_n mod p. C_0 = 1.

Implement
catalan_mod_p(n: int, p: int) → int
Examples
in[3,1000000007]out5
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.