You are given a positive integer n. Return the square root of n.
If n is not a perfect square, return the floor of its square root — the greatest integer that is less than or equal to the true square root.
In other words, find the largest integer x such that x * x <= n.
x * x <= n look like yes, yes, yes, no, no — you want the last yes.sqrt can return values like 3.9999999 for large perfect squares, so flooring it gives an off-by-one wrong answer.n = 15 the true root is about 3.87, but the answer is 3 — the largest integer whose square does not exceed n — never 4.lo and hi have crossed and neither necessarily points at the answer. Saving ans = mid every time mid * mid <= n guarantees you return the largest value that actually fit.mid * mid in a 64-bit (or bigger) type, or rearrange the test as mid <= n / mid to avoid overflow entirely.x = (x + n/x) / 2, which converges quadratically.Input: n = 4 Output: 2 4 is a perfect square, so its square root is exactly 2.
Input: n = 11 Output: 3 The square root of 11 is about 3.316. It is not a whole number, so we return the floor: 3.
Input: n = 1 Output: 1 1 is a perfect square — its square root is 1.
- 1 <= n <= 3*10^4
Computing an integer square root without calling a library function is a classic warm-up that hides a big idea: the answer lives in a sorted, invisible search space, and anything sorted can be binary-searched. Master this and you unlock a whole family of search-on-the-answer problems.
x grows, x * x only grows — once a square overshoots n, every larger square overshoots too.Restated plainly: find the largest integer x with x * x <= n. If n is a perfect square, x * x equals n exactly; otherwise x is the floor of the true square root.
Worked example — n = 11
x: 1 2 3 4
x*x: 1 4 9 16
fits ✓ too big ✗
answer: 3 (3*3 = 9 <= 11, but 4*4 = 16 > 11)
Asking a few sharp questions before coding shows you think about contracts, not just code.
“Is n always at least 1?”
Yes — n >= 1, so the answer is always at least 1 and we never worry about zero or negatives.
“Can I use the built-in square root function?”
Interviewers almost always say no — the point is to derive it. Floating-point sqrt can also be off by one for large inputs.
“What should I return for n = 1?”
1 — it is a perfect square. This is the smallest input and a common off-by-one trap.
“What about a value just below a perfect square, like n = 15?”
Return 3, not 4 — we floor, never round. 4*4 = 16 > 15.
“How large can n get?”
Up to 3 * 10^4 here — even a linear scan passes, but binary search is the answer the interviewer wants, and it scales to 10^18.
Before I code, a few quick questions.
I assume n is at least 1, so the answer is always at least 1 — correct?
And when n is not a perfect square, I should floor the root, never round up — for n equals 15 the answer is 3.
Since squares grow monotonically, I will binary-search for the largest x with x squared at most n.
Ask each candidate x the question: does x * x <= n? The answers look like yes, yes, yes, no, no, no… — all the yeses come first, because squaring is monotonic. Our target is the last yes.
n = 11
x: 1 2 3 | 4 5 ...
x*x<=11: yes yes yes | no no
^ last yes = answerAny time answers split into a yes-prefix and a no-suffix, you can find the boundary in O(log n) steps: probe the middle, keep the half that might contain the last yes, repeat. No need to test every candidate.
When mid * mid <= n, mid is a valid answer — save it and search right for a bigger one. When mid * mid > n, mid is useless — search left. The last saved value is the floor square root.
| Brute force | Binary search | |
|---|---|---|
| Time | O(sqrt(n)) | O(log n) |
| Space | O(1) | O(1) |
| Idea | Try sides 1, 2, 3… until one overshoots | Halve the candidate range around the yes-no boundary |
Both full implementations are in the Approaches selector below.
Key takeaway
When a question about a number has yes-yes-yes-no-no answers as the candidate grows, you do not need the formula — you binary-search the boundary. This search on the answer pattern reappears everywhere: allocation problems, minimum capacity to ship packages, Koko eating bananas.
lo = 1, hi = n, ans = 1
while lo <= hi:
mid = (lo + hi) / 2
if mid*mid <= n: ans = mid; lo = mid + 1 # valid — try bigger
else: hi = mid - 1 # too big — go smaller
return ans