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 base and n is the power.


The function will take two inputs: base and power. Here is an example of the inputs:

base = 2
power = 4


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


Coding exercise

You need to implement the pow(base, power) function in the skeleton code given below.

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