As reported: Amazon SDE II (Hyderabad, July 2025), bar-raiser round: previous experience, tech stack, situational questions and why Amazon, then one algorithm question — top hashtags of Twitter: implement two APIs, one that pushes a tweet into the database and one that returns the top-k hashtags;
kis always fixed.
Implement HashtagTracker:
HashtagTracker(k)—kis fixed for the tracker's lifetime.postTweet(text)— store a tweet. Its words are separated by single spaces; a word that starts with#and has at least one more character is a hashtag. Every occurrence counts.topHashtags()— thekhashtags with the highest counts so far, highest first. Ties are broken by lexicographic order (smaller first). If fewer thankdistinct hashtags exist, return all of them.
Example
HashtagTracker(2)
postTweet("loving the #sun and #sea")
postTweet("#sea #sea again")
topHashtags() -> ["#sea", "#sun"] #sea: 3, #sun: 1
postTweet("#sun #sky")
postTweet("#sun")
topHashtags() -> ["#sea", "#sun"] both have 3; "#sea" is smaller
Constraints: 1 ≤ k ≤ 100; at most 2·10⁴ calls in total, at most 1000 of them topHashtags; tweets have 1 to 280 characters: lowercase letters, digits, # and single spaces.
Code runner format: two lines, LeetCode's class format: the method names as a JSON array (starting with "HashtagTracker"), then their arguments as a JSON array of arrays. The harness prints the results as a JSON array (null for methods without a return value).