Skip to interview prep
Apexed

75. Fast Exponentiation (Modular Power)

MediumDivide & conquer

Return (base ^ exp) mod m using O(log exp) multiplications. All inputs are non-negative and m ≥ 1.

Example 1

Input: base = 2, exp = 10, m = 1000
Output: 24
Explanation: 1024 mod 1000

Constraints

  • 0 ≤ base, exp ≤ 10⁹
  • 1 ≤ m ≤ 46340 (so products stay exact)

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Fast Exponentiation (Modular Power) — coding interview practice — Apexed