Skip to main content

50. Pow(x, n)

mediumAsked at Intel

Implement pow(x, n) without using the language's exponent operator. Intel asks because the optimal O(log n) fast-exponentiation algorithm is the same algorithm that ships inside modular-exponentiation hardware for RSA acceleration — and the negative-exponent and INT_MIN edge cases trip ~40% of candidates.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Intel loops.

  • Glassdoor (2026-Q1)Intel SWE-II onsite reports cite pow(x, n) as a recurring 30-minute math + recursion round.
  • Levels.fyi (2025-08)Intel SWE interview report describes the fast-exponentiation + negative-n combo.
  • LeetCode discuss (2025-11)Intel-tagged company-frequency listing.

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 - 1
  • n is an integer.
  • Either x is not zero or n > 0.
  • -10^4 <= x^n <= 10^4

Examples

Example 1

Input
x = 2.00000, n = 10
Output
1024.00000

Example 2

Input
x = 2.10000, n = 3
Output
9.26100

Example 3

Input
x = 2.00000, n = -2
Output
0.25000

Explanation: 2^-2 = 1/(2^2) = 1/4 = 0.25

Approaches

1. Linear multiply (brute)

Multiply x by itself |n| times. Invert if n was negative.

Time
O(n)
Space
O(1)
function myPowBrute(x, n) {
  if (n === 0) return 1;
  const negative = n < 0;
  let absN = Math.abs(n);
  let result = 1;
  for (let i = 0; i < absN; i++) result *= x;
  return negative ? 1 / result : result;
}

Tradeoff: Times out for |n| near 2^31. The point of the problem is to find O(log n).

2. Fast exponentiation, iterative (optimal)

Process n's binary representation. For each set bit, accumulate the corresponding square of x. Critical: convert n to BigInt or handle INT_MIN explicitly because -INT_MIN overflows int.

Time
O(log n)
Space
O(1)
function myPow(x, n) {
  if (n === 0) return 1;
  // Handle negative exponent. Use absolute value as a JS number (no overflow risk in JS up to 2^53).
  let exp = n;
  if (exp < 0) {
    x = 1 / x;
    exp = -exp;
  }
  let result = 1;
  let base = x;
  while (exp > 0) {
    if (exp & 1) result *= base;
    base *= base;
    exp = Math.floor(exp / 2); // signed-safe; >>> 1 would only work up to 2^31 - 1
  }
  return result;
}

Tradeoff: Logarithmic in n. Iterative beats recursion on the call-stack cost. Note: in JS, `-INT_MIN` doesn't overflow because Number covers up to 2^53, so we can negate freely; in C/C++ this is THE edge case interviewers test.

Intel-specific tips

Intel interviewers will absolutely poke at INT_MIN. The exchange goes: candidate writes `if (n < 0) n = -n`, interviewer asks 'what if n is INT_MIN?'. The answer in C: cast to long before negating, or handle INT_MIN as `pow(x, INT_MIN) = pow(x, INT_MIN + 1) / x`. In JS the issue evaporates because numbers go to 2^53, but you should still articulate the C-language hazard — that articulation is the Intel-specific senior signal because Intel writes lots of C.

Common mistakes

  • Forgetting the INT_MIN edge case (in C/C++) — `-INT_MIN` overflows back to INT_MIN.
  • Recursive solution without tail-call optimization → stack overflow for large n.
  • Using `exp >>> 1` for the right-shift — works in JS only when exp fits in 32 bits; use `Math.floor(exp/2)` for safety with negative-now-flipped large exponents.
  • Forgetting that x^0 = 1 even for x = 0 (or returning NaN); the problem guarantees not-both-zero but you should still handle n = 0 first.

Follow-up questions

An interviewer at Intel may pivot to one of these next:

  • Modular exponentiation: pow(x, n, mod). (Same algorithm, just reduce mod at each step.)
  • Matrix exponentiation: pow(M, n) for a 2x2 matrix M — directly enables O(log n) Fibonacci.
  • What if x and n are very large arbitrary-precision integers? (BigInt; same algorithm.)

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does processing the binary representation of n give O(log n)?

n has log2(n) bits. For each bit, we do at most one multiply (when the bit is set) plus one square (always). So total multiplies are 2 * log2(n) — that's the constant hiding in the Big-O.

Iterative vs recursive — which does Intel prefer?

Iterative. Intel hardware-SWE code tends to live in driver or firmware contexts where the call stack is shallow and recursion is discouraged. The iterative version is also easier to inline by the compiler.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Pow(x, n) and other Intel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →