Implement and debug event filtering in Python
Company: Affirm
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: Medium
Interview Round: Onsite
You are given a list of event dictionaries with keys: id (str), type (str), ts (int, seconds since epoch), payload (dict). Implement filter_events(events, include_types=None, exclude_types=None, start_ts=None, end_ts=None, limit=None, dedupe_by_id=False) -> List[dict] with the following behavior:
- If include_types is provided, return only events whose type is in include_types.
- If exclude_types is provided, remove events whose type is in exclude_types (after applying include_types if both are provided).
- Keep events whose ts is within the inclusive window [start_ts, end_ts] when the bounds are provided.
- Return results in ascending ts order; if ts ties, preserve original input order (stable sort).
- If limit is provided, return the earliest limit events after filtering and sorting.
- If dedupe_by_id is True, keep only the event with the largest ts for each id.
- Discuss time and space complexity and how you would handle already-sorted input.
Debugging sub-part: Identify and fix the bugs in the following snippets and explain tests that would catch them.
A)
def filter_types(events, t):
return [e for e in events if e['type'] is t]
B)
def filter_events(events, include=set()):
return [e for e in events if e['type'] in include]
Quick Answer: This question evaluates proficiency in data manipulation and algorithmic reasoning in Python, covering event filtering, stable sorting, deduplication by identifier, timestamp windowing, and analysis of time and space complexity.