Integer Power Without a Library Call
Updated
Problem statement
You are given a real number x and an integer n. Return x raised to the power n.
- For
n > 0, this is the product ofncopies ofx. - For
n = 0, the answer is1. - For
n < 0, the answer is1divided byxraised to the power-n.
Do not use a built-in power function (such as pow, Math.pow or **); the point is to build the power yourself.
An answer is accepted when it differs from the expected value e by at most 10^-5 * max(1, |e|).
Function
myPow(x: double, n: int) → doubleExamples
Example 1
x = 1.25n = 3return = 1.9531251.25 × 1.25 × 1.25 = 1.953125.
Example 2
x = 2.5n = -2return = 0.16A negative power means one divided by the positive power: 2.52 = 6.25, and 1 / 6.25 = 0.16.
Example 3
x = -1.5n = 5return = -7.59375(-1.5)5 = -7.59375. An odd power of a negative number stays negative.
Constraints
-100.0 < x < 100.0-2^31 <= n <= 2^31 - 1xis not 0, orn > 0.-10^4 <= x^n <= 10^4
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Power with Integer ExponentsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Every Result of Grouping an ExpressionC3.ai · ONSITE INTERVIEWC3.ai interview questions
- Pow(x, n)Bloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Fibonacci NumberMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Josephus SurvivorApple · ONSITE INTERVIEWApple interview questions
- N-th Fibonacci NumberBank of America · ONSITE INTERVIEWBank of America interview questions
- Integer to English Words: Up to 20 DigitsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Fibonacci with Input ValidationMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- FactorialAMD · ONSITE INTERVIEWAMD interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)