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
Stream Pattern Detection
00:00
5 left

Stream Pattern Detection

HardPython

Problem

Bumble Date wants to identify recurring sequences of user actions, such as view_profile -> swipe_right -> send_message, while processing activity events in arrival order. Given a stream of events and multiple action patterns, return the users who complete each pattern at least threshold times.

Events from the same user form that user's action sequence in stream order. Pattern occurrences must be contiguous within that per-user sequence, and overlapping occurrences count separately.

Formal Specification

Implement find_activity_patterns(events, patterns, threshold). events is a list of triples [timestamp, user_id, action], where timestamps are nondecreasing. patterns is a list of non-empty action lists. Return a list whose ith element contains the sorted user IDs that matched patterns[i] at least threshold times. User IDs are strings, actions are strings, and matching is case-sensitive.

Process the events as a stream: do not construct a complete action sequence for every user. An event contributes only to its user's automaton state.

Constraints

  • 1 <= len(events) <= 200,000
  • 1 <= len(patterns) <= 20,000
  • Total number of actions across all patterns is at most 100,000
  • 1 <= threshold <= 10^9
  • Each event action and user ID is a non-empty string
  • Events are ordered by nondecreasing timestamp

Function Signature

def find_activity_patterns(events, patterns, threshold):
Interviewer

Your question is Stream Pattern Detection. 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.