Reported August 2026
IMCbinary search

Choose Containers

Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IMC OA. Under 2s to a working solution.
Founder's read

IMC dropped this one in August 2026, and it's a lot simpler than the pharma story makes it sound. Strip the wrapper and you have a lower-bound lookup repeated across many sorted lists. For every container set, each requirement needs the smallest size that's at least that amount, and you sum the gaps. Pick the set with the least total waste, lowest index on ties, -1 if nothing covers every order. If you blank during the OA, StealthCoder runs invisibly on your desktop and can hand you the approach while you type.

The problem

You work for a pharmaceutical company that produces liquid medicine. Patients requiring these medications often require different amounts. To package the medication for delivery, you can choose between different sets of containers. Each container set specifies the sizes of containers it allows. For example, set 1 might offer containers with size 300 ml and 200 ml, while set 2 might offer containers with sizes 400 ml, 250 ml, and 100 ml.
When fulfilling a customer order for a given amount, a single container must be used and filled completely. If a container does not exist that matches the required amount, the next largest container is used. The extra medication included in that larger container, i.e. size of the container - required amount, is considered waste.
Your job is to evaluate different sets of containers and identify the set of containers that will minimize the total waste for a given collection of orders.
Return the zero-based index of the set of containers which minimizes the overall waste. If multiple sets provide the same minimum waste, return the lower index. If no set satisfies the required amounts, return -1.
Container data
The requirements array contains the requested sizes, and numContainerSets is the number of container sets. Each row of containers stores a container-set index followed by one container size.
The containers rows are given in set order: the sizes for set 0 are followed by the sizes for set 1, and so on. Within each set, the sizes are sorted in ascending order.

Function
chooseContainers(requirements: int[], numContainerSets: int, containers: int[][]) → int

Examples
Example 1
requirements = [4, 6, 6, 7]
numContainerSets = 3
containers = [[0, 3], [0, 5], [0, 7], [1, 6], [1, 8], [1, 9], [2, 3], [2, 5], [2, 6]]
return = 0
The containers array is a 2D array where the first element is the container set id and the second is the size. In this case, the first set, id 0, has three containers with sizes 3, 5, and 7. The second set has containers with sizes 6, 8, and 9, and the third set has sizes 3, 5, and 6.
Using the first set, the losses are:
5 - 4 = 1
7 - 6 = 1
7 - 6 = 1
7 - 7 = 0
The total waste is 1 + 1 + 1 + 0 = 3 units.
Using the second set type, the losses are:
6 - 4 = 2
6 - 6 = 0
6 - 6 = 0
8 - 7 = 1
The total waste is 2 + 0 + 0 + 1 = 3 units.
The third set cannot be used because its maximum capacity is 6 and there is a requirement for 7.
Two sets of containers can be used that each result in 3 wasted units. The lower index set is at index 0.
Example 2
requirements = [4, 6]
numContainerSets = 2
containers = [[0, 5], [0, 7], [0, 10], [1, 4], [1, 10]]
return = 0
Set 0 uses capacities 5 and 7 for requirements 4 and 6, wasting 1 + 1 = 2 units. Set 1 uses capacities 4 and 10, wasting 0 + 4 = 4 units. Return 0.

Constraints
1 <= n <= 10^5, where n = requirements.length
1 <= numContainerSets <= 10^4
1 <= totalNumContainers <= 10^5, where totalNumContainers = containers.length
1 <= requirements[i] <= 10^9
0 <= containers[i][0] < numContainerSets
0 <= containers[i][1] <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is binary search per requirement inside each set's sorted sizes, which the input already gives you. Group the rows by set index, then for each set loop through requirements and find the first size >= the requirement. If none exists, the set is invalid, skip it. Otherwise add size minus requirement. The catch is scale. Requirements go to 10^5 and sets to 10^4, so checking every requirement against every set is too slow in the worst case. Sort requirements once, and use a prefix sum with counts, or a two-pointer walk per set, so each set costs about its own size plus log work. Also use 64-bit sums, since waste can reach 10^5 * 10^9. Watch ties (return the lower index) and the -1 case. StealthCoder is your hedge if the complexity argument slips under the clock.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Choose Containers cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass IMC's OA.

IMC reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Choose Containers FAQ

What's the real trick in Choose Containers?+

It's a lower-bound search. For each requirement, find the smallest container in a set that's greater than or equal to it. Sum the differences per set, skip sets that can't cover the largest requirement, and keep the best total with the lowest index on ties.

How hard is this IMC question really?+

Easy to medium. The logic is simple, but the constraints punish a naive nested loop. If you binary search or use a sorted two-pointer pass per set, you're fine. The traps are overflow and the invalid-set case.

Do I need to sort anything?+

The container sizes are already sorted within each set, so you can search them directly. Sorting the requirements is optional, but it lets you do a two-pointer sweep per set instead of a binary search for every requirement.

What edge cases break solutions?+

A requirement larger than a set's biggest container makes that set invalid. If every set is invalid, return -1. Use a 64-bit total because waste can exceed 32-bit range. On equal waste, keep the earlier index, so only update on strictly smaller totals.

How do I prepare for this in 48 hours?+

Practice writing a lower-bound binary search from memory, then group-by-index parsing for the containers array. Run the two examples by hand, including the tie in Example 1. That covers nearly everything this problem tests.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IMC.

OA at IMC?
Invisible during screen share
Get it