Skip to content
Decoder

Sqrt(x)

Easy

Problem

Given a non-negative integer x, return its square root rounded down to the nearest whole number. You may not use a built-in power or square-root function (no math.sqrt, **, Math.sqrt or Math.pow).

Example 1

Input
x = 4
Output
2

Example 2

Input
x = 8
Output
2
Why
the real root is 2.828..., rounded down

Example 3

Input
x = 0
Output
0

Constraints

  • 0 <= x <= 2^31 - 1

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.