← Grammarly Interview Insights
Started okay with the basic subscribe/publish interface, a map from topic to list of handlers, nothing fancy.
Start with a minimal in-memory pub/sub implementation using a map of topics to handler lists, then iteratively layer on wildcard matching, async delivery, durability, and delivery semantics while explicitly calling out trade-offs at each step. Treat the discussion as a design conversation: clarify requirements, sketch the API, then reason about thread safety, back-pressure, and scaling before diving into code-level details.
Pro tip: Anchor every extension in a concrete failure mode it addresses (e.g., wildcards for hierarchical topics, durable subscriptions for offline consumers, at-least-once for payment events) and explicitly state the trade-off you're accepting—this shows you design for real systems, not just features.
Ask about expected scale, delivery guarantees, ordering, and whether topics are hierarchical. Define subscribe(topic, handler), unsubscribe(topic, handler), and publish(topic, message) with clear semantics for return values and error cases.
Use a concurrent map from topic to a thread-safe collection of handlers. For publish, snapshot the handler list and invoke each handler, handling exceptions so one bad handler doesn't break others.
Support wildcard patterns (e.g., 'orders.*', 'orders.#') via a trie or regex-based matcher, and decouple publish from handler execution using a thread pool or per-subscriber queues to avoid blocking the publisher.
Persist messages for durable subscriptions (e.g., write-ahead log or broker-backed queue) and implement at-least-once (ack after processing, retry on failure) vs. at-most-once (fire-and-forget) with idempotency keys for deduplication.
Use concurrent data structures and locks for subscription changes, bounded queues with rejection or blocking for back-pressure, and partition topics across brokers or shard subscribers to scale horizontally.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.