KatInterview

492. Construct the Rectangle

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 core of the problem is to find two integers, L (length) and W (width), that are factors of a given area. These factors must satisfy three conditions:

Solution

Construct the Rectangle in Python

class Solution:
    def constructRectangle(self, area: int) -> List[int]:
        # Start searching for the width W from the largest possible value.
        # From L >= W, we have W*W <= L*W = area, so W <= sqrt(area).
        # To minimize L-W, L and W should be as close as possible,
        # which means we should find the largest W that is a divisor of area.
        w = int(area**0.5)
        
        # Loop downwards from sqrt(area) to find the first divisor.
        # This will be the largest W <= sqrt(area) that divides area.
        while area % w != 0:
            w -= 1
        
        # Once W is found, L is simply area / W.
        l = area // w
        
        # Return the length and width as a list.
        return [l, w]

Construct the Rectangle in Java

class Solution {
    public int[] constructRectangle(int area) {
        // Start searching for the width W from the largest possible value.
        // From L >= W, we have W*W <= L*W = area, so W <= sqrt(area).
        // To minimize L-W, L and W should be as close as possible,
        // which means we should find the largest W that is a divisor of area.
        int w = (int) Math.sqrt(area);
        
        // Loop downwards from sqrt(area) to find the first divisor.
        // This will be the largest W <= sqrt(area) that divides area.
        while (area % w != 0) {
            w--;
        }
        
        // Once W is found, L is simply area / W.
        int l = area / w;
        
        // Return the length and width as an array.
        return new int[]{l, w};
    }
}

Construct the Rectangle in C++

class Solution {
public:
    vector<int> constructRectangle(int area) {
        // Start searching for the width W from the largest possible value.
        // From L >= W, we have W*W <= L*W = area, so W <= sqrt(area).
        // To minimize L-W, L and W should be as close as possible,
        // which means we should find the largest W that is a divisor of area.
        int w = static_cast<int>(sqrt(area));
        
        // Loop downwards from sqrt(area) to find the first divisor.
        // This will be the largest W <= sqrt(area) that divides area.
        while (area % w != 0) {
            w--;
        }
        
        // Once W is found, L is simply area / W.
        int l = area / w;
        
        // Return the length and width as a vector.
        return {l, w};
    }
};

Construct the Rectangle in TypeScript

function constructRectangle(area: number): number[] {
    // Start searching for the width W from the largest possible value.
    // From L >= W, we have W*W <= L*W = area, so W <= sqrt(area).
    // To minimize L-W, L and W should be as close as possible,
    // which means we should find the largest W that is a divisor of area.
    let w = Math.floor(Math.sqrt(area));

    // Loop downwards from sqrt(area) to find the first divisor.
    // This will be the largest W <= sqrt(area) that divides area.
    while (area % w !== 0) {
        w--;
    }

    // Once W is found, L is simply area / W.
    const l = area / w;

    // Return the length and width as an array.
    return [l, w];
};

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.