As reported: Amazon SDE-2 loop (Bengaluru, Amazon Pay, May 2025), round 4 — DSA after 15 minutes of Leadership Principles: build a search suggestion service from a text file of all previous searches (with duplicates). When a user types a prefix such as
bat, return the top suggestions — most frequent first, ties alphabetical. The interviewer asked how the Trie nodes are created, why an end-of-word marker is needed, how to collect all words under a prefix and how to get the top K, plus the time complexity of insert and query.
You are given searches, every search ever made (the same string can appear many times), a list of prefixes typed by users, and an integer k. For each prefix, return up to k distinct past searches that start with that prefix, ordered by how often they were searched (most first); searches with the same count are ordered alphabetically.
Example
searches = ["book","banana","batana","batman","batman","batman",
"bathroom","bathroom","batter"]
prefixes = ["bat", "ba", "c"]
k = 3
Output: [["batman","bathroom","batana"],
["batman","bathroom","banana"],
[]]
batman was searched 3 times and bathroom twice; batana, batter and banana once each, so alphabetical order decides among them.
Constraints: 1 ≤ searches.length ≤ 2·10⁴; 1 ≤ prefixes.length ≤ 2·10³; each string has 1–20 lowercase letters; 1 ≤ k ≤ 10.
Code runner format: three lines: searches (JSON array of strings), prefixes (JSON array of strings), k. Implement suggest(searches, prefixes, k) returning one list per prefix; the harness prints it as JSON.