← Problems59. Late arriving events per visitorMediumSQL
00:00 / 30:00

Late arriving events per visitor

Medium33% solve itAsked at Adobe

The collection tier assigns sequence_id in the order events were received, while occurred_at is when they actually happened on the client. Offline clients flush their buffers late, so events arrive out of order and downstream windowed jobs quietly undercount. Measure how often it happens.

An event is late when it occurred before an event of the same visitor that was received earlier — that is, before the high-water mark of occurred_at across all smaller sequence_id values for that visitor.

Input schema

raw_events sequence_id bigint -- receive order, globally unique and increasing visitor_id string occurred_at timestamp -- when it happened on the client

Constraints

  • Return visitor_id, late_events for visitors with at least one late event
  • A visitor's first received event is never late
  • Compare against the high-water mark of every earlier event, not only the previous one
  • Order by late_events descending, then visitor_id
  • Up to 30,000,000 events

Example 1one buffered event arrives after the fact

Input · raw_events
visitor_idoccurred_atsequence_id
v12026-07-01 09:00:001
v12026-07-01 09:30:002
v12026-07-01 09:10:003
Output
visitor_idlate_events
v11

Example 2events received in order are never late

Input · raw_events
visitor_idoccurred_atsequence_id
v12026-07-02 09:00:001
v12026-07-02 09:01:002
v22026-07-02 10:00:003
v22026-07-02 08:00:004
Output
visitor_idlate_events
v21

Topics

window functionsdata quality

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.sql
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.