Integer Square Root with Error Checks
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Bloomberg OA reported in July 2020 asks for an integer square root, and the trap isn't the math. It's the negative input and the overflow when you square a candidate near 2^31. A naive mid * mid blows past a 32-bit int and flips the comparison. If you've got an assessment coming, this one looks easy and punishes sloppy bounds. The clean answer is binary search on the answer, with the overflow guarded. StealthCoder sits invisibly as a safety net on the live OA if you blank on the boundaries, but the pattern here is short enough to carry in your head.
The problem
Return floor(sqrt(x)) when x is nonnegative. Return -1 when x is negative. Do not use a library square-root function, and avoid overflow when comparing a candidate square with x. Function integerSqrt(x: int) → int Examples Example 1 x = 8 return = 2 The square root is between 2 and 3, so its floor is 2. Example 2 x = -4 return = -1 Negative input uses the documented sentinel. Constraints -2^31 <= x <= 2^31 - 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is binary search over candidate roots from 0 to x. Check the negative case first and return -1 before anything else. Then find the largest mid where mid * mid <= x. The pitfall is overflow. In a fixed-width 32-bit language, mid * mid can exceed 2^31 - 1 and wrap negative, so you accept a bad candidate. Fix it by comparing mid <= x / mid, or by using a 64-bit type for the product. Also watch x = 0 and x = 1, where a lazy loop bound or division by zero bites you. Compute mid as lo + (hi - lo) / 2 to avoid a second overflow. Return hi when the loop ends, since that's the floor. If you freeze on the loop invariants during the live OA, StealthCoder gives you a working version to check against, but the logic is only about ten lines.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Integer Square Root with Error Checks cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as sqrtx. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Integer Square Root with Error Checks FAQ
What's the trick in the Bloomberg integer square root problem?+
Binary search the answer between 0 and x, keeping the largest mid where mid * mid <= x. Handle negative x first by returning -1. The real trick is avoiding overflow, so compare mid <= x / mid or use a 64-bit product instead of squaring in 32 bits.
How do I avoid overflow without a bigger integer type?+
Use division instead of multiplication. Check mid <= x / mid for mid greater than 0. That keeps every value inside the 32-bit range. Also compute the midpoint as lo + (hi - lo) / 2 so the sum of bounds can't overflow either.
Which edge cases should I test before submitting?+
Test x = -4 returning -1, x = 0, x = 1, x = 8 returning 2, a perfect square like 16, and x = 2^31 - 1. The max value is the one that catches overflow bugs. Also check the number just below a perfect square to confirm you floor correctly.
Can I use Newton's method instead of binary search?+
Yes, it converges fast, but it's riskier here. You need careful integer division and a correct stopping condition, and overflow still lurks in the initial guess. Binary search is easier to get right under pressure, so stick with it unless you've done Newton's method recently.
How should I prepare for this in 48 hours?+
Write the binary search from memory twice, once with the division comparison and once with a 64-bit product. Run the edge cases by hand. This pattern shows up as a building block in many problems, so the loop invariant matters more than memorizing this exact question.