Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

836. Rectangle Overlap

Easy45.3% Acceptance
MathGeometry
Asked by:
S
ServiceNow
Facebook
ProblemSolutions (12)VideosCompanies (3)Notes

Problem Statement

An axis-aligned rectangle is represented 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. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.

Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.

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

Example 1:

Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true

Example 2:

Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false

Example 3:

Input: rec1 = [0,0,1,1], rec2 = [2,2,3,3]
Output: false

Constraints:

  • rec1.length == 4
  • rec2.length == 4
  • -109 <= rec1[i], rec2[i] <= 109
  • rec1 and rec2 represent a valid rectangle with a non-zero area.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
F
D
Docusign

Approach

The key idea in Rectangle Overlap is to determine whether two axis-aligned rectangles share a region with positive area. Each rectangle is typically represented using four values: [x1, y1, x2, y2], where (x1, y1) is the bottom-left corner and (x2, y2) is the top-right corner.

Instead of computing the overlapping region directly, a simpler approach is to check when rectangles do not overlap. Two rectangles fail to overlap if one is completely to the left, right, above, or below the other. By evaluating these boundary conditions using coordinate comparisons, we can quickly determine if an overlap exists.

This method relies purely on basic geometry and coordinate comparisons, making it extremely efficient. Since only a few constant-time comparisons are required, the algorithm runs in O(1) time and uses O(1) space. This makes it ideal for interview scenarios where clarity and correctness are important.

Complexity

ApproachTime ComplexitySpace Complexity
Boundary comparison using rectangle coordinatesO(1)O(1)

Video Solution Available

GeeksforGeeks

View all video solutions

Solutions (12)

Rectangle Non-Overlap Conditions

This approach checks if there are conditions that confirm non-overlapping rectangles. Specifically, two rectangles do not overlap if:

  • The right side of the first rectangle is to the left of the left side of the second rectangle.
  • The left side of the first rectangle is to the right of the right side of the second rectangle.
  • The top side of the first rectangle is below the bottom side of the second rectangle.
  • The bottom side of the first rectangle is above the top side of the second rectangle.

If any of these conditions are true, the rectangles do not overlap. Otherwise, they do overlap.

Time Complexity: O(1), since the operation involves a constant number of comparisons.
Space Complexity: O(1), since the space used by the function is constant with respect to inputs.

PythonC++JavaJavaScriptC#C
1def isRectangleOverlap(rec1, rec2):
2    return not (rec1[2] <= rec2[0] or  
3                rec1[0]

Explanation

The function checks whether any of the non-overlapping conditions are true. If none are true, it means the rectangles overlap.

Checking Overlap Using Intersection

This approach computes the potential intersecting rectangle and verifies if its calculated dimensions form a valid rectangle with a positive area. The logic involves:

  • Calculating the intersection rectangle as:
    • Left side: max(rec1[0], rec2[0])
    • Right side: min(rec1[2], rec2[2])
    • Bottom side: max(rec1[1], rec2[1])
    • Top side: min(rec1[3], rec2[3])
  • Checking if the intersection's width and height are both positive.

Time Complexity: O(1)
Space Complexity: O(1)

PythonC++JavaJavaScriptC#C
1

Video Solutions

Watch expert explanations and walkthroughs

Find if two rectangles overlap | GeeksforGeeks

GeeksforGeeks
5:1867,808 views

Asked By Companies

3 companies
S
ServiceNow
F
Facebook
D
Docusign

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Related Topics

MathGeometry

Problem Stats

Acceptance Rate45.3%
DifficultyEasy
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Rectangle Overlap asked in FAANG interviews?

Yes, variations of rectangle and interval overlap problems appear in technical interviews, including FAANG companies. They test understanding of geometry, boundary conditions, and logical reasoning with coordinates.

What data structure is best for Rectangle Overlap?

No special data structure is required for this problem. Simple variables representing the rectangle coordinates are enough to compare boundaries and determine whether an overlap exists.

What is the optimal approach for Rectangle Overlap?

The optimal approach checks whether two rectangles are separated horizontally or vertically. If none of the non-overlapping conditions are true, the rectangles must overlap with positive area. This solution only requires a few coordinate comparisons and runs in constant time.

How do you determine if two rectangles do not overlap?

Two rectangles do not overlap if one lies completely to the left, right, above, or below the other. These conditions can be checked by comparing the x and y boundaries of the rectangles.

>=
rec2
[
2
]
or
4
rec1
[
3
]
<=
rec2
[
1
]
or
5
rec1
[
1
]
>=
rec2
[
3
]
)
def
isRectangleOverlap
(
rec1
,
rec2
)
:
2
x_overlap
=
min
(
rec1
[
2
]
,
rec2
[
2
]
)
>
max
(
rec1
[
0
]
,
rec2
[
0
]
)
3
y_overlap
=
min
(
rec1
[
3
]
,
rec2
[
3
]
)
>
max
(
rec1
[
1
]
,
rec2
[
1
]
)
4
return
x_overlap
and
y_overlap

Explanation

The function calculates potential overlapping parts along both axes and ensures these parts form an area (both x and y overlap must be true).

Max Points on a LineHard
Rectangle AreaMedium
Self CrossingHard
Convex PolygonMedium
More similar problems