Raise a Number to an Integer Power
Updated
Problem statement
Given a real number x and an integer n, return x raised to the power n, that is x^n. Do not call a built-in power function.
- For
n > 0,x^nisxmultiplied by itselfntimes. x^0 = 1.- For
n < 0,x^n = 1 / x^(-n).
Your answer is accepted if it differs from the exact value v by at most 10^-5 * max(1, |v|). So a result smaller than 10^-5 in size may be returned as 0.
Function
power(x: double, n: int) → doubleExamples
Example 1
x = 2.0n = 10return = 1024.0Ten factors of 2: 2 * 2 * ... * 2 = 1024.
Example 2
x = 2.5n = -3return = 0.064A negative power means one divided by the positive power: 2.5^(-3) = 1 / 2.5^3 = 1 / 15.625 = 0.064.
Example 3
x = -0.5n = -7return = -128.0(-0.5)^(-7) = 1 / (-0.5)^7 = 1 / (-0.0078125) = -128. The base is negative and the power is odd, so the result is negative.
Constraints
-100 <= x <= 100, with at most 4 digits after the decimal point-2^31 <= n <= 2^31 - 1- If
xis0, thenn > 0 - The exact value of
x^nis between-10^4and10^4
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Multiply Without the Multiplication OperatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
- Power with Integer ExponentsNVIDIA · PHONE SCREENNVIDIA interview questions
- Pow(x, n)Bloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Fewest Games to Find the Top Two TeamsSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Count the 1 Bits in a 32-Bit ValueAmazon · PHONE SCREEN
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- Checked Recursive SumHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Power of Two Without DivisionArm · PHONE SCREEN
- Powers of Two in a RangeAkuna Capital · PHONE SCREENAkuna Capital interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)