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)
  1. A. 7
  2. B. 6
  3. C. 5
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic