ProgrammingVery hardTCS NQT · Accenture

Items of sizes 5, 7, 5, 2, 4, 2, 5, 1, 6 are packed in this order into bins of capacity 10 using the first-fit rule: put each item into the first existing bin with enough room, otherwise open a new bin. What is the number of bins printed?

Items of sizes 5, 7, 5, 2, 4, 2, 5, 1, 6 are packed in this order into bins of capacity 10 using the first-fit rule: put each item into the first existing bin with enough room, otherwise open a new bin. What is the number of bins printed?
bins = empty list
for each item x in order
    if some bin has room for x then put x in the first such bin
    else open a new bin holding x
end for
Print number of bins
  1. A. 6
  2. B. 4
  3. C. 5
  4. D. 9

Correct answer: C. 5

First fit reuses earlier bins and needs five bins for these items. Next fit never reuses a closed bin and uses more bins, the ceiling of total size over capacity is a lower bound that is not reached, and counting items is meaningless.

More Pseudocode and Code Tracing questions

Exams that ask this topic