← Hash Strategies

Micro-Drill #258 — Trie prefix check (starts_with)

Hash Strategies Target: 10s

Same as search but returns True when you reach the end of the prefix — no need to check is_end.

def starts_with(root, prefix):
    node = root
    for ch in prefix:
        if ch not in node.children:
            return False
        node = node.children[ch]
    return True

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #257 Micro #259 →