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- A. a#bbc#
- B. a#bccc
- C. a#bcb#
- 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#.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Procedure f(Integer n) if (n == 0) return end if Print n f(n - 1) Print n End…
- What will be the output of the following pseudocode? // a trie (prefix tree) starts with only an empty root for each word in {"car"…
- What will be the output of the following pseudocode? // LRU cache of capacity 2: get(k) returns the value or -1; put evicts the least…
- What will be the output of the following pseudocode? Integer g[3][4] = {{1, 1, 1, 0}, {1, 1, 1, 1}, {0, 1, 1, 1}} Integer c = 0 for (each…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function count7(Integer n) if…
- What will be the output of the following pseudocode? String s = "ABCAB" Integer cnt[26] = all 0, total = 0 for (each ch in s) cnt[ch] =…