2dbi
Home/Paytm/Implement a Trie for UPI ID Autocomplete
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 match
  • startsWith(prefix) → true/false prefix exists
  • autocomplete(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
  • autocomplete does 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
LeadersAccount