ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Insert into an empty BST, in this order: 8, 3, 10, 1, 6, 14, 4, 7, 13
Queue q = {root}
while (q not empty)
Node t = dequeue(q)
Print t.key
if (t.left != NULL) enqueue(q, t.left)
if (t.right != NULL) enqueue(q, t.right)
end while- A. 1 3 4 6 7 8 10 13 14
- B. 8 3 10 1 6 14 4 7 13
- C. 8 3 1 6 4 7 10 14 13
- D. 8 3 10 1 6 4 7 14 13
Correct answer: B. 8 3 10 1 6 14 4 7 13
A queue gives level-order traversal: level 1: 8; level 2: 3, 10; level 3: 1, 6, 14; level 4: 4, 7, 13. Output 8 3 10 1 6 14 4 7 13 — here it equals the insertion order, but only by coincidence.
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? (Print writes its values on one line, separated by spaces) Integer burst[3] = {5, 3…
- What will be the output of the following pseudocode? Integer g[4][4] = {{0,0,0,0}, {0,1,0,0}, {0,0,0,1}, {1,0,0,0}} // 1 = blocked Integer…
- What will be the output of the following pseudocode? (substring(s, i, len) is len characters starting at index i; a Set keeps one copy of…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; strings are indexed from 0)…
- What will be the output of the following pseudocode? Function p(Integer n, Integer k) if (n == 0) return 1 end if if (n < 0 OR k == 0)…
- What will be the output of the following pseudocode? Integer calls = 0 Function f(Integer a, Integer b) calls = calls + 1 if (b == 0)…