ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
// BST: smaller keys go left, larger (or equal) keys go right
Insert into an empty BST, in this order: 8, 3, 10, 1, 6, 14, 4, 7, 13
Function height(Node t)
if (t == NULL)
return 0
end if
return 1 + max(height(t.left), height(t.right))
End Function
Integer comps = number of nodes visited while searching for 7 from the root
Print height(root), comps- A. 3 4
- B. 4 3
- C. 4 4
- D. 5 4
Correct answer: C. 4 4
The longest root-to-leaf paths are 8–3–6–4, 8–3–6–7 and 8–10–14–13, each with 4 nodes, so height = 4. Searching for 7 visits 8, 3, 6, 7: 4 nodes. Output 4 4.
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? Insert into an empty BST, in this order: 8, 3, 10, 1, 6, 14, 4, 7, 13 Queue q =…
- 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)…