Merge Sorted Event Streams from Web and App and Drop Duplicates
Last updated: November 29, 2026
Quick Overview
Meta clients sometimes log the same event from both the web and the app pipeline. Given several streams of (timestamp, event_id, payload) each sorted by timestamp, write Python that yields one merged, time-ordered stream with each event_id kept once.
Meta
November 29, 20260
0
0 solved
Meta clients sometimes log the same event from both the web and the app pipeline. Given several streams of (timestamp, event_id, payload) each sorted by timestamp, write Python that yields one merged, time-ordered stream with each event_id kept once.
Data engineering phone screens at Meta often ask for streaming-friendly Python rather than SQL. The interviewer wants a k-way merge that stays lazy, a clear dedupe rule, and a plan for memory when the stream is unbounded.
What the Interviewer Expects
- Uses heapq.merge or a manual heap for a lazy k-way merge
- Keeps the first copy of each event_id in timestamp order
- Explains O(N log k) time
- Bounds memory with a time-based eviction window for long streams
- Handles empty streams
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- What if streams are only approximately sorted?
- How do you pick the eviction window size?
- How would you do this in Spark or Flink?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Each input is an iterator of (ts, event_id, payload) sorted by ts. Duplicates share event_id but may differ slightly in ts. The output should be sorted by ts, with ties broken by event_id ...
Approach
heapq.merge performs a lazy k-way merge using a heap of size k. As events come out in order, keep a set of seen ids. For the bounded-memory version, keep a min-heap of (ts, id) and evict ids whose t...