Skip to content
tezvyn:
Menu

Detect a three-action user sequence in real-time at scale

Source: Wikipedia: Complex event processingHardHow cards are made

Tests stream processing and stateful pattern matching on unbounded data. A strong design uses a CEP engine with keyed event-time windows, pushes alerts via WebSocket, and trades state-memory for latency.

What's really being asked

This question evaluates your ability to design a distributed system for stateful pattern matching over high-volume, unbounded event streams with strict latency requirements. The interviewer wants to see that you understand complex event processing concepts, event-time semantics, keyed state management, and the operational trade-offs between correctness, cost, and speed at scale.

The full answer

First, ingestion and serialization: use a distributed log like Kafka or Kinesis to collect action events with user-id keys to preserve ordering. Second, stream processing layer: deploy a CEP-capable engine such as Apache Flink, Kafka Streams, or Siddhi to evaluate the three-action sequence. Model the pattern as a finite state machine or use a built-in CEP operator with a strict event-time window, for example thirty to sixty seconds, to bound state. Third, state storage: maintain per-user partial matches in a local keyed state store backed by a changelog or remote KV store like RocksDB to survive restarts. Fourth, delivery: push the detected pattern to the user device immediately via WebSocket, SSE, or a mobile push gateway rather than relying on polling. Fifth, trade-offs: call out that event-time windows and exactly-once semantics add latency and memory overhead; large windows increase state size and GC pressure; out-of-order events require watermarking and may delay triggers; and shared devices or session resets can cause false positives.

The mistakes people make

Proposing to run a periodic SQL query over a data warehouse or OLAP store is a critical red flag because batch latency is measured in minutes, not milliseconds. Suggesting a simple application-level cache without addressing failover, backpressure, or event reordering reveals inexperience with stream processing. Ignoring the distinction between processing time and event time leads to incorrect matches when events arrive out of order. Overlooking delivery semantics, such as assuming the client polls an API every few seconds, fails the low-latency requirement.

What usually comes next

How would you handle actions that cross a session boundary or a device switch? What happens if the second action arrives five minutes late? How do you scale the state store when per-user state grows? How do you prevent duplicate tutorial triggers under exactly-once requirements? What is your recovery plan if the stream processor restarts mid-sequence?

A concrete example

Imagine a ride-sharing app where the sequence is open map, drop a pin, and tap schedule. Events are keyed by rider-id and flow into a Flink job. The job uses a CEP pattern with a forty-second event-time window and a watermark of five seconds. Partial matches are stored in Flink's keyed state backed by RocksDB with incremental checkpoints to S3. When the third action matches, the job writes a trigger record to a Redis pub-sub channel that the mobile gateway forwards over an open WebSocket. The trade-off is that riders on flaky networks may trigger watermarks late, delaying the tutorial by a few seconds, but the system avoids false positives from out-of-order events.

Interview question

Why must the pattern matcher use event-time windows rather than processing-time windows for this use case?

  • a.Event-time windows correctly handle out-of-order events and prevent incorrect matches when actions arrive delayed.Correct
  • b.Event-time windows remove the need for watermarks because the window boundary itself reorders events.
  • c.Event-time windows eliminate the need for a local keyed state store like RocksDB.
  • d.Event-time windows guarantee lower end-to-end latency than processing-time windows.
Why?

The card explicitly warns that ignoring the processing-time versus event-time distinction leads to incorrect matches when events arrive out of order, which watermarking addresses. Option D is a tempting distractor because the system has strict latency requirements, but event-time semantics and watermarking actually add latency in exchange for correctness.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles