Whole square root
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
Given a whole number n, return the biggest whole number r whose square, r × r, is not more than n.
For perfect squares that's the exact square root: for n = 49, it's 7. Otherwise it's the square root rounded down: for n = 50, 7 × 7 = 49 fits but 8 × 8 = 64 is too much, so it's 7 again.
Examples
Example 1
- Input
- n = 49
- Output
- 7
7 × 7 = 49 exactly.
Example 2
- Input
- n = 50
- Output
- 7
7 × 7 = 49 fits; 8 × 8 = 64 is too much.
Example 3
- Input
- n = 0
- Output
- 0
Rules
nis between 0 and 2,000,000,000,000 (two trillion).- Don't use
Math.sqrt,Math.powor**: the point is to find it yourself.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We want the biggest whole number whose square fits under n. For 50 that's 7: 7 × 7 = 49 fits, 8 × 8 = 64 doesn't. And we can't use Math.sqrt.
2. Solve an example by hand
There's no list here. But think about the candidates 0, 1, 2, 3… and ask each one "does your square fit?". For n = 50: 0 yes, 1 yes, … 7 yes, 8 no, 9 no… The answers are yes, then no, and they flip once. We want the last yes.
3. The slow way
let r = 0;while ((r + 1) * (r + 1) <= n) r++;return r;Correct, and fine for small numbers. For two trillion, it counts up to 1,414,213, one at a time. That's O(√n).
4. Spot the pattern
A yes/no question that flips once, over a range of numbers: binary search on the answer. The "list" is just the numbers 0 to n; we never need to build it.
5. The idea
Search the range 0 to n. For the middle candidate:
- Its square fits? It's a possible answer. Remember it, and look for a bigger one:
low = mid + 1. - Too big? Look smaller:
high = mid - 1.
Binary search over the possible answers
candidates
- n =
- 20
- mid × mid =
- –
- best =
- 0
function wholeSquareRoot(n) { let low = 0; let high = n; let best = 0; while (low <= high) { const mid = Math.floor((low + high) / 2); if (mid * mid <= n) { best = mid; // fits: remember it, and try bigger low = mid + 1; } else { high = mid - 1; // too big: try smaller } } return best;}The answer is somewhere from 0 to 20.
6. The code, line by line
let low = 0; let high = n; let best = 0;The answer is between 0 and
n(fornof 0 or 1, it'snitself).bestremembers the biggest candidate that fit.while (low <= high) { const mid = Math.floor((low + high) / 2);Standard binary search: look at the middle of the range.
if (mid * mid <= n) { best = mid; // fits: remember it, and try bigger low = mid + 1;It fits. Remember it, and see if something bigger also fits.
} else { high = mid - 1; // too big: try smaller }Too big. Everything from here up is too big as well.
return best;The last candidate that fit is the answer.
7. How fast is it?
| Count up | Binary search | |
|---|---|---|
| Time | O(√n): 1.4 million steps for two trillion | O(log n): about 41 steps |
| Extra memory | O(1) | O(1) |
The big lesson: binary search doesn't need a list. Any time the answer is a number and you can check "is this big enough?", you can search for it.
Common mistakes
- Squares too big to be exact. JavaScript numbers are exact only up to about 9 quadrillion (
Number.MAX_SAFE_INTEGER). Forn= two trillion, the firstmidis a trillion, and its square is far past that limit. Here the comparison still comes out right, because that square is clearly bigger thann. In languages with fixed-size whole numbers it would overflow, so a safe habit is to comparemid <= n / midinstead. - Returning
loworhighwithout thinking. When the loop ends, which one is the answer? Rememberingbestavoids the guesswork.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Whole square root
Read the 15 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function wholeSquareRoot(n) { let low = 0; let high = n; let best = 0; while (low <= high) { const mid = Math.floor((low + high) / 2); if (mid * mid <= n) { best = mid; // fits: remember it, and try bigger low = mid + 1; } else { high = mid - 1; // too big: try smaller } } return best;}