Integer Square Root
Return the greatest integer whose square is at most the nonnegative target. The values list is unused.
Return the greatest integer whose square is at most the nonnegative target. The values list is unused.
Input and constraints
- Target must be nonnegative.
- Use at most 32 integers. Each numeric input is between -1000 and 1000.
Code inspector
def solve(values, target): low = 0 high = target best = 0 while (low <= high): mid = (low + ((high - low) // 2)) if ((mid * mid) <= target): best = mid low = (mid + 1) else: high = (mid - 1) return bestVisualizer
Step 1 of 6Empty array
Inspect the current state
Test candidate root 19 against the tile budget.
Variable watch
Step explanation
Test candidate root 19 against the tile budget.
Worked explanation
Find the widest square tiled floor you can cover with a fixed number of tiles.
Guided solution
Square a candidate root; a square within budget makes every smaller root feasible too.
O(log(t + 2)) time · O(1) auxiliary space
Examples
Worked example · {"values":[],"target":38} ⇒ 6
Edge case 1 · {"values":[],"target":0} ⇒ 0
Edge case 2 · {"values":[],"target":1} ⇒ 1
Progressive hints
Hint 1
Find the widest square tiled floor you can cover with a fixed number of tiles.Hint 2
Square a candidate root; a square within budget makes every smaller root feasible too.Hint 3
Step through the worked example and inspect the highlighted variables before each update.Common mistake
Exact equality is unnecessary for numbers between consecutive squares.
Interview follow-up
How would you avoid overflow for very large integer targets?