Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Pow(x, n)
Problem
Compute x raised to the integer power n, where x is a floating-point number and n may be negative, zero, or positive. Do it in time logarithmic in |n|.
xis a small magnitude float, well under 100 in absolute value.nfits in a signed 32-bit int, so it can be as negative as-2^31.- Either
xis non-zero ornis positive, so0^0and division by zero do not arise. - The result is guaranteed to fit in a 64-bit float; answers within
1e-5of the true value are accepted.
Examples
x = 3.0, n = 7→2187.0.x = 3.0, n = -2→0.1111..., since3^-2 = 1 / 3^2.x = 2.0, n = -2147483648→0.0;2^-2147483648underflows to zero. The magnitude2^31does not fit in a signed 32-bit int, which is the trap in languages with fixed-width integers.
Hints
- Multiplying
xby itselfntimes is O(n). Forn = 2^31that is billions of multiplications. Can you reuse work? x^n = (x^(n/2))^2whennis even, andx * (x^(n/2))^2when odd. How deep does that recursion go?- Fast exponentiation (exponentiation by squaring), recursively or by iterating over the bits of
n. Handle negativenby invertingxand negatingnfirst.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.