Time-Indexed Service for Multi-Topic Vehicle Recording Files
System DesignModel answerLast updated
Design and build a service that ingests recorded vehicle sessions from MCAP files, stores them with a time index, and exposes APIs to list sessions and fetch messages by timestamp.
You are given a set of recorded vehicle drives, each called a run. Runs are stored in MCAP, an open container format for timestamped, multi-topic log data. Each run holds messages on topics such as camera (images or video frames) and location (position data). The MCAP files carry their own schemas, and every message payload is encoded with Protocol Buffers. Every message has a timestamp, and timestamps differ between messages and between topics.
Conceptually, the data is laid out like this:
run_a
camera
message_1
message_2
location
message_1
message_2
Build a small service over this data. You may use an AI coding assistant, and you may pick the language and storage (Python with SQLite is a reasonable fit for a short session). The full feature list is long, so decide which features are required and build those first. The two known requirements are:
- list all runs;
- query messages by timestamp, which means the stored data needs an index on time.
You will finish with a demo and be asked how you worked with the assistant.
Part 1: Ingest and store the runs
Read every run, decode its messages, and store them so they can be queried. One option keeps message metadata (run, topic, timestamp) apart from raw payloads; another places every decoded message in a single database. Choose one and justify it.
Cover:
- how you read MCAP, handle schemas and Protobuf decoding, and which timestamp you keep;
- the data model for runs, topics and messages, and where large payloads such as images live;
- one store versus split metadata and payload storage, including consistency;
- ingestion that is fast and safe to re-run.
Part 2: List runs and query by timestamp
Design and implement the API: list all runs, and fetch messages by timestamp. Timestamp queries must be fast.
Cover:
- endpoints, and paging for large results;
- index design, and how you verify the database really uses the index;
- timestamp units, precision and boundary semantics;
- behavior for unknown runs or topics and for windows with no messages.
State which clarifying questions you would ask first (for example: which timestamp to query on, whether a query is a range, an exact value or a nearest match, and whether clients need decoded payloads or only metadata).
Part 3: Working with an AI assistant and presenting
Explain how you would use the assistant to explore unfamiliar data and a long specification, how you make sure its output is correct, and how you would present the result (demo, tests, documentation). Expect the question: "How did you interact with the AI?"
Hints
Hint 1
Pin down the query semantics first: which timestamp (log time vs publish time vs a field inside the payload), and whether queries are ranges, exact matches or nearest-message lookups. This determines the index.
Hint 2
Store timestamps as 64-bit integers in their native unit (MCAP uses nanoseconds), and create a composite index such as (run_id, topic, timestamp) so a range scan is a single index seek. Check it with EXPLAIN QUERY PLAN.
Hint 3
Keep large camera payloads out of the hot index path (blob column in a separate table, or files referenced by path), and use keyset paging on (timestamp, id) instead of OFFSET.
Hint 4
For the AI-assistant part, describe a loop: let it summarize the spec and inspect the files, you decide priorities, then verify with tests built from facts you can check independently (message counts, first and last timestamps).
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarify first
Before writing code, I would settle these points, because they drive the schema and the index:
- Which timestamp? MCAP records both a log time and a publish time per message, and the Protobuf payload may carry its own. I would default to log time, since every message has it and it is the basis for ordering in the file, and expose the others only if the spec asks.
- Query shape. A range
[start, end), an exact value, or "nearest to t". Range queries are the common case; nearest needs two lookups (one at or before, one at or after). - Payload needs. Decoded content, raw bytes, or metadata only. This decides whether payloads are returned inline or fetched separately.
- Scale and layout. Number and size of runs, and whether a run is one MCAP file with several channels or several files.
- UI or API only. I would assume API plus a command-line or simple demo unless told otherwise.
I would also ask which features are required, and build those before anything optional.
Part 1: Ingestion and storage
Reading. Use the MCAP reader library to iterate messages. Each message gives a channel (topic), a schema, a log time, a publish time and the payload bytes. Decode Protobuf using the schema embedded in the file (build the message class from the descriptor set in the schema record), rather than hard-coding generated classes. This keeps the service correct when schemas differ between runs.
Data model.
runs(id, name, source_path, start_ns, end_ns, message_count)topics(id, run_id, name, schema_name)messages(id, run_id, topic_id, ts_ns INTEGER, publish_ns INTEGER, payload_ref)payloads(message_id, bytes)or files on disk
One store versus split. I would split metadata from payloads. Metadata is small, and it is what the index scans; camera frames are large and would bloat the B-tree pages and slow range scans. The trade-off is consistency: two stores can drift. I would mitigate it by writing payloads first, then committing metadata in one transaction, and by storing a content hash or length so a verification job can detect orphans. For a small exercise, a single SQLite file with a separate payloads table gives the same separation with transactional safety and is the pragmatic choice. If payloads were many gigabytes, I would move them to object storage or files keyed by run and offset, and could even point back into the original MCAP file by byte offset and avoid copying.
Decoded or raw. Store raw Protobuf bytes and decode on read. Decoding at ingest multiplies storage and loses fidelity if a schema changes. For location data, which is small, I might also store extracted latitude and longitude columns if the spec asks for spatial queries.
Safe re-runs. Make ingestion idempotent: a unique key on (run name, source file hash), and wrap each run in one transaction, deleting or skipping if it already exists. Use batched inserts (executemany) and create secondary indexes after bulk load if volume is large.
Part 2: API and index
Endpoints.
GET /runsreturns runs with name, start and end time, topics and counts.GET /runs/{run}/messages?topic=&start=&end=&limit=&cursor=returns messages in the half-open window[start, end), ordered by timestamp.GET /runs/{run}/messages/{id}/payloadreturns the raw or decoded payload.- Optionally
GET /runs/{run}/nearest?topic=&ts=.
Index. Store ts_ns as a 64-bit integer, never a float or string, since nanosecond epoch values exceed float precision (about 2^53) and exact comparisons matter. Create CREATE INDEX idx_msg ON messages(run_id, topic_id, ts_ns, id). A window query is then one seek plus a sequential scan: O(log n + k) for k results. Confirm with EXPLAIN QUERY PLAN, which should show SEARCH ... USING INDEX idx_msg (run_id=? AND topic_id=? AND ts_ns>? AND ts_ns<?) and not SCAN. Note that timestamps across topics differ, so the index must include the topic (or the query must handle merging topics by timestamp).
Paging. Use keyset paging on (ts_ns, id) rather than OFFSET, since offset cost grows with depth and duplicates in timestamps would break naive cursors. Cap limit (for example 1,000).
Edge cases.
- Unknown run or topic: 404 with a clear message, not an empty list.
- Valid run, window with no messages: 200 with an empty list.
start > end: 400.- Boundaries: document that start is inclusive and end is exclusive.
- Units: document nanoseconds, and accept them as integers in the API (strings in JSON if clients are JavaScript, to avoid precision loss).
- Duplicate timestamps: tie-break by id.
Part 3: Working with the AI assistant
Exploration. Use the assistant to summarize the long spec into a prioritized checklist (required vs optional), and to write small scripts that print topics, schemas, message counts and time ranges for each run. I confirm its reading of the spec against the original text.
Division of labor. I own the design decisions: which timestamp, schema, index and paging. The assistant drafts boilerplate such as the reader loop, endpoint scaffolding and test fixtures. I review every generated line that touches timestamps, transactions or SQL.
Verification.
- Independent facts: message counts per topic and first and last timestamps, computed with the MCAP CLI or a separate script, must match what the service reports.
- Tests: a tiny synthetic MCAP built in the test, covering boundaries (message exactly at start and at end), duplicate timestamps, empty windows, unknown run and paging across multiple pages with no gaps or repeats.
- Performance check:
EXPLAIN QUERY PLANplus a timing on a large run. - Round-trip: decode a payload and compare to the source message.
Prioritization and demo. Build list-runs and timestamp query end to end first, with tests, then stretch features (nearest lookup, payload download, a simple UI). The demo walks through: ingest a run, list runs, run a window query, show the query plan using the index, and show a boundary case. A short README documents setup, timestamp semantics and design trade-offs.
Honest account. When asked how I used the AI, I would describe concrete moments: where it saved time, one place where its output was wrong or subtly off (for example a unit or boundary error) and how a test caught it, and what I deliberately did not delegate.
Common mistakes
- Using floats or strings for timestamps, or mixing seconds and nanoseconds.
- Indexing only on time when queries are per run and topic.
- Using
OFFSETpaging, or ignoring duplicate timestamps. - Storing large images in the same table that the index scans.
- Building a UI before the required API works.
- Trusting generated code without tests built from independently known facts.