Find minimum racks for 2D bin packing
Company: Accenture
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of combinatorial optimization and multi-dimensional bin packing, assessing algorithmic design, resource-allocation reasoning, and computational complexity awareness.
Constraints
- 1 <= N <= 20 (number of machines) for the exact bitmask DP
- Each machine fits in an empty rack: x_i <= A and y_i <= B
- 0 <= x_i, y_i and 1 <= A, B
- Resource requirements and capacities are non-negative integers
Examples
Input: ([[1, 1], [1, 1], [1, 1]], [2, 2])
Expected Output: 2
Explanation: Two identical (1,1) machines fit in one rack as (2,2); the third needs its own rack. Total 2.
Input: ([[3, 3]], [3, 3])
Expected Output: 1
Explanation: A single machine that exactly fills the rack capacity needs exactly one rack.
Input: ([], [5, 5])
Expected Output: 0
Explanation: No machines means zero racks are required.
Input: ([[2, 1], [1, 2], [2, 1], [1, 2]], [3, 3])
Expected Output: 2
Explanation: Pair complementary machines: (2,1)+(1,2) = (3,3) fits one rack; the other pair fills a second. Total 2.
Input: ([[5, 5], [5, 5], [5, 5]], [5, 5])
Expected Output: 3
Explanation: Each machine alone already saturates both dimensions, so no two can share a rack. Total 3.
Input: ([[1, 3], [3, 1], [2, 2], [2, 2]], [4, 4])
Expected Output: 2
Explanation: Rack 1: (1,3)+(3,1) = (4,4). Rack 2: (2,2)+(2,2) = (4,4). Both racks are exactly full. Total 2.
Input: ([[4, 4]], [4, 4])
Expected Output: 1
Explanation: One machine exactly matching the rack capacity uses a single rack.
Hints
- The naive bound is at most N racks (one machine each). The lower bound is at least 1. You want the exact minimum, and exact 2D bin packing is NP-hard — so an exponential exact algorithm is acceptable here.
- Represent any group of machines as a bitmask. Precompute feasible[mask] = whether all machines in that mask fit together in ONE rack (sum of x <= A AND sum of y <= B).
- DP over subsets: dp[mask] = minimum racks to pack exactly the machines in mask. Transition: pick any feasible submask 'sub' of mask to be the contents of one rack, then dp[mask] = min over sub of dp[mask ^ sub] + 1.
- Iterate submasks efficiently with the trick `sub = (sub - 1) & mask`. The total work across all masks is O(3^N).