Sqrt(x) easy
Description
Given a non-negative integer x, compute and return the integer square root of x. The result should be truncated â floor(sqrt(x)).
You may not use a built-in exponent or square-root function.
Examples
> Case 1: x = 4 Output: 2
> Case 2: x = 8 Output: 2 # floor(sqrt(8)) = floor(2.828) = 2
> Case 3: x = 0 Output: 0
> Case 4: x = 2147395599 Output: 46339Constraints
0 <= x <= 2^31 - 1
Binary search on the answer
This is the introductory âbinary search on the answerâ problem. The clever realization: we donât have an array to search â we have a range of candidate answers [0, x], and the predicate s² > x is monotonic over that range.
s : 0 1 2 3 4 5 6 ...
s² > 8 : F F F T T T T ...The first s where s² > x is one past the integer square root. Subtract 1 to get the answer.
Code
Use long (or long long) for mid in C++ / Java. When x = 2^31 - 1, mid can be as large as 2^31 / 2 â 46340, and mid * mid exceeds the 32-bit int max. Pythonâs unbounded ints donât have this problem.
Why hi = x + 1 and not hi = x?
The first s where s² > x could be x + 1 itself for tiny x. With hi = x exclusive, we exclude s = x as a candidate â but for x = 0, the answer is 0, and weâd never visit it. By setting hi = x + 1, we make the range inclusive of every valid s.
Concrete example: x = 0. lo = 0, hi = 1. Iteration: mid = 0, 0 * 0 = 0 > 0 is false, so lo = 1. Loop exits with lo = 1. Return lo - 1 = 0. â
Approach 2: Newtonâs method
The non-binary-search approach. Newtonâs iteration converges quadratically:
def my_sqrt_newton(x):
if x == 0: return 0
s = x
while s * s > x:
s = (s + x // s) // 2
return s- Time: O(log log x) â quadratic convergence beats binaryâs linear.
- Space: O(1).
Newtonâs method is what languagesâ sqrt() actually uses (with floating-point math and seed values from a lookup table). Binary search is more intuitive and easier to write correctly under interview pressure.
Approach 3: Bit manipulation
A puristâs solution that uses only bit shifts:
def my_sqrt_bits(x):
if x == 0: return 0
result = 0
bit = 1 << 15 # highest bit we care about (for 32-bit x)
while bit > 0:
if (result + bit) ** 2 <= x:
result += bit
bit >>= 1
return resultSame O(log x) cost as binary search; cute but not interview-clearer.
The broader pattern
âBinary search on the answerâ is the single most powerful application of binary search beyond the textbook. Sqrt(x) is the gateway. Other interview problems with the same shape:
| Problem | Answer space | Predicate |
|---|---|---|
| Sqrt(x) (this one) | [0, x] | s² > x |
| Capacity to Ship Packages in D Days | [max(w), sum(w)] | can ship in D days at capacity c |
| Koko Eating Bananas | [1, max(pile)] | can finish in H hours at speed k |
| Split Array Largest Sum | [max, sum] | can split with max-sum ⤠x in K parts |
| Minimum Speed to Arrive on Time | [1, big] | arrival time at speed s ⤠deadline |
| Find K-th Smallest Pair Distance | [0, max - min] | count of pairs with distance ⤠x ⼠K |
If your answer is âan integer in some rangeâ and thereâs a monotonic check, binary-search the integer range. The pattern saves dozens of lines of clever logic.
Analysis
- Time: O(log x) for binary search; O(log log x) for Newton.
- Space: O(1).