Given a non-negative integer x
, return the square root of x
rounded down to the nearest integer. The returned integer should be non-negative as well.
You must not use any built-in exponent function or operator.
pow(x, 0.5)
in c++ or x ** 0.5
in python.Example 1:
Input: x = 4
Output: 2
Explanation: The square root of 4 is 2, so we return 2.
Example 2:
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
Constraints:
0 <= x <= 2^31 - 1
Can you implement a function to solve this problem in Python?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to find the square root of a number involves systematically checking possible answers. We essentially guess and check every whole number, starting from the smallest possible value, until we find one that, when squared, equals or gets very close to the given number. If the square of our guess exceeds the number, we know our previous guess was the closest integer square root.
Here's how the algorithm would work step-by-step:
def my_sqrt(number_to_square_root):
possible_square_root = 0
while True:
square_of_possible_root = possible_square_root * possible_square_root
# If perfect match, return the root
if square_of_possible_root == number_to_square_root:
return possible_square_root
# If square is too big, the previous number was the closest root.
if square_of_possible_root > number_to_square_root:
return possible_square_root - 1
# We haven't found it yet, so increment and continue
possible_square_root += 1
The most efficient way to find the square root of a number is to use a guessing game that gets smarter over time. Instead of checking every single number one by one, we make an initial guess and then repeatedly refine it to get closer and closer to the true square root.
Here's how the algorithm would work step-by-step:
def my_sqrt(x):
if x == 0:
return 0
initial_guess = x / 2.0
tolerance = 0.00001
while True:
squared_guess = initial_guess * initial_guess
# Check if our guess is close enough to the actual square root
if abs(squared_guess - x) < tolerance:
return int(initial_guess)
# If the guess is too high, adjust it to be lower
if squared_guess > x:
initial_guess = (initial_guess + (x / initial_guess)) / 2
else:
# If the guess is too low, adjust it to be higher
initial_guess = (initial_guess + (x / initial_guess)) / 2
#Updating according to Newton's method to reduce iterations
Case | How to Handle |
---|---|
x = 0 | Return 0 since the square root of 0 is 0. |
x = 1 | Return 1 since the square root of 1 is 1. |
x is a large perfect square that causes integer overflow if its true square root is calculated without precautions. | Use long integer arithmetic during binary search or other root-finding methods to prevent overflow when squaring mid values. |
x is very large (close to MAX_INT) | Handle this case correctly using binary search, as linear iteration would be inefficient. |
x = Integer.MAX_VALUE | Binary search should be implemented carefully to prevent overflow when calculating mid * mid. |
x is a perfect square, but very large | Binary search should quickly converge to the correct integer square root. |
x is a non-perfect square | The algorithm should correctly truncate the decimal part and return the integer part of the square root. |
Negative input for x | The problem description specifies non-negative integers, but handling negative input by either throwing an exception or returning 0 for invalid input enhances robustness. |