Count Product Page Hits Over the Trailing Five Minutes

Last updated: November 29, 2026

Quick Overview

Design a counter with `hit(timestamp)` and `get_hits(timestamp)` that returns how many hits occurred in the last 300 seconds, inclusive of the current second. Timestamps are in seconds and calls arrive in non-decreasing time order.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Design a counter with `hit(timestamp)` and `get_hits(timestamp)` that returns how many hits occurred in the last 300 seconds, inclusive of the current second. Timestamps are in seconds and calls arrive in non-decreasing time order.

A phone-screen design-a-structure warm-up in Amazon's style, framed around a lightweight per-host metric for detail page traffic. The interviewer checks a correct window boundary, constant memory when hits are bursty, and a sensible answer to concurrency and out-of-order follow-ups.

What the Interviewer Expects
  • Picks a structure and states the window boundary precisely
  • Offers a fixed 300-slot circular buffer for O(1) memory
  • Handles many hits in the same second
  • Explains the queue alternative and its memory cost
  • Discusses concurrency or distribution when asked
Key Topics to Cover
design a data structure
sliding window
circular buffer
metrics
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
  • What if timestamps can arrive slightly out of order?
  • How would you make this thread-safe on a busy host?
  • How would you aggregate counts across a fleet of 1000 hosts?
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

get_hits(ts) counts hits with timestamp tt where ts−300<t≤tsts - 300 < t \le ts. Calls are in non-decreasing time. Many hits can share a second, so memory should not grow with traffic....

High-Level Design

A queue of timestamps works but stores one entry per hit, which is unbounded during a traffic spike. Since resolution is one second and the window is 300 seconds, use two arrays of length 300 indexed ...


Submit Your Answer
Markdown supported