50. Pow(x, n)
mediumAsked at MetaCompute x raised to the n-th power. Meta asks this to test whether you reach for the O(log n) fast-exponentiation pattern and can handle the negative exponent edge case.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Meta loops.
- Glassdoor (2026-Q1)— Meta E3/E4 phone-screen reports cite this as a recurring math/recursion warmup.
- Blind (2025-11)— Recurring Meta interview problem.
Problem
Implement pow(x, n), which calculates x raised to the power n (i.e., x^n).
Constraints
-100.0 < x < 100.0-2^31 <= n <= 2^31 - 1n is an integer.Either x is not zero or n > 0.-10^4 <= x^n <= 10^4
Examples
Example 1
x = 2.00000, n = 101024.00000Example 2
x = 2.10000, n = 39.26100Example 3
x = 2.00000, n = -20.25000Approaches
1. Linear loop
Multiply x by itself n times.
- Time
- O(n)
- Space
- O(1)
function myPowLinear(x, n) {
if (n === 0) return 1;
if (n < 0) { x = 1 / x; n = -n; }
let result = 1;
for (let i = 0; i < n; i++) result *= x;
return result;
}Tradeoff: TLE at n = 10^9. Mention only as the baseline.
2. Fast exponentiation, recursive
x^n = (x * x)^(n/2) for even n; x * x^(n-1) for odd n. Recurse.
- Time
- O(log n)
- Space
- O(log n) recursion
function myPowRec(x, n) {
if (n === 0) return 1;
if (n < 0) return 1 / myPowRec(x, -n);
if (n % 2 === 0) {
const half = myPowRec(x, n / 2);
return half * half;
}
return x * myPowRec(x, n - 1);
}Tradeoff: Halving the exponent each step gives log n. Recursive form is clearest for whiteboarding.
3. Fast exponentiation, iterative (optimal)
Binary exponentiation: walk n's bits. If a bit is set, multiply result by current power-of-x. Square x each step.
- Time
- O(log n)
- Space
- O(1)
function myPow(x, n) {
if (n < 0) { x = 1 / x; n = -n; }
let result = 1;
while (n > 0) {
if (n & 1) result *= x;
x *= x;
n = Math.floor(n / 2);
}
return result;
}Tradeoff: O(1) extra space, no recursion. The bit-walk framing is the canonical 'binary exponentiation' pattern that generalizes to modular exponentiation.
Meta-specific tips
Meta interviewers grade this on the log-n recognition. State out loud: 'I can halve the exponent each step because x^n = x^(n/2) * x^(n/2). For odd n, peel off one factor.' The iterative bit-walk version is cleaner; the recursive version is easier to whiteboard. Either passes the bar.
Common mistakes
- Forgetting to handle n = INT_MIN — negating it overflows in some languages (JS doubles are safe).
- Computing half twice (half * half is correct; calling myPowRec(x, n/2) * myPowRec(x, n/2) is wasteful).
- Using Math.pow — defeats the purpose. The interviewer wants the algorithm.
Follow-up questions
An interviewer at Meta may pivot to one of these next:
- Modular exponentiation (compute x^n mod m).
- Matrix exponentiation (for Fibonacci-like recurrences).
- Integer power without using floats.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why O(log n) and not O(n)?
Each step halves the exponent. After log2(n) halvings, the exponent is 0. So we do at most log2(n) multiplications.
Iterative or recursive?
Iterative uses O(1) space and avoids the stack. Recursive is shorter and clearer. Both score the same — pick what you can write without bugs.
Practice these live with InterviewChamp.AI
Drill Pow(x, n) and other Meta interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →