2D bin packing algorithms: shelf, guillotine, skyline and MaxRects
Codemia Knowledge Hub ·
The 2D bin packing problem asks how to place a set of rectangles into the fewest fixed-size bins without overlap. It is NP-hard, so practical systems use heuristics (shelf, guillotine, skyline or MaxRects) rather than exact optimization. It shows up in cutting-stock software, sprite atlas generation, warehouse planning and manufacturing.
Why geometry makes 2D packing hard
Each item has a width and a height, so total area alone does not decide whether items fit. Two sets of rectangles with the same combined area can pack very differently.
A valid algorithm must ensure that:
- every item fits within a bin's boundaries
- no two rectangles overlap
- optional rules such as rotation or guillotine cuts are respected
The search space grows quickly, so exact methods such as branch-and-bound or mixed-integer programming are usually reserved for small instances.
Heuristic families
Shelf algorithms
Items are arranged in horizontal rows called shelves. A new rectangle goes on the current shelf if it fits; otherwise a new shelf starts. Shelf methods are simple and fast, but they waste space when item heights vary a lot.
Guillotine algorithms
Each placement splits the free space with straight cuts. This is useful when the real manufacturing process must cut material edge to edge.
Skyline and MaxRects
These methods keep a more detailed description of the free space and usually pack more densely than simple shelves. They are common in texture atlas generators and layout engines.
First-fit decreasing shelf packer in Python
A practical baseline:
- sort rectangles by height or area, largest first
- place each rectangle on the first shelf where it fits
- if no shelf fits, start a new shelf or open a new bin
This single-bin shelf packer returns None if an item does not fit in the bin:
It is not state of the art, but it is easy to reason about and often good enough as a starting point.
Extending it to multiple bins
Keep a list of bins and place each rectangle into the first bin that can accept it, opening a new bin when none can. That makes the algorithm a 2D analog of first-fit decreasing. Its quality depends heavily on item ordering and on the free-space strategy.
Rotation and domain constraints
Some applications allow 90-degree rotation and others do not. Rotation can improve packing density substantially, but it doubles the placement choices. A packer that silently rotates items may look impressive in tests and still be unusable in production.
Manufacturing systems may also require guillotine cuts, margins between parts or forbidden orientations. Treat these as first-class constraints instead of patching them in later.
Choosing an approach
- For something simple and explainable, start with a shelf heuristic.
- When packing density matters more, use MaxRects, skyline or a guillotine heuristic tuned to your real constraints.
- When exact optimality is required and instances are small, use an optimizer.
Then tune against your actual workload, and watch for two common mistakes. Sorting only by area is rarely enough, because height and aspect ratio often matter as much. And area utilization is not the only measure: in manufacturing, cut pattern validity and machine constraints can matter more than pure density.
Related reading
System design interview problems on this topic
- Design an E-commerce Recommendation System: solution and walkthrough
- Design an LLM Inference Service: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough
- Design ChatGPT: solution and walkthrough

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.