The Minimum Rounds to Complete All Tasks problem gives you a list of tasks where each task has a difficulty level. In each round, you can complete either 2 or 3 tasks of the same difficulty level. Find the minimum number of rounds to complete all tasks, or return -1 if it's impossible. This Minimum Rounds to Complete All Tasks interview question tests greedy reasoning with a mathematical twist.
Amazon and Anduril ask this medium-level problem because it cleanly tests the interplay of counting, greedy math, and hash tables. It verifies that candidates can think about problems in terms of partition math (how to express a number as a sum of 2s and 3s) and apply it efficiently. The array, hash table, counting, and greedy interview pattern is directly demonstrated.
Use a hash map to count the frequency of each task difficulty. For each frequency f:
f/3 rounds (all groups of 3).(f-4)/3 + 2 rounds (one group of 4 = 2+2, rest in 3s).(f-2)/3 + 1 rounds (one group of 2, rest in 3s).Equivalently, the formula is ceil(f / 3) for f ≥ 2.
Tasks: [2, 2, 3, 3, 2, 4, 4, 4, 4, 4].
Mathematical greedy problems reward candidates who can derive formulas instead of simulating. Practice writing out the cases for small values (f=1 through f=6) to discover the ceiling pattern. The insight that "ceil(f/3) rounds" handles all cases except f==1 is elegant once seen. Whenever a problem involves partitioning a count into fixed-size groups, think immediately about ceiling division — it's a compact and powerful tool.