Magicsheet logo

Check if Grid can be Cut into Sections

Medium
12.5%
Updated 8/1/2025

Asked by 3 Companies

Check if Grid can be Cut into Sections

What is this problem about?

The "Check if Grid can be Cut into Sections interview question" is a geometric interval problem. You are given a grid and a list of rectangular boxes placed within it. You need to determine if the grid can be cut into three sections using either two horizontal lines or two vertical lines such that no line passes through any of the boxes.

Why is this asked in interviews?

Amazon and Bloomberg ask the "Check if Grid can be Cut into Sections coding problem" to evaluate a candidate's ability to simplify 2D geometry into 1D interval problems. It tests "Sorting interview pattern" skills and the ability to find "gaps" in overlapping intervals.

Algorithmic pattern used

This problem is solved using the Interval Merging pattern.

  1. Dimension Reduction: A horizontal cut only depends on the Y-ranges of the boxes. A vertical cut only depends on the X-ranges.
  2. Gap Finding:
    • Project all boxes onto the Y-axis to get a set of intervals [ystart,yend][y_{start}, y_{end}].
    • Sort these intervals and merge them.
    • Count the number of disjoint "merged" intervals. If there are at least 3 disjoint intervals, then there are at least 2 horizontal gaps where you can cut.
  3. Repeat: Do the same for the X-axis.
  4. Condition: Return true if either the X-axis or Y-axis allows for 2 or more cuts (i.e., has 3+ merged components).

Example explanation

Grid with 3 boxes: Box A at y=[0,2]y=[0, 2], Box B at y=[3,5]y=[3, 5], Box C at y=[6,8]y=[6, 8].

  • Merged intervals: [0,2],[3,5],[6,8][0, 2], [3, 5], [6, 8].
  • There are 3 disjoint segments.
  • We can cut at y=2.5y=2.5 and y=5.5y=5.5. Result: True.

Common mistakes candidates make

  • 2D Complexity: Trying to find lines in 2D space without realizing that a "cut across the whole grid" simplifies to a 1D problem on each axis.
  • Incorrect Counting: Thinking you need 3 cuts. You need to split the grid into 3 sections, which requires only 2 cuts.
  • Sorting Errors: Forgetting to sort intervals before merging, which is essential for the greedy merge algorithm.

Interview preparation tip

Whenever you see a problem with "rectangles" and "lines crossing the whole grid," immediately try to project the rectangles onto the X and Y axes. Turning a 2D problem into two independent 1D "Interval interview pattern" problems is a powerful problem-solving technique.

Similar Questions