PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Uber

Implement sqrt with Newton vs binary search

Last updated: Mar 29, 2026

Quick Overview

This question evaluates numerical methods and algorithmic robustness, testing understanding of Newton’s method for root-finding, floating‑point stability and IEEE‑754 edge cases, and integer binary-search techniques for exact floor square roots within the Coding & Algorithms domain for a Data Scientist role.

  • hard
  • Uber
  • Coding & Algorithms
  • Data Scientist

Implement sqrt with Newton vs binary search

Company: Uber

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Implement two Python functions: (1) sqrt_newton(x, tol=1e-12) returning the principal square root for float x ≥ 0, without using sqrt/pow; handle x ∈ [0, 1e308], return 0 for x=0, +∞ for x=+∞, and raise ValueError for x<0. Choose a numerically stable initial guess that avoids overflow/underflow for very large/small x, and stop when |y^2−x|/max(x,1) ≤ tol or after 100 iterations. Ensure no division-by-zero and discuss how to guard against catastrophic cancellation. (2) isqrt_bisect(n) returning ⌊√n⌋ for integer n with 0 ≤ n < 2^63, using binary search with O(log n) iterations and no intermediate overflow (e.g., compare mid ≤ n//mid instead of mid*mid ≤ n). Then answer: a) Prove Newton’s method for √x is quadratically convergent near the root; b) For worst-case x in [1e−12, 1e12] with y0 = max(1, x), estimate the iterations to reach tol and justify; c) Compare per-iteration cost and total operations versus binary search for 64-bit n; d) Briefly outline how you would extend sqrt_newton to support complex results for x<0 under IEEE‑754 and why naive branching can be unsafe.

Quick Answer: This question evaluates numerical methods and algorithmic robustness, testing understanding of Newton’s method for root-finding, floating‑point stability and IEEE‑754 edge cases, and integer binary-search techniques for exact floor square roots within the Coding & Algorithms domain for a Data Scientist role.

Related Interview Questions

  • Maximize Throughput and Count Trigger Components - Uber (medium)
  • Replace Dashes With Nearest Letters - Uber (medium)
  • Find Earliest Column With One - Uber (easy)
  • Solve Wonderful Strings and Grid Queries - Uber (hard)
  • Count Islands After Land Additions - Uber (medium)
Uber logo
Uber
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Coding & Algorithms
6
0

Implement numerically robust square-root routines and analyze convergence

Task 1 — sqrt_newton(x, tol=1e-12)

Implement a Python function that returns the principal square root of a nonnegative float x without using sqrt or pow.

Requirements:

  • Input domain: x ∈ [0, 1e308].
  • Special cases: return 0.0 for x = 0; return +∞ for x = +∞; raise ValueError for x < 0.
  • Numerical stability: choose an initial guess that avoids overflow/underflow for very large/small x. Do not use sqrt/pow.
  • Iteration: use Newton's method and stop when |y^2 − x| / max(x, 1) ≤ tol or after 100 iterations.
  • Robustness: avoid division by zero and guard against catastrophic cancellation in the stopping test.

Task 2 — isqrt_bisect(n)

Implement a Python function that returns ⌊√n⌋ for an integer n with 0 ≤ n < 2^63 using binary search.

Requirements:

  • Complexity: O(log n) iterations.
  • No intermediate overflow: compare mid ≤ n // mid instead of mid*mid ≤ n.

Theory

Answer the following:

a) Prove Newton’s method for √x is quadratically convergent near the root.

b) For worst-case x in [1e−12, 1e12] with starting guess y0 = max(1, x), estimate the number of iterations needed to reach tol = 1e−12 (per the stop rule) and justify.

c) Compare per-iteration cost and total operations of Newton’s method versus binary search for 64-bit n.

d) Briefly outline how to extend sqrt_newton to support complex results for x < 0 under IEEE‑754 and why naïve branching can be unsafe.

Solution

Show

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Uber•More Data Scientist•Uber Data Scientist•Uber Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ 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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.