KatInterview

Event Log Selector with Window, Dedupe and Cap

Arrays & HashingAuto-graded · 20 test cases
MediumAuto-gradedArrays & Hashinghashingstable-sortfiltering

Last updated

Filter a list of event records by type, time window and id, then order them stably by timestamp and truncate, testing careful handling of optional arguments and ties.

You are given a log of records. Each record is a dictionary with these keys:

  • id (string): an identifier that may repeat across records.
  • type (string): the category of the record.
  • ts (integer): a timestamp in seconds since the epoch.
  • payload (dictionary): arbitrary extra data. It is never inspected, only carried along.

Write a function that selects records from the log according to the optional rules below and returns them in a defined order.

Parameters

In this order:

  1. events: the list of records.
  2. allowed_types: a list of strings, or None.
  3. blocked_types: a list of strings, or None.
  4. min_ts: an integer, or None.
  5. max_ts: an integer, or None.
  6. max_count: a non-negative integer, or None.
  7. latest_per_id: a boolean.

Rules

Apply the steps in this order.

  1. Allowed types. If allowed_types is not None, keep only records whose type appears in it. An empty list is a real filter that allows nothing, so the result is empty. None means no restriction.
  2. Blocked types. If blocked_types is not None, drop records whose type appears in it. A type listed in both allowed_types and blocked_types is dropped.
  3. Time window. Keep records with min_ts <= ts <= max_ts. Both ends are inclusive, and each bound is applied only if it is not None. If min_ts > max_ts, nothing survives.
  4. Per-id dedupe. If latest_per_id is True, work on the records that survived steps 1 to 3. For each distinct id, keep only the record with the largest ts. If several records of the same id share that largest ts, keep the one that appears first in the input.
  5. Ordering. Sort the remaining records by ts ascending. Records with equal ts keep their relative order from the original input.
  6. Cap. If max_count is not None, return only the first max_count records of the sorted result. 0 gives an empty list, and a value larger than the result size returns everything.

Return a list of the original record dictionaries. Do not modify the input list or its records.

Constraints

  • 0 <= len(events) <= 10^5
  • Every record has all four keys with the types described above.
  • max_count, when given, is >= 0.
  • Type names are compared by string equality.
  • Aim for roughly O(n log n) time.

Examples

Example 1

Input: select_events([{'id': 'a', 'type': 'click', 'ts': 30, 'payload': {'page': 'home'}}, {'id': 'b', 'type': 'view', 'ts': 10, 'payload': {}}, {'id': 'c', 'type': 'click', 'ts': 20, 'payload': {'page': 'pricing'}}], ['click'], None, None, None, None, False)

Output: [{'id': 'c', 'type': 'click', 'ts': 20, 'payload': {'page': 'pricing'}}, {'id': 'a', 'type': 'click', 'ts': 30, 'payload': {'page': 'home'}}]

Example 2

Input: select_events([{'id': 'a', 'type': 'click', 'ts': 5, 'payload': {}}, {'id': 'b', 'type': 'view', 'ts': 2, 'payload': {}}, {'id': 'c', 'type': 'purchase', 'ts': 7, 'payload': {}}, {'id': 'd', 'type': 'click', 'ts': 5, 'payload': {}}], None, ['purchase'], 5, 7, 2, False)

Output: [{'id': 'a', 'type': 'click', 'ts': 5, 'payload': {}}, {'id': 'd', 'type': 'click', 'ts': 5, 'payload': {}}]

Example 3

Input: select_events([{'id': 'x', 'type': 'login', 'ts': 10, 'payload': {'n': 1}}, {'id': 'y', 'type': 'login', 'ts': 8, 'payload': {'n': 2}}, {'id': 'x', 'type': 'logout', 'ts': 15, 'payload': {}}, {'id': 'z', 'type': 'login', 'ts': 15, 'payload': {}}], None, None, None, None, None, True)

Output: [{'id': 'y', 'type': 'login', 'ts': 8, 'payload': {'n': 2}}, {'id': 'x', 'type': 'logout', 'ts': 15, 'payload': {}}, {'id': 'z', 'type': 'login', 'ts': 15, 'payload': {}}]

Hints

Hint 1

Treat None as 'no restriction' using an `is not None` check, never a truthiness check, so an empty allowed list correctly yields nothing.

Hint 2

Do the cheap per-record checks first, remembering each survivor's original position. Then dedupe with a dictionary keyed by id, replacing an entry only when the new ts is strictly greater.

Hint 3

Sort by (ts, original_index) and apply the cap last, since 'first max_count' only makes sense after ordering. Convert the type lists to sets for fast lookups.