Square Root with Newton Iteration
Updated
Problem statement
You are given a real number x that is zero or positive. Return its square root, which is also zero or positive. Compute it with Newton iteration.
Your answer is accepted if it is off from the true root by at most 10^-9 * max(1, sqrt(x)). If x is zero, return zero.
Do not round the root down to a whole number. When the root has a fractional part, keep it.
Function
newtonSquareRoot(x: double) → doubleExamples
Example 1
x = 2.0return = 1.4142135623730951The square root of 2 is approximately 1.4142135623730951.
Example 2
x = 0.25return = 0.5The fractional input 0.25 has square root 0.5.
Example 3
x = 0.0return = 0.0Zero has square root zero.
Constraints
x = 0or10^-12 <= x <= 10^12.- Input and output use double-precision floating-point values.
More Goldman Sachs Phone Screen questions
- House Robber IIIPHONE SCREEN · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count Staircase WaysPHONE SCREEN · Seen Oct 2026
- Is a Power of TenPHONE SCREEN · Seen Oct 2026
- Minimum Traveling Salesperson TourPHONE SCREEN · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Root of the Largest TreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- 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
- Minimum of a Convex FunctionRippling · PHONE SCREENRippling interview questions
- Minimum Full Glasses for an Exact Water VolumeMarshall Wace · PHONE SCREENMarshall Wace interview questions
- Count Centaur Friend RequestsMeta · PHONE SCREENMeta interview questions
- Percentile and Boundary QueriesDatadog · PHONE SCREENDatadog interview questions
- Categorical Sampling with Recorded DrawsMicrosoft · PHONE SCREENMicrosoft interview questions
- Sure Profit on a Horse RaceSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Sqrt(x)LinkedIn · PHONE SCREENLinkedIn interview questions