Implement Event Filtering and Queue Routing
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
An upstream event pipeline continuously emits events as JSON strings. You need to implement an in-memory routing service that decides which downstream queues should receive each event.
Implement an `EventRouter` with the following behavior:
- `void subscribe(String queueName, String filterExpression)`: registers a queue with a filter expression.
- `List<String> route(String eventJson)`: parses the incoming JSON event, evaluates all registered filters, and returns the names of every queue whose filter matches the event. Return queue names in subscription order. In a real system this method would send to queues, but for this exercise returning queue names is enough.
Filter-expression requirements:
- A filter compares JSON fields using dot paths, such as `type`, `region`, or `detail.userId`.
- Support comparison operators: `==`, `!=`, `>`, `>=`, `<`, `<=`.
- Support boolean operators: `AND`, `OR`, `NOT`, and parentheses.
- Values may be quoted strings, numbers, booleans, or `null`.
- Missing fields should be treated as `null`.
- Use JSON type-aware comparison where possible; for example, numeric comparisons should only be applied to numbers.
Example:
```text
subscribe("usOrders", "type == \"order\" AND region == \"us\"")
subscribe("largeOrders", "type == \"order\" AND amount >= 100")
subscribe("errors", "detail.level == \"error\" OR type == \"failure\"")
route("{\"type\":\"order\",\"region\":\"us\",\"amount\":125}")
=> ["usOrders", "largeOrders"]
```
You may assume a standard JSON parser/helper is available for converting the event string into a traversable JSON object. Focus on the service structure, filter parsing/evaluation, and routing behavior.
Quick Answer: This question evaluates parsing and evaluation of domain-specific filter expressions, JSON traversal and type-aware comparisons, boolean logic handling, and in-memory routing with subscription-order preservation.