PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Uber
  • Coding & Algorithms
  • Data Scientist

Compute square root to 1 decimal

Company: Uber

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

## 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.4` - `x = 9` → `3.0` - `x = 0` → `0.0` ### Clarifications to ask - Rounding vs truncation to 1 decimal. - Input range (e.g., up to 1e9?) and whether `x` can be non-integer. - Acceptable error tolerance if not using decimal formatting.

Quick Answer: 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.

Given a non-negative real number `x`, implement `sqrt1dp(x)` that returns the square root of `x` **rounded to one digit after the decimal point**, WITHOUT calling any built-in square-root function (no `math.sqrt`, `Math.sqrt`, `**0.5`, `pow(x, 0.5)`, etc.). The result must be accurate to 1 decimal place (rounding error < 0.05). **Approach:** Use binary search (or Newton's method) to converge on the root, then round to one decimal. Binary search over the interval `[0, max(1, x)]`: each iteration halves the candidate window, so `~60` iterations already drives the error far below `1e-9`. Newton's iteration `g = (g + x/g) / 2` converges quadratically and is even faster. **Examples:** - `sqrt1dp(2)` → `1.4` - `sqrt1dp(9)` → `3.0` - `sqrt1dp(0)` → `0.0` - `sqrt1dp(10)` → `3.2`

Constraints

  • 0 <= x <= 1e9
  • x may be a non-integer real number
  • Do not call any built-in square-root function
  • Result must be accurate to 1 decimal place (error < 0.05)

Examples

Input: (2,)

Expected Output: 1.4

Explanation: sqrt(2) = 1.41421..., rounds to 1.4

Input: (9,)

Expected Output: 3.0

Explanation: Perfect square: sqrt(9) = 3.0

Input: (0,)

Expected Output: 0.0

Explanation: Edge case: sqrt(0) = 0.0

Input: (1,)

Expected Output: 1.0

Explanation: sqrt(1) = 1.0

Input: (16,)

Expected Output: 4.0

Explanation: Perfect square: sqrt(16) = 4.0

Input: (0.25,)

Expected Output: 0.5

Explanation: Non-integer < 1: sqrt(0.25) = 0.5 (root exceeds the input)

Input: (100,)

Expected Output: 10.0

Explanation: sqrt(100) = 10.0

Input: (2.25,)

Expected Output: 1.5

Explanation: Non-integer input: sqrt(2.25) = 1.5

Input: (1000000,)

Expected Output: 1000.0

Explanation: Large input: sqrt(1000000) = 1000.0

Input: (10,)

Expected Output: 3.2

Explanation: sqrt(10) = 3.16227..., rounds to 3.2

Hints

  1. The root of x lies in [0, max(1, x)] — note that for 0 < x < 1 the root is GREATER than x, which is why the upper bound is max(1, x), not x.
  2. Binary search: pick mid; if mid*mid < x the root is higher, else it is lower. Each iteration halves the error, so a fixed iteration count (e.g. 100-200) guarantees far better than 1-decimal precision.
  3. Newton's method converges faster: start with a guess g and repeat g = (g + x/g) / 2 until it stops changing.
  4. Only round to 1 decimal AFTER converging to full precision — rounding early loses accuracy. Use round(root, 1).
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Deep Equality of Two Records - Uber (medium)
  • Shortest Path in a Grid with Blocked Cells - Uber (medium)
  • Design and Implement an LRU Cache - Uber (medium)
  • Reconstruct the Alphabet Order of an Alien Language - Uber (medium)
  • Maximize Throughput and Count Trigger Components - Uber (medium)