Bird
Raised Fist0
Interview Prepmath-geometrymediumAmazonGoogle

Minimum Area Rectangle

Choose your preparation mode4 modes available
📋
Problem

Imagine you have a set of points representing trees in a park, and you want to build the smallest rectangular fence that encloses four of them as corners. How do you find that rectangle with minimum area?

Given a set of points in the 2D plane, find the minimum area of any rectangle formed by four of these points with sides parallel to the x and y axes. If no such rectangle exists, return 0. Input: An array of points where each point is represented as [x, y]. Output: The minimum area of a rectangle formed by any four points, or 0 if none exists.

1 ≤ n ≤ 10^50 ≤ x, y ≤ 10^9All points are distinct
Edge cases: No rectangle possible → output 0All points on a single line → output 0Multiple rectangles → output minimum area among them
</>
IDE
def minAreaRect(points: list[list[int]]) -> int:public int minAreaRect(int[][] points)int minAreaRect(vector<vector<int>>& points)function minAreaRect(points)
def minAreaRect(points):
    # Write your solution here
    pass
class Solution {
    public int minAreaRect(int[][] points) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int minAreaRect(vector<vector<int>>& points) {
    // Write your solution here
    return 0;
}
function minAreaRect(points) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: 0Failing to detect any rectangle because code only checks adjacent points or misses verifying all four corners.✅ For each pair of points as diagonal, check if the other two corners exist in the point set before calculating area.
Wrong: Area too small or incorrect due to integer overflowUsing 32-bit integers for area calculation with large coordinates causes overflow.✅ Use 64-bit integers or Python's arbitrary precision integers for area calculation.
Wrong: Non-zero output for empty or single point inputNo guard clause for inputs with fewer than 4 points.✅ Return 0 immediately if points length < 4.
Wrong: Non-zero output for colinear pointsNot verifying that rectangles require at least two distinct x and y coordinates.✅ Check that points have at least two distinct x and y values before calculating area.
Wrong: TLE on large inputsUsing brute force O(n^4) or O(n^3) approach instead of O(n^2) hash set diagonal pair method.✅ Implement O(n^2) solution using hash sets and diagonal pair checks.
✓
Test Cases
t1_01basic
Input{"points":[[1,1],[1,3],[3,1],[3,3],[2,2]]}
Expected4

The rectangle formed by points (1,1), (1,3), (3,1), and (3,3) has area 4, which is the minimum.

t1_02basic
Input{"points":[[0,0],[0,2],[2,0],[2,2],[1,1],[3,3]]}
Expected4

The rectangle with corners (0,0), (0,2), (2,0), and (2,2) has area 4, which is the minimum rectangle area.

t2_01edge
Input{"points":[]}
Expected0

Empty input means no points, so no rectangle can be formed; output is 0.

t2_02edge
Input{"points":[[1,1]]}
Expected0

Only one point means no rectangle can be formed; output is 0.

t2_03edge
Input{"points":[[0,0],[1,0],[2,0],[3,0],[4,0]]}
Expected0

All points lie on a single horizontal line; no rectangle can be formed; output is 0.

t3_01corner
Input{"points":[[1,1],[1,4],[4,1],[4,4],[2,2],[3,3]]}
Expected9

Minimum rectangle is formed by points (1,1), (1,4), (4,1), (4,4) with area 9; points (2,2) and (3,3) do not form smaller rectangle.

t3_02corner
Input{"points":[[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]]}
Expected4

Multiple rectangles possible; minimum area rectangle is (1,1),(1,3),(3,1),(3,3) with area 4.

t3_03corner
Input{"points":[[0,0],[0,1000000000],[1000000000,0],[1000000000,1000000000]]}
Expected{"high":232830643,"low":-1486618624,"unsigned":false}

Rectangle formed by points at extreme coordinates has area 10^18, testing integer overflow handling.

t4_01performance
Input{"_description":"n=100000 at constraint boundary - executor generates this input"}
⏱ Performance - must finish in 2000ms

Input size n=100000 tests O(n^2) or worse algorithms; must complete within 2 seconds.

Practice

(1/5)
1. Consider the following code snippet that checks if a number is a palindrome by reversing half of its digits. What is the value of reversed_half after the second iteration of the while loop when input is 12321?
easy
A. 12
B. 1
C. 21
D. 123

Solution

  1. Step 1: Trace first iteration

    Initial x=12321, reversed_half=0 reversed_half = 0*10 + 12321%10 = 1 x = 12321 // 10 = 1232
  2. Step 2: Trace second iteration

