Build Aggregated Depth Levels From an Order Event Feed

Last updated: November 29, 2026

Quick Overview

Given a stream of order-level events (open, partial fill, cancel) with decimal prices and sizes, maintain the aggregated quantity at each price level and answer queries for the top N bid and ask levels. Levels whose quantity reaches zero must disappear.

Gemini
Coding & Algorithms
Software Engineer
Gemini
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a stream of order-level events (open, partial fill, cancel) with decimal prices and sizes, maintain the aggregated quantity at each price level and answer queries for the top N bid and ask levels. Levels whose quantity reaches zero must disappear.

Gemini publishes market data at several granularities, and clients of its WebSocket feeds often rebuild an aggregated book from order-level updates. The interviewer checks correct bookkeeping between per-order and per-level state and good choices for the query path.

What the Interviewer Expects
  • Track per-order remaining size so fills and cancels can update the right level
  • Remove a level exactly when its aggregate hits zero, using exact decimals
  • Sort bids descending and asks ascending for the top N
  • Ignore or log events for unknown order ids instead of crashing
  • Discuss a sorted structure for faster top N queries
Key Topics to Cover
Order Book
Hash Map
Sorting
Market Data
Decimal Arithmetic
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you detect a missed message using sequence numbers and recover with a snapshot?
  • How would you emit only the levels that changed to downstream subscribers?
  • What changes if queries for top 10 happen thousands of times per second?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Events: open(id, side, price, qty), fill(id, qty) reducing remaining size, cancel(id) removing the rest. Query top(n) returns up to n (price, qty) pairs per side, bids from highest price, as...

High-Level Design

Two maps: orders[id] = (side, price, remaining) and depth[side][price] = aggregate. Each event computes a delta for one level. If the aggregate becomes zero the level is deleted. The query sorts t...


Submit Your Answer
Markdown supported

Related Questions