🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Tries (Prefix Trees): all 15 questions

Quiz: Tries (Prefix Trees) · Question 11 of 15

What does this print? (code)

What does this print?

root = {}
for w in ["tea", "ten", "to", "in"]:
    node = root
    for ch in w:
        node = node.setdefault(ch, {})
    node["$"] = True
print(sorted(root), sorted(root["t"]), len(root["t"]["e"]))
  1. A.['i', 't'] ['e', 'o'] 1
  2. B.['t', 'i'] ['e', 'o'] 2
  3. C.['i', 't'] ['a', 'n', 'o'] 2
  4. D.['i', 't'] ['e', 'o'] 2
Answer & explanation

Correct answer: ['i', 't'] ['e', 'o'] 2

The root has children i and t. Under t come e (tea, ten) and o (to), and under "te" come a and n.

Can you get the rest right without peeking?

Take the timed Quiz: Tries (Prefix Trees) quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →