Implement the function pow(x, n), which calculates x^n.
Note that you cannot use any built-in functions that directly implement pow.
Examples:
- Input:
x = 2, n = 5→ Output:32 - Input:
x = 3.4, n = 3→ Output:39.04
Key Ideas:
- When
nis even:x^n = (x^(n/2))^2 - When
nis odd:x^n = x * (x^(n-1)) - Handle negative exponents by returning
1 / pow(x, -n)
This approach tests understanding of recursion, divide and conquer, and edge case handling (e.g. negative powers, floating-point base).
The VOprep team has long accompanied candidates through various major company OAs and VOs, including SnowFlake, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.