ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String stream = "aabcbc"
Integer cnt[26] = all 0
Queue q = empty
for (each ch in stream)
    cnt[ch] = cnt[ch] + 1
    enqueue(q, ch)
    while (q not empty AND cnt[front(q)] > 1)
        dequeue(q)
    end while
    if (q is empty) Print '#' else Print front(q)
end for
  1. A. a#bbc#
  2. B. a#bccc
  3. C. a#bcb#
  4. D. aabbcc

Correct answer: A. a#bbc#

After each character the queue's front is the first character so far that has appeared only once: a, then # (a repeats), b, b, c (b now repeats), # (c repeats too). Printed together: a#bbc#.

More Pseudocode and Code Tracing questions

Exams that ask this topic