FastPrepOverlap Area of Two Rectangles
Problems/Overlap Area of Two Rectangles

Overlap Area of Two Rectangles

Nutanix logoNutanix● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem comes from a Nutanix intern interview report about a one-hour on-campus round. The second of its two coding questions asks how much two axis-aligned rectangles overlap. Each rectangle is described by two opposite corners (bottom-left and top-right).

The report gives no range for the coordinates and does not say whether they are whole numbers. It has no rule for rectangles that only touch, and shows no examples.

Here we chose the following. Corners are whole numbers from -10^4 to 10^4. Every rectangle has a positive width and height. Rectangles that meet only along a line segment or at a single point share an area of 0. The function returns the area as an integer. All examples and tests are our own.

Our version is about 90% the same as the reported question.

We describe a rectangle by four whole numbers [x1, y1, x2, y2]: it is made of all points whose x-coordinate is between x1 and x2 and whose y-coordinate is between y1 and y2, ends included. Its sides are therefore horizontal or vertical.

You are given two such rectangles, rec1 and rec2. Return the area of the part of the plane that both of them cover. When they have no point in common, or their common points form just a line segment or a single point, return 0.

Function

overlapArea(rec1: int[], rec2: int[]) → int

Examples

Example 1

rec1 = [3,1,9,6]rec2 = [-2,-1,5,4]return = 6

Going left to right, the shared part starts at the larger left edge, max(3, -2) = 3, and stops at the smaller right edge, min(9, 5) = 5, so it is 2 units wide. Going bottom to top, it starts at max(1, -1) = 1 and stops at min(6, 4) = 4, so it is 3 units tall. The area is 2 × 3 = 6.

Example 2

rec1 = [4,-6,7,-2]rec2 = [-5,1,0,3]return = 0

rec2 ends at x = 0, before rec1 begins at x = 4, so no vertical line crosses both rectangles and they have no point in common. The answer is 0. (Subtracting edges anyway gives a width of 0 - 4 = -4 and a height of -2 - 1 = -3. Their product, 12, is not an area.)

Example 3

rec1 = [-4,3,2,8]rec2 = [-1,-2,6,3]return = 0

The bottom side of rec1 and the top side of rec2 both lie on the line y = 3. Along that line, the two sides overlap from x = -1 to x = 2. That overlap is only a line segment, which has no area, so the answer is 0.

Constraints

  • Each of rec1 and rec2 holds exactly 4 numbers.
  • Inside one rectangle, x1 < x2 and y1 < y2, so its width and height are positive.
  • Every number lies between -10^4 and 10^4, inclusive.

More Nutanix Onsite Interview questions

  • Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
  • Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
  • Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
  • Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
  • LRU CacheONSITE INTERVIEW · Seen Aug 2025
  • Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
  • Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
  • Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
  • Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
  • Sudoku SolverONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Rectangle OverlapMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Correct Three Protractor ReadingsSusquehanna International Group (SIG) · ONSITE INTERVIEWSusquehanna International Group (SIG) interview questions
  • Common Part of Two RectanglesExpedia · ONSITE INTERVIEWExpedia interview questions
  • Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
  • Max Points on a LineLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Rasterize a Circle with Integer PixelsPure Storage · ONSITE INTERVIEWPure Storage interview questions
  • Point Inside a TriangleSambaNova Systems · PHONE SCREEN · ONSITE INTERVIEWSambaNova Systems interview questions
  • Circle Intersection in Three DimensionsNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Range of a Third CorrelationTower Research Capital · PHONE SCREEN
  • Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions

Interview reports that mention this question

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public int overlapArea(int[] rec1, int[] rec2) {
        // Write your code here.
    }
}
Checking account…