    reversed_half = 1*10 + 1232%10 = 10 + 2 = 12 x = 1232 // 10 = 123
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    After two iterations reversed_half is 12 [OK]
Hint: Track variable updates carefully each loop iteration [OK]
Common Mistakes:
  • Off-by-one iteration count
  • Mixing x and reversed_half values
  • Incorrect modulo or division
2. What is the time complexity of the optimized Sieve of Eratosthenes algorithm that counts primes less than n by marking multiples of odd primes only, and why?
medium
A. O(n * sqrt(n)) because each number is checked against primes up to sqrt(n)
B. O(n) because each number is visited only once during marking
C. O(n log n) because each prime's multiples are marked up to n
D. O(n log log n) because the sieve marks multiples efficiently up to sqrt(n)

Solution

  1. Step 1: Identify outer loop complexity

    The outer loop runs over odd numbers up to sqrt(n), roughly O(sqrt(n)/2).
  2. Step 2: Analyze inner marking loop

    Each composite number is marked only once by its smallest prime factor, leading to total marking cost O(n log log n).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Classic sieve time complexity is O(n log log n) [OK]
Hint: Sieve complexity is famously O(n log log n) [OK]
Common Mistakes:
  • Confusing with brute force O(n sqrt(n))
  • Assuming linear O(n) due to single visits
  • Overestimating to O(n log n)
3. What is the time complexity of the optimal happy number detection algorithm that uses a fixed set of known unhappy cycle numbers and repeatedly computes the sum of squares of digits until reaching 1 or a cycle?
medium
A. O(n^2) due to repeated digit extraction and sum computations
B. O(n) where n is the input number
C. O(k * log n) where k is the number of iterations until termination and log n is digits count
D. O(1) constant time because the unhappy cycle set is fixed

Solution

  1. Step 1: Identify operations per iteration

    Each iteration computes sum of squares of digits, which takes O(log n) time since number of digits is proportional to log n.
  2. Step 2: Count number of iterations

    Number of iterations k until reaching 1 or cycle is variable but bounded; total time is O(k * log n).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Time depends on iterations and digit count, not linear or constant [OK]
Hint: Sum of digit squares costs O(log n) per iteration [OK]
Common Mistakes:
  • Assuming linear O(n) time
  • Confusing fixed cycle set with constant time
  • Mistaking digit extraction cost
4. Consider the following buggy code for finding the nth ugly number. Which line contains the subtle bug that causes incorrect results for some inputs?
import heapq

def nthUglyNumber(n):
    heap = [1]
    seen = set()
    ugly = 1
    for _ in range(n):
        ugly = heapq.heappop(heap)
        for factor in [2, 3, 5]:
            new_ugly = ugly * factor
            if new_ugly not in seen:
                seen.add(new_ugly)
                heapq.heappush(heap, new_ugly)
    return ugly
medium
A. Line 3: Initializing seen as an empty set instead of containing 1
B. Line 6: Popping from heap before checking if heap is empty
C. Line 9: Multiplying ugly by factor without checking for overflow
D. Line 11: Adding new_ugly to seen before pushing to heap

Solution

  1. Step 1: Check initialization of seen set

    The seen set is empty initially, but 1 is already in the heap. This causes duplicates because 1 is not marked as seen.
  2. Step 2: Understand impact of missing 1 in seen

    Without 1 in seen, the algorithm may push 1 multiple times, causing duplicates and incorrect nth ugly number.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Adding 1 to seen fixes duplicates and ensures correctness [OK]
Hint: Always mark initial heap elements as seen to avoid duplicates [OK]
Common Mistakes:
  • Forgetting to add initial element to seen set
  • Assuming heap uniqueness without seen tracking
  • Ignoring edge cases with initial values
5. Suppose you want to compute the LCM of multiple numbers (more than two). Which of the following approaches correctly extends the iterative Euclidean algorithm and avoids overflow?
hard
A. Multiply all numbers together first, then divide by the GCD of all numbers at the end.
B. Compute LCM pairwise from left to right using lcm_safe(a, b) repeatedly on the list.
C. Use brute force to find the smallest number divisible by all inputs by checking multiples of the max number.
D. Compute GCD of all numbers first, then multiply all numbers and divide by that GCD.

Solution

  1. Step 1: Understand LCM of multiple numbers

    LCM(a,b,c) = LCM(LCM(a,b), c). Computing pairwise LCMs iteratively is standard and safe.
  2. Step 2: Evaluate options

    Multiplying all numbers first risks overflow. Brute force is inefficient. Computing GCD of all and dividing product is incorrect because GCD of all does not directly give LCM.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Pairwise LCM with overflow-safe method is correct and efficient [OK]
Hint: Compute LCM pairwise to avoid overflow and maintain correctness [OK]
Common Mistakes:
  • Multiplying all numbers first causing overflow
  • Misusing GCD of all numbers to compute LCM
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard