Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Detect Duplicate Events in Streams
00:00
5 left

Detect Duplicate Events in Streams

MediumPython

Problem

A consumer of Slack's Events API may receive the same event more than once. Given events in nondecreasing timestamp order, filter duplicates while keeping the first occurrence of each event_id within a rolling time window.

An event is a duplicate if its event_id has already been accepted and its timestamp is within the inclusive interval [current_timestamp - window, current_timestamp]. Events whose previous occurrence is older than this interval are accepted again. Preserve the input order of accepted events.

Formal Specification

Implement deduplicate_events(events, window), where events is a list of dictionaries with string event_id, integer timestamp, and arbitrary payload fields. window is a nonnegative integer number of seconds. Return a list containing the original event dictionaries that are not duplicates.

Use a hash-based structure for fast membership checks and a queue to expire IDs that leave the rolling window.

Constraints

  • 0 <= len(events) <= 10^6
  • Events are sorted by nondecreasing timestamp
  • 0 <= timestamp <= 10^12
  • 0 <= window <= 10^9
  • Each event contains a nonempty string event_id

Function Signature

def deduplicate_events(events, window):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.