Compute a square-root approximation with a maintained search interval and explicit precision convention. Explain overflow-aware comparisons, floating-point stopping, and why integer input differs from an integer-floor output request.
Approximate a Square Root and Clarify the Integer Follow-up
Company: Citadel Securities
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Write a function that computes the square root of a nonnegative number using basic arithmetic and comparisons. Then explain what changes if the input is guaranteed to be an integer.
### Constraints and Clarifications
The report does not specify precision, numeric bounds, or whether the integer follow-up changes the output. For the base practice contract, accept a finite nonnegative real input x, do not call a built-in square-root routine, and return a numerical approximation with absolute error at most1e-9*max(1,sqrt(x)). This tolerance is an explicit practice convention. Numeric representation and a portable finite test domain require later console validation; no unreported maximum is invented. Integer input still has a real square root unless the interviewer explicitly asks for floor(sqrt(n)).
### What a Strong Answer Covers
A convergent search interval, overflow-aware comparisons, stopping and floating-point stagnation, and complexity in terms of precision. Discuss the distinct integer-floor variant without silently changing the base return value.
### Follow-up Questions
- Why is sqrt(2) not an integer even though2 is an integer?
- How can an integer-floor implementation compare a candidate square without multiplication overflow?
Overview: Compute a square-root approximation with a maintained search interval and explicit precision convention. Explain overflow-aware comparisons, floating-point stopping, and why integer input differs from an integer-floor output request.
Write a function that computes the square root of a nonnegative number using basic arithmetic and comparisons. Then explain what changes if the input is guaranteed to be an integer.
Constraints and Clarifications
The report does not specify precision, numeric bounds, or whether the integer follow-up changes the output. For the base practice contract, accept a finite nonnegative real input x, do not call a built-in square-root routine, and return a numerical approximation with absolute error at most1e-9*max(1,sqrt(x)). This tolerance is an explicit practice convention. Numeric representation and a portable finite test domain require later console validation; no unreported maximum is invented. Integer input still has a real square root unless the interviewer explicitly asks for floor(sqrt(n)).
What a Strong Answer Covers Guidance
A convergent search interval, overflow-aware comparisons, stopping and floating-point stagnation, and complexity in terms of precision. Discuss the distinct integer-floor variant without silently changing the base return value.
Follow-up Questions Guidance
Why is sqrt(2) not an integer even though2 is an integer?
How can an integer-floor implementation compare a candidate square without multiplication overflow?