Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Stream Validation Function

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

Your question is Stream Validation Function. 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

Alaska Airlines Mileage Plan receives user profile events as an ordered stream. Write a function that accepts valid events while identifying malformed, duplicate, or out-of-order data.

An event is valid when it contains event_id, user_id, email, and occurred_at; both identifiers are non-empty strings; email contains exactly one @ with a non-empty local part and a domain containing a period; and occurred_at is a non-negative integer. An event is also invalid if its event_id was already accepted or if its timestamp is not strictly later than the most recently accepted event for the same user. Rejected events must not update validation state.

Formal Specification

Implement process_user_stream(events). The input is a list of dictionaries. Return a dictionary with accepted, the original valid event dictionaries in input order, and rejected_indices, the zero-based indices of invalid events in input order.

Extra fields are allowed and must be preserved. The input stream is already ordered, so do not sort it.

Constraints

  • 1 <= len(events) <= 100000
  • Each event contains at most 20 fields
  • event_id and user_id are intended to be non-empty strings
  • occurred_at must be a non-negative integer
  • Rejected events do not update duplicate or timestamp state
  • Extra fields are allowed and must be preserved

Function Signature

def process_user_stream(events):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output