Sqrt(x)
Sqrt(x)
Difficulty: Easy · Topics: Math, Binary Search
Given a non-negative integer x, return the integer part of its square root, rounded down. The returned integer must also be non-negative.
You are explicitly forbidden from using any built-in exponent functions or operators (e.g., pow(x, 0.5) or x ** 0.5).
Example 1:
Input: x = 4 Output: 2 Explanation: The square root of 4 is 2.
Example 2:
Input: x = 8 Output: 2 Explanation: The square root of 8 is approximately 2.828. Rounded down to the nearest integer, this is 2.
Constraints:
0 <= x <= 2^31 - 1
Similar LeetCode problem: Sqrt(x)
AI interview prep
Core dilemma
Finding an efficient, non-brute-force method to compute an approximate mathematical value within integer constraints, without relying on built-in functions.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Sqrt(x)
Sqrt(x)
Difficulty: Easy · Topics: Math, Binary Search
Given a non-negative integer x, return the integer part of its square root, rounded down. The returned integer must also be non-negative.
You are explicitly forbidden from using any built-in exponent functions or operators (e.g., pow(x, 0.5) or x ** 0.5).
Example 1:
Input: x = 4 Output: 2 Explanation: The square root of 4 is 2.
Example 2:
Input: x = 8 Output: 2 Explanation: The square root of 8 is approximately 2.828. Rounded down to the nearest integer, this is 2.
Constraints:
0 <= x <= 2^31 - 1
Similar LeetCode problem: Sqrt(x)