Compute square root to 1 decimal
UberData Scientist · Technical ScreenLast updated
This question evaluates a candidate's understanding of numerical methods, floating-point precision, and implementation of approximate functions when producing a square root rounded to one decimal place.
Problem
Given a non-negative real number x, implement a function sqrt1dp(x) that returns (\sqrt{x}) rounded (or truncated—clarify with interviewer) to one digit after the decimal point.
Requirements
- Do not call a built-in square root function.
- Your answer must be accurate to 1 decimal place (e.g., error < 0.05 if rounding).
- Discuss how you would optimize the algorithm (time complexity and convergence).
Examples
x = 2→1.4x = 9→3.0x = 0→0.0
Clarifications to ask
- Rounding vs truncation to 1 decimal.
- Input range (e.g., up to 1e9?) and whether
xcan be non-integer. - Acceptable error tolerance if not using decimal formatting.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.