KatInterview

69. Sqrt(x)

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to compute the square root of a non-negative integer x and return the result rounded down to the nearest integer. We are explicitly forbidden from using built-in exponent functions like pow() or **.

Solution

Sqrt(x) in Python

class Solution:
    def mySqrt(self, x: int) -> int:
        # Handle edge cases for 0 and 1
        if x < 2:
            return x
        
        # Binary search for the integer square root
        left, right = 1, x
        while left <= right:
            # Use integer division
            mid = left + (right - left) // 2
            
            # In Python, integers have arbitrary precision, so mid*mid won't overflow.
            if mid * mid > x:
                right = mid - 1 # mid is too large
            else:
                left = mid + 1  # mid is potentially the answer, try for a larger one

        # At the end of the loop, `right` is the largest integer `k` such that k*k <= x.
        return right

Sqrt(x) in Java

class Solution {
    public int mySqrt(int x) {
        // Handle edge cases for 0 and 1
        if (x < 2) {
            return x;
        }

        // Binary search for the integer square root
        int left = 1, right = x;
        while (left <= right) {
            // Prevent (left + right) overflow
            int mid = left + (right - left) / 2;

            // Use division to prevent mid*mid overflow
            // If mid > x / mid, then mid*mid > x
            if (mid > x / mid) {
                right = mid - 1; // mid is too large
            } else {
                left = mid + 1; // mid is potentially the answer, try for a larger one
            }
        }

        // At the end of the loop, `right` is the largest integer `k` such that k*k <= x.
        return right;
    }
}

Sqrt(x) in C++

class Solution {
public:
    int mySqrt(int x) {
        // Handle edge cases for 0 and 1
        if (x < 2) {
            return x;
        }

        // Binary search for the integer square root
        int left = 1, right = x;
        while (left <= right) {
            // Prevent (left + right) overflow
            int mid = left + (right - left) / 2;
            
            // Use division to prevent mid*mid overflow
            // If mid > x / mid, then mid*mid > x
            if (mid > x / mid) {
                right = mid - 1; // mid is too large
            } else {
                left = mid + 1;  // mid is potentially the answer, try for a larger one
            }
        }
        
        // At the end of the loop, `right` is the largest integer `k` such that k*k <= x.
        return right;
    }
};

Sqrt(x) in TypeScript

function mySqrt(x: number): number {
    // Handle edge cases for 0 and 1
    if (x < 2) {
        return x;
    }

    // Binary search for the integer square root
    let left = 1, right = x;
    while (left <= right) {
        // Use Math.floor to ensure mid is an integer
        const mid = Math.floor(left + (right - left) / 2);

        // Use division to prevent potential precision issues with mid*mid for large numbers
        // If mid > x / mid, then mid*mid > x
        if (mid > x / mid) {
            right = mid - 1; // mid is too large
        } else {
            left = mid + 1;  // mid is potentially the answer, try for a larger one
        }
    }

    // At the end of the loop, `right` is the largest integer `k` such that k*k <= x.
    return right;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.