PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's understanding of numerical methods for root finding, floating-point precision control, and algorithmic time complexity analysis when computing square roots without built-in functions.

  • hard
  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Compute square root with precision

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Given a non-negative integer `val` and a non-negative integer `precise`, compute the square root of `val` without using any built-in square-root function. Return a result whose absolute error is at most `10^-precise`. Also explain the time complexity of your approach.

Quick Answer: This question evaluates a candidate's understanding of numerical methods for root finding, floating-point precision control, and algorithmic time complexity analysis when computing square roots without built-in functions.

Given two non-negative integers `val` and `precise`, compute the square root of `val` without using any built-in square-root operation. Return the result truncated to exactly `precise` digits after the decimal point as a float. This guarantees that the absolute error is less than `10^-precise`. Be prepared to explain the time complexity of your approach.

Constraints

  • 0 <= val <= 10^12
  • 0 <= precise <= 6
  • Do not use any built-in square-root function

Examples

Input: (10, 3)

Expected Output: 3.162

Explanation: `sqrt(10) ≈ 3.162277...`, and truncating to 3 decimal places gives `3.162`.

Input: (2, 5)

Expected Output: 1.41421

Explanation: `sqrt(2) ≈ 1.414213...`, and truncating to 5 decimal places gives `1.41421`.

Hints

  1. Binary search works because if `x*x <= val`, then every number smaller than `x` also satisfies the condition.
  2. To avoid floating-point precision issues, scale the problem: finding `sqrt(val)` to `precise` decimal places is equivalent to finding the integer square root of `val * 10^(2 * precise)`.
Last updated: May 4, 2026

Loading coding console...

PracHub

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

Product

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

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

  • Remove Duplicate Letters Lexicographically - Bytedance (medium)
  • Elements Occurring More Than n/3 Times in a Sorted Array - Bytedance (medium)
  • Least Frequently Used (LFU) Cache - Bytedance (hard)
  • Course Schedule Feasibility - Bytedance (hard)
  • Find the Best Word for a Query Suffix - Bytedance (hard)