Problem: Total Area of Overlapping Rectangles

You are given two rectangles situated on a 2-D coordinate plane, which may overlap. Your task is to compute the total area covered by these rectangles.

Input:
The first line of input holds an integer 'T', indicating the number of test cases.
For each test case:
First line: Four integers 'x1', 'y1', 'x2', 'y2' - top-left and bottom-right coordinates of the first rectangle.
Second line: Four integers 'x3', 'y3', 'x4', 'y4' - top-left and bottom-right coordinates of the second rectangle.
Output:
Return an integer for each test case, representing the total area covered by the two rectangles.
Example:

Assuming rectangle coordinates and scenarios are illustrated with examples, as depicted in accompanying diagrams (not displayed here).

Constraints:

  • 1 ≤ T ≤ 10^5
  • -10^9 ≤ x1, y1, x2, y2 ≤ 10^9
  • x1 < x2, x3 < x4
  • y1 > y2, y3 > y4
  • Time Limit: 1 second
Note:
Edges of rectangles are aligned parallel to the X and Y axes. 
Be the first one to answer
Add answer anonymously...
SAP Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter