KatInterview

Subscription Filter Matching with Ordered Delivery

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designdesignhash-mapjson

Last updated

Simulate a message router that matches nested JSON events against per-queue filter policies and returns matching queues in first-subscription order.

You are simulating a small in-memory message router. Queues register a filter policy, and each incoming event is delivered to every queue whose policy it satisfies.

Because the judge calls a single function, the router is driven by a script of commands. operations[i] is the command name and arguments[i] is the list of arguments for that command. Process the commands in order and return one result per command.

Commands

  • "subscribe" with arguments [queue_id, filter_policy]
    • Stores filter_policy for queue_id.
    • If queue_id is new, it goes to the end of the subscription order.
    • If queue_id already exists, only its policy is replaced and its position in the order stays as it was.
    • Result for this command: None.
  • "route" with arguments [event]
    • Result: the list of queue_ids whose current policy matches event, in subscription order (the order in which each queue was first subscribed). Return [] if nothing matches.

Filter policies

A policy is a dictionary mapping a field path to a condition. An event matches a policy only if every entry is satisfied. A policy with no entries matches every event.

Field paths. A path such as "user.address.city" is split on . and followed through nested dictionaries in the event. If any step is missing, or the value reached at some step is not a dictionary, the field is absent and its condition fails. A field that is present with the value null (None) exists, but it satisfies none of the condition types below.

Condition types. The condition for a field is one of the following.

  1. Value list. A list of primitives (str, int, float, bool). The condition holds if the field equals at least one entry. An empty list never matches. Equality is type-aware:
    • Booleans are only equal to booleans (true is not 1, and false is not 0).
    • Numbers compare by numeric value, so 100 equals 100.0.
    • Strings only equal strings.
    • A field holding a list, a dictionary or null never equals any entry.
  2. Numeric bounds. A dictionary using any of "$gt", "$gte", "$lt", "$lte". The field value must be a real number (int or float, not a boolean), and it must satisfy every bound listed. Each bound value must itself be a real number and not a boolean, otherwise the condition fails.
  3. Prefix. A dictionary containing the key "$prefix". The field value must be a string, the prefix must be a string, and the value must start with the prefix. When "$prefix" is present, only this check is made and other keys in that dictionary are ignored.

Any condition that is neither a list nor a dictionary does not match. If a type does not fit the condition (for example, a numeric bound applied to a string field), the condition is simply not satisfied.

Constraints

  • Up to 1,000 distinct queues.
  • An event has at most 50 fields and is nested at most 5 levels deep.
  • Strings are at most 100 characters long.
  • All values are standard JSON types: dict, list, str, int, float, bool, None.
  • operations and arguments have the same length, and every command name is either "subscribe" or "route".

Examples

Example 1

Input: run_router_script(['subscribe', 'subscribe', 'route', 'route'], [['queue-1', {'customer.country': ['US', 'CA'], 'order.total': {'$gte': 100}}], ['queue-2', {'event_id': {'$prefix': 'txn_'}}], [{'event_id': 'txn_98721', 'customer': {'country': 'US'}, 'order': {'total': 150.5}}], [{'event_id': 'audit_001', 'customer': {'country': 'UK'}, 'order': {'total': 200}}]])

Output: [None, None, ['queue-1', 'queue-2'], []]

Example 2

Input: run_router_script(['subscribe', 'subscribe', 'subscribe', 'route'], [['first', {}], ['second', {'kind': ['alert']}], ['first', {'kind': ['alert']}], [{'kind': 'alert'}]])

Output: [None, None, None, ['first', 'second']]

Hints

Hint 1

Keep a mapping from queue id to policy. Python dicts keep insertion order, and assigning to an existing key does not move it, which gives you the required update-in-place behavior.

Hint 2

Write a helper that walks a dotted path through nested dicts and reports failure if a key is missing or the current value is not a dict. Handle a null value correctly: it is present, but matches nothing.

Hint 3

In Python, bool is a subclass of int, so True == 1 and isinstance(True, int) are both true. Add explicit bool checks in both the equality helper and the numeric-bound check.