Integer Square Root
Updated
Problem statement
Given a non-negative integer x, return the square root of x rounded down to the nearest integer. In other words, return the largest integer r such that r * r <= x.
Do not use a built-in square root or power function. The judge checks only the returned value, so this rule is not checked automatically.
Function
mySqrt(x: int) → intExamples
Example 1
x = 10return = 33 * 3 = 9 is at most 10, but 4 * 4 = 16 is more than 10. The square root of 10 is about 3.16, and rounding down gives 3.
Example 2
x = 49return = 749 is a perfect square: 7 * 7 = 49, so the answer is exactly 7.
Example 3
x = 0return = 0The square root of 0 is 0.
Constraints
0 <= x <= 2^31 - 1
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Integer Square Root with Error ChecksBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Koko Eating BananasTekion · ONSITE INTERVIEWTekion interview questions
- Koko Eating BananasAmazon · ONSITE INTERVIEW
- Maximum Supplier ProfitExpedia · ONSITE INTERVIEWExpedia interview questions
- K-th Amount a Payout Machine Can GiveRippling · ONSITE INTERVIEWRippling interview questions
- First Register Overflow TickNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Two Eggs: Minimum Worst-Case DropsApple · ONSITE INTERVIEWApple interview questions
- Find a Function Root by BisectionJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Pressure Chamber Linear InterpolationSpaceX · PHONE SCREENSpaceX interview questions