Longest Chain of Shipping Boxes That Nest Inside Each Other

Last updated: November 29, 2026

Quick Overview

Given box footprints as (width, height), a box fits inside another only if both its width and height are strictly smaller. Return the largest number of boxes that can be nested into a single chain.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given box footprints as (width, height), a box fits inside another only if both its width and height are strictly smaller. Return the largest number of boxes that can be nested into a single chain.

Amazon packaging engineers think hard about box nesting for storage and returns. The onsite question is the 2D longest increasing subsequence; the interviewer checks the sort trick for equal widths and the O(n log n) patience sorting LIS.

What the Interviewer Expects
  • Reduce 2D nesting to LIS on heights after sorting
  • Sort width ascending and height descending for ties
  • Explain why the descending tie-break prevents equal widths from chaining
  • Use binary search on tails for O(n log n)
  • Mention the O(n^2) DP baseline
Key Topics to Cover
1D dynamic programming
longest increasing subsequence
binary search
sorting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the actual chain?
  • What changes for 3D boxes?
  • What if boxes can be rotated?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of (width, height) pairs. Nesting requires strict inequality in both dimensions; no rotation. Output: the maximum chain length....

High-Level Design

Sort by width so any chain appears as a subsequence in sorted order. Then we need a strictly increasing subsequence of heights, but boxes with the same width must not chain. Sorting equal widths by he...


Submit Your Answer
Markdown supported