Code Room
CodingMediumcod-g182
Subject Modular arithmeticLevel Mid–Senior~18 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.