Article start
DSA Course: Interview Patterns and Problem Solving
Module 15: Math & Number Theory

Binary Exponentiation: Fast Power Pattern

Compute powers under modulo by reading exponent bits.

May 29, 2026·24

Learning Outcome

After this lesson, you should be able to reduce power computation from linear time to logarithmic time.

Problem Statement

Given base, exp, and mod, return base raised to exp modulo mod.

InputOutputWhy
base = 2, exp = 10, mod = 100000000710242 raised to 10 is 1024, and it is smaller than the modulus.

Brute Force Approach

Multiply base into the answer exp times. This is O(exp) and can overflow without frequent modulo.

Optimized Approach

Use exponent bits. If the current bit is 1, multiply result by base. Square base every step and halve exp.

Exact Pseudocode

result = 1 % mod
base = base % mod
while exp > 0:
  if exp is odd:
    result = (result * base) % mod
  base = (base * base) % mod
  exp = exp // 2
return result

Reference Code

class Solution:
    def modPow(self, base, exp, mod):
        result = 1 % mod
        base %= mod

        while exp > 0:
            if exp & 1:
                result = (result * base) % mod
            base = (base * base) % mod
            exp >>= 1

        return result

Sample Dry Run

StepStateResult
Startresult = 1, base = 2, exp = 1010 is even
Squarebase = 4, exp = 5now odd
Multiplyresult = 4, base = 16, exp = 2bit was 1
Finishafter final odd bit, result = 1024return 1024

Complexity

MeasureValueReason
TimeO(log exp)The exponent is halved each loop.
SpaceO(1)Only result, base, and exp are stored.

Edge Cases

  • exp = 0 should return 1 modulo mod.
  • Take modulo after every multiplication.
  • mod should be positive in normal interview versions.

Interview Checklist

  • Initialize result as 1 modulo mod.
  • Multiply result only when the current exponent bit is 1.
  • Square base and shift exponent each loop.

FAQs

Why does halving the exponent work?

Each step consumes one binary bit of the exponent while squaring the base for the next power of two.

Why take modulo every step?

It keeps values bounded and preserves the final modulo result.

What is the core pattern?

Binary exponentiation.

Test your knowledge

Take a quick quiz based on this chapter.

Discussion

0 comments

Sign in to share a question or add to the discussion.
Start the discussion

Ask a question or share what stood out to you.