This problem was asked by Google.

Implement integer exponentiation. That is, implement the pow(x, y) function, where x and y are integers and returns x^y.

Do this faster than the naive method of repeated multiplication.

For example, pow(2, 10) should return 1024.


I wrote the solutions in Swift. Feel free to comment with your solution in the language of your choice.

func pow(_ x : Int, _ y : Int) -> Int {
  if y == 0 { return 1 }
  let power: Int = pow(x, y/2)
  return y % 2 != 0 ? (x * power * power) : (power * power)

This problem was provided by Daily Coding Problem. Get exceptionally good at coding interviews by solving one problem every day.

Leave a Reply