Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard
Problem: Compute a^b mod 1337 where the exponent b is given as a digit array (too large for an integer), using a^(10x + d) = (a^x)^10 · a^d.
Problem: Compute a^b mod 1337 where the exponent b is given as a digit array (too large for an integer), using a^(10x + d) = (a^x)^10 · a^d.
def super_pow(a, digits):
res = 1
for d in digits:
res = pow(res, 10, 1337) * pow(a, d, 1337) % 1337
return res
print(super_pow(2, [1, 0]), super_pow(3, [2, 0, 0]))What is printed?- A. 1024 1
- B. 1024 3
- C. 10 200
- D. 1024 30
Correct answer: D. 1024 30
2¹⁰ = 1024 < 1337, so the first is 1024. For 3²⁰⁰ the loop processes digits 2, 0, 0, building the exponent 2 → 20 → 200 without ever forming the big number; the result is 1024 30.
More Coding Interview Problems questions
- A stack supports push, pop and multipop(k) (pop up to k elements). Starting from empty, what is the worst-case total cost of any sequence…
- A binary counter starts at 0 and is incremented n times; each increment flips trailing 1s to 0 and one 0 to 1. What is the total number of…
- A service must detect whether a dependency graph with 10⁵ modules and 10⁶ edges has a cycle. Which approach is both correct and efficient?
- Shortest paths in a large grid where each move costs either 0 or 1. Which algorithm is the most efficient correct choice?
- You must report the 100 largest values from a stream of 10⁹ integers using about 1 MB of memory. Which approach fits?
- Edge case: JavaScript type coercion. javascript console.log(typeof null, [] + {}, [] == ![], 0.1 + 0.2 === 0.3); What is printed?