Grokking Oracle Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Rectangle Overlap
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

You are given a rectangle as a list [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-right corner. The top and bottom edges of the rectangle are parallel to the X-axis, and the left and right edges are parallel to the Y-axis.

Two rectangles overlap if the rectangles share a part of the area; mere touching at edges or corners doesn't count.

Given two rectangles rec1 and rec2, return true if they overlap, otherwise return false.

Examples

  • Example 1:

    • Input: rec1 = [0, 0, 1, 1], rec2 = [1, 1, 2, 2]
    • Expected Output: false
    • Justification: The two rectangles touch at a corner but do not share any area, thus they do not overlap.
  • Example 2:

    • Input: rec1 = [0, 0, 2, 2], rec2 = [1, 1, 3, 3]
    • Expected Output: true
    • Justification: The rectangles share a part of their area, thus they overlap.
  • Example 3:

    • Input: rec1 = [1, 1, 3, 3], rec2 = [2, 2, 4, 4]
    • Expected Output: true
    • Justification: These rectangles share a central area, thereby confirming overlap.

Solution

To solve this problem, we approach it by checking the conditions under which rectangles do not overlap, which simplifies the problem significantly. We know that if one rectangle is to the left, right, above, or below the other, they do not overlap. This method is effective because it reduces the problem to simple comparisons of the x and y coordinates of the rectangles' corners.

Our approach involves checking if the maximum of the bottom-left x-coordinates is less than the minimum of the top-right x-coordinates and if the maximum of the bottom-left y-coordinates is less than the minimum of the top-right y-coordinates. If both conditions are true, the rectangles overlap. This method is both simple and efficient, avoiding unnecessary calculations and complexities involved in area computation or edge/corner cases.

Step-by-Step Algorithm

  1. Check Left: Verify if the right edge (x2 value) of the first rectangle (rec1) is to the left of the left edge (x1 value) of the second rectangle (rec2). This is done by comparing if rec1[2] <= rec2[0].

  2. Check Right: Determine if the left edge (x1 value) of rec1 is to the right of the right edge (x2 value) of rec2. This involves checking if rec1[0] >= rec2[2].

  3. Check Below: Check if the top edge (y2 value) of rec1 is below the bottom edge (y1 value) of rec2 by comparing if rec1[3] <= rec2[1].

  4. Check Above: Ascertain if the bottom edge (y1 value) of rec1 is above the top edge (y2 value) of rec2. This is done by verifying if rec1[1] >= rec2[3].

  5. Return Result: If none of the above conditions are true, it means that rec1 and rec2 overlap. Return true in this case. If any of the conditions are met, it implies that the rectangles do not overlap; hence, return false.

Algorithm Walkthrough with Example

Given rectangles rec1 = [1, 1, 3, 3] and rec2 = [2, 2, 4, 4]:

  1. Check Left: Compare if the right edge of rec1 (3) is to the left of the left edge of rec2 (2). This is not true since 3 is not less than or equal to 2.

  2. Check Right: Determine if the left edge of rec1 (1) is to the right of the right edge of rec2 (4). This condition is false because 1 is not greater than or equal to 4.

  3. Check Below: Check if the top edge of rec1 (3) is below the bottom edge of rec2 (2). This is false since 3 is not less than or equal to 2.

  4. Check Above: Ascertain if the bottom edge of rec1 (1) is above the top edge of rec2 (4). This condition is also false as 1 is not greater than or equal to 4.

  5. Return Result: Since none of the conditions for being to the left, right, above, or below are met, the rectangles must overlap. Therefore, for rec1 = [1, 1, 3, 3] and rec2 = [2, 2, 4, 4], the algorithm returns true, indicating that the rectangles overlap.

Code

Python3
Python3

. . . .

Complexity Analysis

  • Time Complexity: O(1)

    • The time complexity of this algorithm is constant, O(1), because the computation to determine if two rectangles overlap involves a fixed number of comparisons. No matter the input size or the specific values of the rectangle coordinates, the number of operations remains the same.
  • Space Complexity: O(1)

    • The space complexity is also constant, O(1), as the algorithm operates directly on the input parameters without utilizing any additional data structures that scale with input size.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible