← 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"])
- A.2 2 1
- B.3 2 1
- C.3 3 1
- 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 →