Skip to content
EasyBinary SearchAI interview only

Nth Root Of A Number

Asked atamazongooglemicrosoft

01 · Problem

You are given two positive integers n and m.

Return the positive integer x such that x raised to the power n equals m exactly (that is, x^n == m). If no such integer exists, return -1.

Do not rely on floating-point power or root functions to decide the answer; compute it with integer arithmetic.

02 · Examples

Example 01
Input
n = 3, m = 27
Output
3

3 * 3 * 3 = 27, so the cube root of 27 is the integer 3.

Example 02
Input
n = 4, m = 69
Output
-1

2^4 = 16 and 3^4 = 81, so 69 has no integer 4th root.

Example 03
Input
n = 1, m = 14
Output
14

Any number is its own 1st root.

03 · Constraints

  • 011 <= n <= 30
  • 021 <= m <= 109
  • 03If an integer root exists it is unique and positive

04 · Optimal complexity

Time
O(n log m)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.