FastPrepIntersection of Two Rectangles

Intersection of Two Rectangles

Figma logoFigma● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Each axis-aligned rectangle is represented as [left, bottom, right, top], where left < right and bottom < top.

Return the positive-area intersection of first and second in the same format. If they do not overlap with positive area, return an empty array. Rectangles that only touch along an edge or at one point have no positive-area intersection.

Function

intersectRectangles(first: int[], second: int[]) → int[]

Examples

Example 1

first = [0,0,4,4]second = [2,1,6,3]return = [2,1,4,3]

The common horizontal interval is [2, 4], and the common vertical interval is [1, 3].

Example 2

first = [-3,-2,5,7]second = [0,0,2,2]return = [0,0,2,2]

The second rectangle lies completely inside the first.

Example 3

first = [0,0,2,2]second = [2,0,5,3]return = []

The rectangles touch at the vertical edge x = 2 but share no positive area.

Constraints

  • first.length == second.length == 4.
  • -10^9 <= left < right <= 10^9.
  • -10^9 <= bottom < top <= 10^9.

More Figma problems

See Figma hiring insights
public int[] intersectRectangles(int[] first, int[] second) {
    // Write your code here.
}
first[0,0,4,4]
second[2,1,6,3]
expected[2,1,4,3]
Checking account…