Code RoomModular exponentiation
MediumPrep Room Coding #756

Modular exponentiation

CodingAlgorithms & data structuresMid–Senior~18 min

Compute (base ** exp) % mod for non-negative integers base, exp, and a positive integer mod. The exponent exp can be as large as 10^9, so you cannot multiply exp times in a loop, and base**exp itself is far too large to materialize. Return the result as an integer in [0, mod). Note that when mod == 1 the answer is always 0.

Implement
fast_pow_mod(base: int, exp: int, mod: int) → int
Examples
in[2,10,1000]out24
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.

0:00 of about 18 min
InputExpectedGot
[2,10,1000]24not run yetsample