Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Efficient Event Stream Processing
00:00
5 left

Efficient Event Stream Processing

HardPython

Problem

Box receives a large chronological stream of file-access events. Detect users who access at least threshold distinct files within any rolling window of time, while processing events incrementally.

Emit an alert only when a user's distinct-file count crosses from below threshold to at least threshold. If the count later falls below the threshold because events expire, emit another alert when the user crosses the threshold again.

Formal Specification

Implement process_events(events, window, threshold).

  • events is an iterable of events, where each event is [timestamp, user_id, file_id].
  • timestamp is an integer, and events arrive in nondecreasing timestamp order.
  • user_id and file_id are strings.
  • window is a positive integer. At event time t, an event is active when timestamp > t - window.
  • threshold is a positive integer.
  • Return a list of alerts, each represented as [timestamp, user_id], in processing order.
  • The function must not copy or sort the entire input stream.

Use data structures that support expiration of old events and duplicate file accesses efficiently. Aim for near-linear processing time and memory proportional to the active window.

Constraints

  • 1 <= number of events <= 10^6
  • 1 <= window <= 10^9
  • 1 <= threshold <= 10^5
  • 0 <= timestamp <= 10^9
  • Events are sorted by nondecreasing timestamp
  • 1 <= len(user_id), len(file_id) <= 100
  • A user can access the same file multiple times within the window

Function Signature

def process_events(events, window, threshold):
Interviewer

Your question is Efficient Event Stream Processing. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

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