Top K Trending Dishes Over a Sliding Time Window

Last updated: November 29, 2026

Quick Overview

Build a class that records dish orders with timestamps and returns the k most ordered dishes in the last W seconds at any query time.

Zomato
Coding & Algorithms
Software Engineer
Zomato
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Build a class that records dish orders with timestamps and returns the k most ordered dishes in the last W seconds at any query time.

A trending-near-you rail on the Zomato home screen depends on counts that expire as time moves. The screen checks whether the candidate can keep counts consistent under eviction and reason about the cost of record versus query.

What the Interviewer Expects
  • Use a queue of events to evict expired entries in order
  • Maintain a running counter per dish
  • Return top k with a heap or partial sort at query time
  • Define tie-breaking deterministically
  • Discuss amortized cost of eviction
Key Topics to Cover
Sliding window
Hash map counting
Heaps
Amortized analysis
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 make queries O(k) if they are far more frequent than writes?
  • How do you shard this per city and merge results?
  • What changes if events arrive out of order by a few seconds?
  • Could you approximate with a Count-Min Sketch, and what would you lose?
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

record(dish, ts) and top_k(now, k) over events with ts > now - W. Timestamps are non-decreasing for writes. Ties broken alphabetically so the rail does not flicker between refreshes. Dishes with...

High-Level Design

Keep a deque of (ts, dish) events and a Counter. Both operations first evict events older than the window from the left of the deque and decrement their counts. The query then selects the k best `(c...


Submit Your Answer
Markdown supported

Related Questions