Count Profile Views in the Trailing Five-Minute Window

Last updated: November 29, 2026

Quick Overview

Design a counter that records a profile view at a given second and returns how many views happened in the last 300 seconds. Timestamps arrive in non-decreasing order and many views can share a second.

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

0

0

0 solved


Design a counter that records a profile view at a given second and returns how many views happened in the last 300 seconds. Timestamps arrive in non-decreasing order and many views can share a second.

A warm-up data structure question in a Meta phone screen, framed around the 'who viewed your profile' style counters on Facebook and Instagram. The interviewer checks that you pick a queue over a full scan, handle bursts that share a timestamp, and can talk about memory under heavy traffic.

What the Interviewer Expects
  • Clarify the window boundary (is a view at t-300 still counted)
  • Use a deque so eviction is amortized O(1)
  • Collapse views in the same second into one bucket to bound memory
  • Keep a running total so count() does not iterate
  • Discuss what changes if timestamps can arrive out of order
Key Topics to Cover
Queue
Sliding window
Amortized analysis
Data structure design
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 support a configurable window size per caller?
  • What if timestamps can arrive slightly out of order, say up to 5 seconds late?
  • How would you shard this when one celebrity profile gets millions of views per minute?
  • Can you make memory fixed at 300 slots regardless of traffic?
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(t) logs one view at second t; count(t) returns views in (t-300, t].
  • Timestamps are non-decreasing across all calls.
  • Bursts are common, so thousands of calls can share one second....
High-Level Design

Store [timestamp, count] buckets in a deque, oldest on the left. A new view either increments the rightmost bucket (same second) or appends a new one. Before answering, pop buckets that fell out of ...


Submit Your Answer
Markdown supported