Sqrt(x)
Updated
Problem statement
You are given a non-negative integer x. Return the square root of x rounded down to a whole number. In other words, return the largest integer r such that r * r <= x.
Do not use a built-in square root or power function. Work it out with whole-number arithmetic.
Function
mySqrt(x: int) → intExamples
Example 1
x = 4return = 22 * 2 = 4, which is not more than 4, and 3 * 3 = 9 is too big. So the answer is 2.
Example 2
x = 8return = 2The real square root of 8 is about 2.83. Rounded down, that is 2: 2 * 2 = 4 <= 8, but 3 * 3 = 9 > 8.
Example 3
x = 2147483647return = 46340This is the largest allowed x. 46340 * 46340 = 2147395600 is not more than x, but 46341 * 46341 = 2147488281 is. So the answer is 46340.
Constraints
0 <= x <= 2^31 - 1
More LinkedIn Phone Screen questions
- Maximum Product of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Maximum Sum of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Merge Two Sorted ListsPHONE SCREEN · Seen Nov 2026
- Total Years of Work ExperiencePHONE SCREEN · Seen Nov 2026
- Sum an Arbitrary-Dimensional ArrayPHONE SCREEN · Seen Oct 2026
- Known Words Matching Phone DigitsPHONE SCREEN · Seen Oct 2026
- Inverse-Depth Nested List SumPHONE SCREEN · Seen Oct 2026
- Valid Number with ExponentsPHONE SCREEN · Seen Oct 2026
- Sort a Quadratic-Transformed ArrayPHONE SCREEN · Seen Oct 2026
- Constant-Time Minimum StackPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Route Length From Two CyclistsJane Street · PHONE SCREENJane Street interview questions
- Pressure Chamber Linear InterpolationSpaceX · PHONE SCREENSpaceX interview questions
- When to Accept a Doubled BetSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Integer Square Root with Error ChecksBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Square Root FunctionCitadel · PHONE SCREENCitadel interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Convex Function MinimizationUber · PHONE SCREENUber interview questions
- Square Root with Newton IterationGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Minimum of a Convex FunctionRippling · PHONE SCREENRippling interview questions
- Minimum Full Glasses for an Exact Water VolumeMarshall Wace · PHONE SCREENMarshall Wace interview questions