DIY: Pow(x, n)

Solve the interview question "Pow(x, n)" in this lesson.

Problem statement

For this problem, you are given an integer base and power value. You have to implement the Pow() function, which will raise the base to a specified power and return its value.

Assume the following constraints:

  • -100 < base < 100
  • -2312^{31} <= power <= 2312^{31}-1
  • -10410^4 <= xnx^{n} <= 10410^4, where x* is the baseand *n* is thepower`.

Input

The Pow() function will take two inputs: base and power, and it will call the quickPow() function recursively, which will take two inputs: base and power / 2. Here is an example of the inputs:

base = 2
power = 4

Output

The Pow() function returns the base raised to a specified power. The following is the output of the inputs shown above:

16

Coding exercise

You need to implement the Pow() function in the skeleton code given below.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.