← Questions

Filter events and route them to queues

0
AmazonCodingLast asked May 2026

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 literal with OP in == != < <= > >=; path is dotted (user.country); literals are numbers, double-quoted strings, true, false, null. Combine with AND, OR, NOT and parentheses; NOT binds tighter than AND, which binds tighter than OR.
  • 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 subscribe time with an error.

Clarifying questions

  • What is the precedence of AND, OR and NOT?
  • How should a missing field compare, and does NOT of a missing field match?
  • Are numbers integers or floats? Is 1 == 1.0 true?
  • 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 path or string CONTAINS.
  • 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

design · rules · queues

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