ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
// a trie (prefix tree) starts with only an empty root
for each word in {"car", "cat", "cart", "dog", "do"}
Node t = root
for (each ch in word)
if (t has no child ch)
create child ch of t
nodes = nodes + 1
end if
t = child ch of t
end for
end for
Print nodes // nodes starts at 0- A. 15
- B. 10
- C. 8
- D. 9
Correct answer: C. 8
Shared prefixes reuse nodes: car creates c, a, r (3); cat adds t (1); cart adds t under r (1); dog adds d, o, g (3); do adds nothing. nodes = 8.
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? // 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] =…
- What will be the output of the following pseudocode? String s = "COMPUTER" Integer i = 0, j = length(s) - 1 while (i < j) while (i < j AND…
- What will be the output of the following pseudocode? Function isSub(String a, String b) // is b a subsequence of a? Integer j = 0 for…