Track Trending and Fading Search Terms With Constant-Time Updates

Last updated: November 29, 2026

Quick Overview

Design a counter over search terms with `inc(term)`, `dec(term)` (removing the term at zero), `max_term()`, and `min_term()`, where every operation runs in O(1) time.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Design a counter over search terms with `inc(term)`, `dec(term)` (removing the term at zero), `max_term()`, and `min_term()`, where every operation runs in O(1) time.

A hard onsite design-a-structure question in Amazon's style, framed around a real-time trending widget for search. Interviewers want the bucket-list design (a doubly linked list of count buckets, each holding a set of terms) and careful pointer surgery when buckets appear and disappear.

What the Interviewer Expects
  • Explains why a heap gives O(log n), not O(1)
  • Uses a doubly linked list of buckets ordered by count
  • Keeps a map from term to its bucket
  • Creates and deletes buckets as counts change
  • Uses sentinel head and tail nodes to avoid edge cases
Key Topics to Cover
design a data structure
doubly linked lists
hash maps
O(1) operations
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the top 10 terms instead of one?
  • How would you decay counts over time so old trends fade?
  • How would you shard this across many hosts for global trending?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

inc adds 1 (a new term starts at 1). dec subtracts 1 and removes the term at 0; dec on an unknown term is a no-op. max_term and min_term return any term with the extreme count, or "" if em...

High-Level Design

A heap or sorted map would make updates O(log⁡n)O(\log n). The trick is that counts only move by 1, so a term only ever hops to the adjacent bucket. Keep a doubly linked list of buckets in increasing count ...


Submit Your Answer
Markdown supported