Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Exponentiation Power Function

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Exponentiation Power Function. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

AQR research calculations may repeatedly evaluate integer powers for signal transformations. Implement exponentiation without using Python's built-in power operators or libraries.

Given a numeric base x and an integer exponent y, return x raised to the power y. In this problem, ^ means exponentiation, not bitwise XOR.

Your solution should use exponentiation by squaring rather than multiplying by x once for every unit of the exponent. Negative exponents must return the reciprocal power.

Formal Specification

  • Input: x, a nonzero integer or floating-point number when y < 0, and y, an integer.
  • Output: The numeric value x^y. Returning an integer for integral positive powers and a floating-point value for negative powers is acceptable.
  • Do not use **, pow, or equivalent library functions.

Constraints

  • -100 <= x <= 100
  • x != 0 when y < 0
  • -10^9 <= y <= 10^9
  • 0^0 is defined as 1
  • Do not use **, pow, or equivalent library functions

Function Signature

def power(x, y):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output