PPaytm·DSASDE-1Technical Interview 1 – DSA
Implement a Trie for UPI ID Autocomplete
Problem
Implement a Trie (prefix tree) that supports UPI ID autocomplete.
Required operations
insert(upiId)— add a UPI ID (e.g., "shivam@paytm")search(upiId)→ true/false exact matchstartsWith(prefix)→ true/false prefix existsautocomplete(prefix, topK)→ return up to K UPI IDs matching the prefix, sorted by frequency
Example
insert("alice@paytm") x3, insert("alex@paytm") x1, insert("bob@upi") x2
autocomplete("al", 2) → ["alice@paytm", "alex@paytm"]
Implementation notes
- Each Trie node stores: children map, isEnd flag, frequency counter
autocompletedoes DFS from the prefix node, collects all words, returns top-K by frequency
Extension
How would you make this thread-safe for concurrent inserts and searches?
added 6 days ago