Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Power Function Without Built-ins

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

Your question is Power Function Without Built-ins. 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

Antino Labs services may need fast numeric transformations without relying on built-in exponentiation. Implement a function that returns x raised to the integer power n, without using pow(), **, or any other built-in power function.

Use exponentiation by squaring so the algorithm remains efficient for very large exponents. If n is negative, return the reciprocal of x raised to -n.

Formal Specification

  • Input: A numeric value x and a signed integer n.
  • Output: A numeric value equal to x^n.
  • The result for n = 0 must be 1, including when x = 0.
  • Inputs will not request 0 raised to a negative power.

Constraints

  • -100 <= x <= 100
  • x may be an integer or floating-point number
  • -2^31 <= n <= 2^31 - 1
  • x != 0 when n < 0
  • Do not use pow(), **, or logarithm-based formulas

Function Signature

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