Pow(x, n)
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in April 2021, and the funny part is there's no data structure at all. Pow(x, n) is pure math: fast exponentiation by squaring. If your OA lands on it, the work is a loop and a few edge cases, not a clever container. Most people know the idea and still lose points on negative exponents and the 32-bit minimum. That's where the traps live. Know the halving trick cold and you're done in ten minutes. If you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the solution while the proctor sees nothing.
The problem
Return x raised to the integer power n. For a negative exponent, return the reciprocal of the corresponding positive power. Function myPow(x: double, n: int) → double Examples Example 1 x = 2.0 n = 10 return = 1024.0 Two multiplied ten times is 1024. Example 2 x = 2.0 n = -2 return = 0.25 A negative exponent takes the reciprocal. Constraints -100.0 < x < 100.0. n is a 32-bit signed integer. Inputs avoid undefined division by zero.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is exponentiation by squaring. Instead of multiplying x by itself n times, you halve the exponent each step. If n is even, square the base and halve n. If n is odd, fold the current base into the result first. That takes O(log n) time and O(1) space when done iteratively. The classic pitfall is n = -2147483648. Negating it in a 32-bit int overflows, so convert n to a 64-bit integer before you flip the sign. Then, for negative n, either invert x first or return 1 divided by the result. Also watch for n = 0, which returns 1.0. A naive O(n) loop will time out on large exponents, and plain recursion without halving risks a stack blowup. If the live OA freezes your brain, StealthCoder is the hedge that surfaces this exact pattern on demand.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Pow(x, n) cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as powx n. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Pow(x, n) FAQ
What's the trick in Pow(x, n)?+
Exponentiation by squaring. Halve the exponent each step and square the base. When the exponent is odd, multiply the result by the current base. This cuts the work from n multiplications to about log n, which is what the assessment is actually checking.
How do I handle negative exponents?+
Compute the positive power, then return its reciprocal. Or invert x first and use the absolute value of n. Either works. Just make sure you do the sign conversion after you've widened n to a 64-bit integer, not before.
Why does n = -2147483648 break so many solutions?+
In a 32-bit signed int, the minimum value has no positive counterpart. Writing n = -n overflows and stays negative. Store n in a 64-bit type first, then take the absolute value. This one edge case is the most common hidden test failure.
Should I write it recursively or iteratively?+
Either passes. Recursion is shorter and mirrors the math, with depth around 32. Iteration uses constant space and avoids any stack worries. Pick whichever you can write without bugs under pressure. Iterative is slightly safer for edge cases.
How do I prepare for this in 48 hours?+
Write the iterative version from memory twice. Test it on x = 2.0 with n = 10, n = -2, n = 0, and the 32-bit minimum. Know why each step halves n. That's the whole problem, and it takes under an hour to lock in.