ProgrammingVery hardTCS NQT · Accenture
Tasks with the same letter must be separated by at least n = 2 idle or other slots. The task string is AAABBC. The greedy formula below gives the minimum number of time slots: take the maximum frequency m, the number k of letters having frequency m, and the answer is the larger of the length of the string and (m - 1) (n + 1) + k. What is printed?
Tasks with the same letter must be separated by at least n = 2 idle or other slots. The task string is AAABBC. The greedy formula below gives the minimum number of time slots: take the maximum frequency m, the number k of letters having frequency m, and the answer is the larger of the length of the string and (m - 1) * (n + 1) + k. What is printed?
m = max frequency; k = count of letters with frequency m Print max(length, (m - 1) * (n + 1) + k)
- A. 7
- B. 6
- C. 5
- D. 9
Correct answer: A. 7
The three A's need two slots between each, A _ _ A _ _ A, and B, B, C fill the gaps (A B C A B _ A), so (m − 1)(n + 1) + k = 2 × 3 + 1 = 7 slots, one idle. The bare length 6 ignores the forced idle slot, (m − 1) × n + k = 5 forgets that each block includes the A itself, and m × (n + 1) = 9 adds a needless trailing block.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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…
- 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…