Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Stream Pattern Detection

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Stream Pattern Detection. Start with the requirements on the right.

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.

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output