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?
  1. A. 1024 1
  2. B. 1024 3
  3. C. 10 200
  4. 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

Exams that ask this topic