ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
trie = {}
for w in ['car', 'cat', 'dog']:
    node = trie
    for ch in w:
        node = node.setdefault(ch, {})
    node['#'] = True
print(sorted(trie), sorted(trie['c']), len(trie['c']['a']))
  1. A. ['c', 'd', 'x'] ['a'] 2
  2. B. ['car', 'dog'] ['a'] 2
  3. C. ['c', 'd'] ['a'] 2
  4. D. ['c', 'd'] ['a'] 3

Correct answer: C. ['c', 'd'] ['a'] 2

The root has two child keys, 'c' and 'd'. 'c' leads to 'a', and the node for 'ca' has the children 'r' and 't', so its length is 2.

More Python Programming questions

Exams that ask this topic