Find the largest piece of cake after horizontal and vertical cuts

Quick Overview

Given a rectangular cake and lists of horizontal and vertical cut positions, find the area of the largest piece left after every cut, modulo 10^9 + 7. It tests sorting, reasoning about how the cuts form pieces, and handling large products without overflow.

Find the largest piece of cake after horizontal and vertical cuts

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a rectangular cake of height `h` and width `w`, and two integer arrays `horizontal_cuts` and `vertical_cuts`: - `horizontal_cuts[i]` is the distance from the top of the cake to the i-th horizontal cut. - `vertical_cuts[j]` is the distance from the left of the cake to the j-th vertical cut. Cut the cake at every position listed in both arrays. Return the maximum area of a piece of cake after all the cuts. Since the answer can be large, return it modulo `10^9 + 7`. ### Function Signature ```python def max_area(h: int, w: int, horizontal_cuts: list[int], vertical_cuts: list[int]) -> int: ``` ### Rules - Each horizontal cut runs across the full width of the cake, and each vertical cut runs across the full height. - The cut arrays may be given in any order. - The edges of the cake (distance 0, and distance `h` or `w`) bound the outer pieces. - The maximum is taken over the true areas. Only the final answer is reduced modulo `10^9 + 7`. ### Constraints - `2 <= h, w <= 10^9` - `1 <= len(horizontal_cuts) <= min(h - 1, 10^5)` - `1 <= len(vertical_cuts) <= min(w - 1, 10^5)` - `1 <= horizontal_cuts[i] < h` - `1 <= vertical_cuts[i] < w` - All values in `horizontal_cuts` are distinct. - All values in `vertical_cuts` are distinct. ### Examples ```text Input: h = 5, w = 4, horizontal_cuts = [1, 2, 4], vertical_cuts = [1, 3] Output: 4 ``` The horizontal cuts split the height into bands of 1, 1, 2 and 1. The vertical cuts split the width into bands of 1, 2 and 1. The largest piece is 2 by 2, so its area is 4. ```text Input: h = 5, w = 4, horizontal_cuts = [3, 1], vertical_cuts = [1] Output: 6 ``` Sorted, the horizontal cuts give bands of 1, 2 and 2, and the vertical cut gives widths of 1 and 3. The largest piece is 2 by 3, with area 6. ```text Input: h = 1000000000, w = 1000000000, horizontal_cuts = [1], vertical_cuts = [1] Output: 64 ``` The largest piece is 999999999 by 999999999. That side length equals `(10^9 + 7) - 8`, so the area modulo `10^9 + 7` is `(-8)^2 = 64`.

Overview: Given a rectangular cake and lists of horizontal and vertical cut positions, find the area of the largest piece left after every cut, modulo 10^9 + 7. It tests sorting, reasoning about how the cuts form pieces, and handling large products without overflow.

Amazon logo
Amazon
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a rectangular cake of height h and width w, and two integer arrays horizontal_cuts and vertical_cuts:

  • horizontal_cuts[i] is the distance from the top of the cake to the i-th horizontal cut.
  • vertical_cuts[j] is the distance from the left of the cake to the j-th vertical cut.

Cut the cake at every position listed in both arrays. Return the maximum area of a piece of cake after all the cuts. Since the answer can be large, return it modulo 10^9 + 7.

Function Signature

def max_area(h: int, w: int, horizontal_cuts: list[int], vertical_cuts: list[int]) -> int:

Rules

  • Each horizontal cut runs across the full width of the cake, and each vertical cut runs across the full height.
  • The cut arrays may be given in any order.
  • The edges of the cake (distance 0, and distance h or w ) bound the outer pieces.
  • The maximum is taken over the true areas. Only the final answer is reduced modulo 10^9 + 7 .

Constraints

  • 2 <= h, w <= 10^9
  • 1 <= len(horizontal_cuts) <= min(h - 1, 10^5)
  • 1 <= len(vertical_cuts) <= min(w - 1, 10^5)
  • 1 <= horizontal_cuts[i] < h
  • 1 <= vertical_cuts[i] < w
  • All values in horizontal_cuts are distinct.
  • All values in vertical_cuts are distinct.

Examples

Input: h = 5, w = 4, horizontal_cuts = [1, 2, 4], vertical_cuts = [1, 3]
Output: 4

The horizontal cuts split the height into bands of 1, 1, 2 and 1. The vertical cuts split the width into bands of 1, 2 and 1. The largest piece is 2 by 2, so its area is 4.

Input: h = 5, w = 4, horizontal_cuts = [3, 1], vertical_cuts = [1]
Output: 6

Sorted, the horizontal cuts give bands of 1, 2 and 2, and the vertical cut gives widths of 1 and 3. The largest piece is 2 by 3, with area 6.

Input: h = 1000000000, w = 1000000000, horizontal_cuts = [1], vertical_cuts = [1]
Output: 64

The largest piece is 999999999 by 999999999. That side length equals (10^9 + 7) - 8, so the area modulo 10^9 + 7 is (-8)^2 = 64.