DSA interview question
A combinatorics module needs N!, the number of ways to arrange N distinct items, for N up to 10^5. Factorials explode: 13! already exceeds a 32-bit int, and 21! exceeds 64 bits. So, like most competitive-programming and interview problems, the answer is asked modulo 10^9 + 7. The recursive definition N! = N × (N − 1)! with 0! = 1 maps directly onto code; the details that matter are applying the modulo at every step and using 64-bit multiplication so the intermediate product can't overflow.
Example: 5! = 120; 13! mod (10^9 + 7) = 227020758.
long long factorialMod(int n); // n! mod 1,000,000,007
Input n | Output | Explanation |
|---|---|---|
| 0 | 1 | 0! = 1 by definition |
| 1 | 1 | |
| 5 | 120 | |
| 10 | 3628800 | Still below the modulus |
| 13 | 227020758 | 13! = 6227020800 exceeds INT_MAX, so the modulo kicks in |
| 20 | 146326063 | 20! = 2432902008176640000, reduced mod 10^9 + 7 |
(Confirm: 0 ≤ n ≤ 10^5; the answer is n! mod 1,000,000,007.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in