🎙 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 13 of 15

What does this print? (code)

What does this print?

root = {"cnt": 0}
def insert(w):
    node = root
    for ch in w:
        node = node.setdefault(ch, {"cnt": 0})
        node["cnt"] += 1
for w in ["apple", "app", "apt", "bat"]:
    insert(w)
print(root["a"]["p"]["cnt"], root["a"]["p"]["p"]["cnt"], root["b"]["cnt"])
  1. A.2 2 1
  2. B.3 2 1
  3. C.3 3 1
  4. D.4 2 1
Answer & explanation

Correct answer: 3 2 1

Three words start with "ap", two of them (apple, app) start with "app", and one starts with "b".

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 →