Find the longest chain of nested boxes where each box is strictly smaller in height and width

Quick Overview

Given boxes described by height and width, find the longest chain in which each box fits strictly inside the next. It tests turning a two-dimensional ordering into a one-dimensional sequence problem and handling boxes that share a height.

Find the longest chain of nested boxes where each box is strictly smaller in height and width

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a list of `boxes` where `boxes[i] = [height_i, width_i]` gives the height and width of the `i`-th box, find the maximum number of boxes that can be nested inside one another. Box `a` fits inside box `b` only if `a`'s height is strictly less than `b`'s height and `a`'s width is strictly less than `b`'s width. Return the maximum number of boxes in such a nesting chain. ### Function Signature ```python def max_nested_boxes(boxes: list[list[int]]) -> int: ``` ### Rules - Each box is `[height, width]`. Boxes cannot be rotated: height is compared only with height, and width only with width. - A chain is a sequence of distinct boxes from the list in which each box fits inside the next one. A single box is a chain of length 1, so the answer is at least 1. - Two boxes with the same height, or the same width, can never be in the same chain. Identical boxes do not nest. ### Constraints - `1 <= len(boxes) <= 2000` - `1 <= height_i, width_i <= 10^5` ### Examples ```text Input: boxes = [[1, 1], [2, 2], [3, 3], [4, 4]] Output: 4 ``` Each box fits inside the next: [1, 1] -> [2, 2] -> [3, 3] -> [4, 4], so all four boxes form one chain. ```text Input: boxes = [[3, 4], [2, 3], [4, 5], [2, 2], [5, 7]] Output: 4 ``` One longest chain is [2, 2] -> [3, 4] -> [4, 5] -> [5, 7]. A chain of five is impossible because [2, 3] and [2, 2] share a height, so at most one of them can be used. ```text Input: boxes = [[4, 1], [4, 2], [4, 3], [5, 4]] Output: 2 ``` The three boxes of height 4 cannot nest in one another, so a chain holds at most one of them, for example [4, 3] -> [5, 4].

Overview: Given boxes described by height and width, find the longest chain in which each box fits strictly inside the next. It tests turning a two-dimensional ordering into a one-dimensional sequence problem and handling boxes that share a height.

Uber logo
Uber
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a list of boxes where boxes[i] = [height_i, width_i] gives the height and width of the i-th box, find the maximum number of boxes that can be nested inside one another.

Box a fits inside box b only if a's height is strictly less than b's height and a's width is strictly less than b's width.

Return the maximum number of boxes in such a nesting chain.

Function Signature

def max_nested_boxes(boxes: list[list[int]]) -> int:

Rules

  • Each box is [height, width] . Boxes cannot be rotated: height is compared only with height, and width only with width.
  • A chain is a sequence of distinct boxes from the list in which each box fits inside the next one. A single box is a chain of length 1, so the answer is at least 1.
  • Two boxes with the same height, or the same width, can never be in the same chain. Identical boxes do not nest.

Constraints

  • 1 <= len(boxes) <= 2000
  • 1 <= height_i, width_i <= 10^5

Examples

Input: boxes = [[1, 1], [2, 2], [3, 3], [4, 4]]
Output: 4

Each box fits inside the next: [1, 1] -> [2, 2] -> [3, 3] -> [4, 4], so all four boxes form one chain.

Input: boxes = [[3, 4], [2, 3], [4, 5], [2, 2], [5, 7]]
Output: 4

One longest chain is [2, 2] -> [3, 4] -> [4, 5] -> [5, 7]. A chain of five is impossible because [2, 3] and [2, 2] share a height, so at most one of them can be used.

Input: boxes = [[4, 1], [4, 2], [4, 3], [5, 4]]
Output: 2

The three boxes of height 4 cannot nest in one another, so a chain holds at most one of them, for example [4, 3] -> [5, 4].