Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
Edge case: nCr mod p is computed with factorials and Fermat inverses. Here n ≥ p.
Edge case: nCr mod p is computed with factorials and Fermat inverses. Here n ≥ p.
p = 7 fact = [1] * 11 for i in range(1, 11): fact[i] = fact[i-1] * i % p inv = lambda x: pow(x, p - 2, p) print(fact[10] * inv(fact[3]) * inv(fact[7]) % p)What is printed?
- A. 1
- B. 6
- C. 0
- D. 3
Correct answer: C. 0
C(10, 3) = 120 ≡ 1 (mod 7), but 7! and 10! are both ≡ 0 mod 7, and pow(0, 5, 7) = 0, so the formula prints 0. Factorial-inverse nCr needs n < p; for n ≥ p use Lucas' theorem: C(10,3) ≡ C(1,0)·C(3,3) = 1.
More Coding Interview Problems questions
- Problem: Extended Euclid returns g = gcd(a, b) and integers x, y with a·x + b·y = g. python def eg(a, b): if b == 0: return a, 1, 0 g, x…
- Problem: A segment tree with lazy propagation supports 'add v to a[l..r]' and 'sum of a[l..r]' on an array of 8 zeros. python N = 8 t, lz…
- Problem: Count inversions (i < j, a[i] > a[j]) with a Fenwick tree over values, scanning from the right. python def inversions(a): n =…
- Problem: Return the longest word that can be built one letter at a time from other words in the list (every prefix must be present); ties…
- Problem: s1[i] and s2[i] are declared equivalent letters (the relation is reflexive, symmetric and transitive). Replace every letter of…
- Problem: Find all roots that give a tree of minimum height, by repeatedly peeling off leaves. python def mht(n, edges): adj = [set() for…