Track Top Trending Titles Over the Last Hour

Last updated: November 29, 2026

Quick Overview

Design a class that ingests play-start events (timestamp, title) in time order and returns the top K titles by play starts within the last W seconds at any query time.

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

0

0

0 solved


Design a class that ingests play-start events (timestamp, title) in time order and returns the top K titles by play starts within the last W seconds at any query time.

Tubi surfaces trending rows on its homepage, and this question asks for a small in-memory version of that counter. The interviewer checks a clean sliding window with incremental counts, sensible eviction on both writes and reads, and a deterministic tie-break.

What the Interviewer Expects
  • Maintains counts incrementally instead of recounting the window
  • Evicts on both record and query so stale events never leak into results
  • Uses a heap or partial sort for top K and states the cost
  • Breaks ties deterministically
  • Removes zero-count titles to bound memory
Key Topics to Cover
Sliding window
Hash maps
Heaps
Streaming counters
Top K
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 scale this when events arrive at hundreds of thousands per second across many servers?
  • How would you replace the exact window with fixed buckets to save memory?
  • Should trending count distinct viewers instead of plays? How would that change the structure?
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(ts, title) with non-decreasing ts, and top_k(now, k) returning up to k (title, count) pairs for events in (now - W, now], highest count first, ties by title id....

High-Level Design

A deque of events in time order plus a hash map of counts. Eviction pops from the left and decrements counts until the oldest event is inside the window. Top K uses heapq.nsmallest on the counts wit...


Submit Your Answer
Markdown supported

Related Questions