← Questions
Filter events and route them to queues
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked May 2026. It tests design, rules and queues.
Build an in-memory router. Queues subscribe with a filter expression over an event's JSON fields; each incoming event goes to every queue whose filter it passes, in the order the queues subscribed. You write the filter parser and evaluator.
Interface
Unless you agree on a different interface with the interviewer, assume:
Router.subscribe(queue: str, filter: str) -> None-
Router.route(event: dict) -> list[str]— the queues that receive the event, in subscription order. - Filter grammar: comparisons
path OP literalwithOPin== != < <= > >=;pathis dotted (user.country); literals are numbers, double-quoted strings,true,false,null. Combine withAND,OR,NOTand parentheses;NOTbinds tighter thanAND, which binds tighter thanOR. - A missing path, or a comparison between different types (a number against a string), is false.
Example
subscribe("big_orders", 'type == "order" AND amount > 100')
subscribe("us", 'user.country == "US"')
subscribe("non_orders", 'NOT (type == "order")')
route({"type": "order", "amount": 250, "user": {"country": "US"}}) -> ["big_orders", "us"]
route({"type": "order", "amount": "250", "user": {"country": "CA"}}) -> [] # "250" is a string
route({"type": "refund", "amount": 40}) -> ["non_orders"]
Constraints
- Up to 10^3 subscriptions and 10^5 events; filters are short.
- An invalid filter is rejected at
subscribetime with an error.
Clarifying questions
- What is the precedence of
AND,ORandNOT? - How should a missing field compare, and does
NOTof a missing field match? - Are numbers integers or floats? Is
1 == 1.0true? - Can paths index into arrays (
items.0.sku)?
What a strong answer covers
- A tokenizer and a recursive-descent parser that turn each filter into a small tree once, at subscribe time.
- An evaluator that walks the event by path and compares with type checks.
- Clear semantics for missing fields and type mismatches, stated up front and tested.
- Routing in subscription order; O(subscriptions × filter size) per event, and how you'd index common equality filters to skip most of them.
- Error messages that point at the bad token.
Follow-ups
- Add
IN [..],EXISTS pathor stringCONTAINS. - Thousands of subscriptions: index by field and value so an event checks only candidates.
- Queues have limited capacity: what happens to an event when one is full?
Where it's asked
- Amazon · Technical phone screen · Last asked May 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
