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- A. 6
- B. 4
- C. 5
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Coins are 1, 5, 10, 21 and 25. To pay 63, the greedy method below always takes the largest coin that fits, using the list in descending…
- Intervals (start, end): (1,10), (2,3), (4,5), (6,7), (8,9). The greedy below selects the largest set of pairwise disjoint intervals…
- Each event is (time, +1) for a train arriving or (time, -1) for a train departing. Arrivals are at 1, 3, 3, 5, 6, 8 and departures are at…
- Five numbers 3, 30, 34, 5, 9 are to be arranged to form the largest possible number. Two numbers x, y are put in the order that gives the…
- Prices by day: 1, 5, 3, 8, 6, 10. Profit is earned in every rise: whenever tomorrow's price is higher than today's, buy today and sell…
- Array a = [3, 1, 2, 5, 4] is sorted by bubble sort: in each pass adjacent pairs out of order are swapped (a[j] > a[j+1]). What is the…