FastPrepInteger Power Without a Library Call
Problems/Integer Power Without a Library Call

Integer Power Without a Library Call

Expedia logoExpedia● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A post published on September 5, 2022, by a student who interviewed for an Expedia SDE internship at a college recruiting drive in August 2022 and did not receive an offer, places it in the later of the two technical interviews, following a linked-list question. The report states the task in one line, for C, and gives no other detail.

It does not say whether the base or the exponent can be fractional or negative, or what limits apply. Here, you can answer in any of 5 languages. The base is a real number strictly between -100 and 100. The exponent is any 32-bit integer, so negative powers and the overflow of -2^31 matter. The exact result is between -10^4 and 10^4, and answers within 10^-5 are accepted. The tests cannot detect a built-in power call, so avoiding one is up to you.

Our version is about 80% the same as the reported question.

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 of n copies of x.
  • For n = 0, the answer is 1.
  • For n < 0, the answer is 1 divided by x raised 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) → double

Examples

Example 1

x = 1.25n = 3return = 1.953125

1.25 × 1.25 × 1.25 = 1.953125.

Example 2

x = 2.5n = -2return = 0.16

A 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 - 1
  • x is not 0, or n > 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)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public double myPow(double x, int n) {
        // Write your code here.
    }
}
Checking account…