Distinct Visitors In The Last Hour For Every Metrica Hit

Last updated: November 29, 2026

Quick Overview

A Metrica counter emits hits as (timestamp, user_id) in time order. For each hit, output the number of distinct users with at least one hit in the window (ts - W, ts].

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

0

0

0 solved


A Metrica counter emits hits as (timestamp, user_id) in time order. For each hit, output the number of distinct users with at least one hit in the window (ts - W, ts].

Yandex Metrica shows real-time visitor counts on site dashboards, so a sliding-window distinct count is a realistic technical screen problem. The interviewer checks amortized O(1) window maintenance and correct window boundaries.

What the Interviewer Expects
  • Maintain a queue of hits inside the window
  • Keep a per-user count so a user leaves only when their last in-window hit expires
  • Get the open versus closed boundary right
  • Explain amortized O(1) per hit
  • Discuss memory when the number of users is huge
Key Topics to Cover
sliding window
hash map
deque
distinct counting
HyperLogLog
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
  • Memory is too large for exact counts on a big site; what approximate structure would you use?
  • How do you handle hits that arrive slightly out of order?
  • How would you merge counts from many collector servers?
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

Hits are sorted by timestamp (ties allowed). Window is (ts - W, ts], so a hit exactly W seconds old has expired. Output one integer per hit....

High-Level Design

Two structures: a deque of hits currently inside the window and a dictionary user -> number of hits in window. On each hit, push it, then pop from the left while the oldest hit is at or before `ts -...


Submit Your Answer
Markdown supported

Related Questions