Problem · Math

Power with Integer Exponents

Learn this problem
MediumLinkedIn logoLinkedInFULLTIMEONSITE INTERVIEW

Problem statement

Implement myPow(x, n), which returns the real number x raised to the signed 32-bit integer exponent n.

Your implementation must handle zero and negative exponents and must not overflow when negating the minimum 32-bit integer.

Function

myPow(x: double, n: int) → double

Examples

Example 1

x = 2.0n = 10return = 1024.0

Multiplying ten factors of 2 gives 1024.

Example 2

x = 2.0n = -2return = 0.25

A negative exponent uses the reciprocal: 2^-2 = 1 / 4.

Constraints

  • n is any signed 32-bit integer.
  • If x is 0, then n is positive.
  • Answers are compared with relative or absolute tolerance 10^-9.

More LinkedIn problems

drafts saved locally
public double myPow(double x, int n) {
    // TODO: compute x raised to n.
}
x2.0
n10
expected1024.0
checking account