DSA Tracker

Medium

Power of a Number (Fast Exponentiation)

A medium Recursion problem included in Apna College, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Recursion
Sheets
2
Core for
11 roles
Platform
LeetCode

The problem

Implement the power function to compute x raised to the power n using fast exponentiation.

Example 1

Input
x = 2.0, n = 10
Output
1024.0
Why
2^10 = 1024.

Example 2

Input
x = 2.1, n = 3
Output
9.261

Constraints

  • -100.0 < x < 100.0
  • -2^31 <= n <= 2^31 - 1

How to think about it

Updated 2026-09-09

Multiplying by x one factor at a time is redundant when you can square the base. Every bit in the binary representation of n represents a power-of-two exponent. Whenever n is even, halving the exponent and squaring the base halves the remaining work without changing the mathematical value.

Approaches, worst first

  1. Linear multiplication

    time O(n) · space O(1)

    Multiply 1.0 by x a total of n times. Unusable for large n because 2^31 iterations exceeds typical time limits by orders of magnitude.

  2. Recursive binary exponentiation

    time O(log n) · space O(log n)

    Branch on whether n is odd or even: x^n = x * x^(n-1) or (x * x)^(n/2). Slashes the exponent in half each step, but consumes call stack frames proportional to log n.

  3. Iterative binary exponentiationWrite this one

    time O(log n) · space O(1)

    Inspect the lowest bit of n. If set, multiply the running result by current base; then square the base and shift n right. Runs in logarithmic time with zero call stack overhead.

Where people lose marks · 3
  • Negating n when n = -2^31. In 32-bit signed two's complement, -(-2^31) overflows back to -2^31, causing an infinite loop unless promoted to a 64-bit integer before inversion.
  • Forgetting to invert the base or result when n is negative. x^(-n) is (1 / x)^n, not -x^n.
  • Handling x = 0 with negative n, though the problem constraints typically guarantee non-zero base for negative powers to avoid division by zero.

The theory behind it

Recursion — the ground this problem stands on. All Recursion problems

What Recursion is

Recursion is a nesting doll that opens to reveal an identical smaller doll inside. In programming, a function solves a substantial problem by delegating smaller versions of the exact same question to fresh invocations of itself. Each invocation operates on shrunken input until hitting an irreducible foundation called a base case, which returns an immediate answer and permits the waiting cascade to resolve backwards.

When to reach for it

Reach for recursion when a problem possesses self-similar subproblems, such as traversing branched tree structures, exploring graph pathways, or generating combinations. Phrases asking for all permutations, subset generation, exhaustive maze navigation, or hierarchical file system traversals signal recursive decomposition. It is natural whenever the answer to a large instance depends on assembling identical solutions for smaller subsets.

How the pattern works

Structure every recursive method around two mandatory stages: the termination stop and the shrinking recurrence. Write the base condition first so the function exits before attempting further execution. Next, trust the recursive call to return valid answers for smaller inputs without mentally unwinding every level at once. Pass accumulation state forward through parameters, or combine child return values on the ascent phase once deeper calls return.

What each operation costs

OperationTime
call stack memory allocation per frameO(d)
traversal of branching recursive call treeO(b^d)
single branch linear recursive unwindO(n)
What usually goes wrong with Recursion
  • Omitting a base case or writing a condition that input values leap over without triggering, triggering fatal call stack overflow crashes.
  • Modifying shared mutable containers across sibling branches without undoing edits on backtracking steps, contaminating alternative search paths.
  • Recomputing duplicate subproblems inside branching calls without memoizing past returns, causing execution times to explode exponentially.

Which roles need this problem

Recursion is a core topic for these 11 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including SDE / Backend Engineer, Full-Stack Developer, Security Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Recursion problems

Problem set and role mapping as of .