Power with an Integer Exponent
Updated
Problem statement
Return x raised to the integer power y as a double-precision number, without using a built-in power function.
For this exercise, a negative exponent means the reciprocal of the corresponding positive power. An exponent of zero returns 1, including 0^0. A zero base is allowed only when y is nonnegative. Return a numerical approximation; it is accepted when its absolute error is at most 1e-9 * max(1, abs(expected)).
Function
integerPower(x: double, y: int) → doubleExamples
Example 1
x = 2y = -3return = 0.125A negative exponent gives the reciprocal:2^-3=1/(2*2*2)=0.125.
Example 2
x = -2y = 5return = -32An odd exponent keeps a negative sign:(-2)^5=-32.
Constraints
- For this exercise, assume -2 <= x <= 2, with x a finite double.
- For this exercise, assume x is zero or abs(x) is at least 0.5.
- For this exercise, assume -1000 <= y <= 1000.
- If x is zero, y is nonnegative.
More Morgan Stanley Onsite Interview questions
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
- Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
- Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
- Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
- Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
- LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
- All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Integers With Multiple Sum of Two CubesCharta Health · PHONE SCREEN · ONSITE INTERVIEWCharta Health interview questions
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Integer BreakAmazon · ONSITE INTERVIEW
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEWeBay interview questions
- Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEWUpstart interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)