KatInterview

Overflow-Safe Power with a Custom Modular Multiplier

Math & Bit ManipulationAuto-graded · 23 test cases
MediumAuto-gradedMath & Bit Manipulationmodular arithmeticbit manipulationexponentiation by squaring

Last updated

Tests whether a candidate can build exponentiation from scratch using only 64-bit-safe addition and multiplication under an arbitrary modulus.

Overflow-Safe Modular Exponentiation

You are working in an environment where integers are limited to 64 bits. Your task is to compute (base ^ exp) mod m without letting any intermediate value exceed 64 bits.

The challenge

When base and exp are both close to 2^61 − 1, a naive base * exp can require up to 122 bits — far beyond a 64-bit register. You must build your solution from the ground up so that every intermediate value stays within 64 bits, just as it would have to in C, C++, Java, or Go.

Build your solution in three layers:

Layer 1 — overflow-safe addition under a modulus

Given x, y ∈ [0, m), return (x + y) mod m. You may not compute x + y directly when the sum could reach or exceed 2^64. Instead, use a comparison-and-subtract strategy that keeps all values below m.

Layer 2 — overflow-safe multiplication under a modulus

Given x, y ∈ [0, m), return (x * y) mod m. Use a binary "double-and-add" loop (analogous to the peasant multiplication algorithm): process the bits of y one at a time, doubling x with your safe addition helper and accumulating into a result.

Layer 3 — fast exponentiation

Given base, exp, and m, return (base ^ exp) mod m. Use a square-and-multiply loop: process the bits of exp one at a time, squaring base with your safe multiplication helper and accumulating into a result.

Rules

  • All three layers must call only the layer below them for any potentially large arithmetic — no plain * or + between values that could be near m unless they are already known to be small.
  • You may assume Python's unbounded integers for the purposes of this problem, but you must enforce the 64-bit discipline yourself: every value passed into or produced by your helpers must stay in [0, m).

Edge-case rules

  • exp = 0 returns 1 % m for any base, including base = 0 (the convention 0^0 = 1 applies).
  • m = 1 always returns 0.
  • base >= m must be reduced to base % m before the first multiplication.

Constraints

  • 0 ≤ base < 2^61 − 1
  • 0 ≤ exp < 2^61 − 1
  • 1 ≤ m < 2^61 − 1
  • Inputs are integers; the return value is an integer in [0, m).

Examples

Example 1

Input: modular_pow(2, 10, 1000)

Output: 24

Example 2

Input: modular_pow(3, 0, 7)

Output: 1

Example 3

Input: modular_pow(10, 3, 6)

Output: 4

Hints

Hint 1

For safe addition of x and y both in [0, m), check whether x >= m - y instead of computing x + y directly. If true, the answer is x - (m - y); otherwise it is x + y. No intermediate ever leaves [0, m].

Hint 2

For safe multiplication, write a loop over the bits of y. Maintain a running double of x (using your safe add) and a running result. When the current low bit of y is set, add the current x into result. This is the same idea as left-to-right binary exponentiation, but for multiplication.

Hint 3

For exponentiation, mirror the multiplication loop exactly one level up: loop over the bits of exp, maintain a squaring base (using your safe multiply), and accumulate into result when a bit is set. Initialise result to 1 % m so the m = 1 edge case is handled automatically.