The interview problem

Practice prompt

A database tool returns a million rows when the agent expected a short answer. Design a result protocol that bounds memory and model context without pretending a partial result is complete.

Original practice, not a reported company interview. All worked numbers are illustrative. Interview date: not applicable. Written October 3, 2026.

1. Scope and assumptions

Limit both transport bytes and model tokens; neither reliably predicts the other. Assume a read-only query and server-side cursors. Authorization must apply to every page, not only cursor creation.

2. Architecture and decisions

Query → bounded result store → manifest and preview → scoped page requests → completeness-aware answer

Return a manifest containing query identity, snapshot identity, total-count status, truncation reason and a next-page handle. Stream rows into bounded storage instead of buffering the whole response in the agent worker. Provide a small typed preview, then let the agent request aggregates or selected ranges. Bind opaque cursor handles to caller, tenant and expiry.

Use a deterministic ordering with a stable tie breaker. If a consistent snapshot is too expensive, explicitly describe that pages can reflect changes. A summary must include coverage and sampling method. Enforce decompressed byte limits before parsing; a small compressed response can still be enormous. Escape data so a row containing instructions remains data.

3. Work through the example

One million rows averaging an assumed 500 bytes produce 500 MB. A 100-row preview is roughly 50 KB, a 10,000-fold reduction. That byte saving does not guarantee the preview fits the token budget. Count tokens separately and shorten large individual fields with explicit omission markers.

4. Failure cases and trade-offs

A cursor over a changing sort key may skip or duplicate rows. Silent truncation creates confident but incomplete answers. Persisting all results improves reproducibility but increases retention and access-control obligations. When aggregate pushdown is available, it can be safer and cheaper than paging the full dataset.

5. Validation and acceptance

Test one giant row, invalid encoding, cursor replay by another tenant and mutation between pages. Ask a counting question whose answer differs between the preview and full result; the agent must request an aggregate or disclose incomplete coverage.

6. Follow-ups and self-review

How would you resume after cursor expiry? Reward separate byte/token bounds, snapshot semantics, scoped handles and visible incompleteness.

Sources and related practice

PostgreSQL cursor documentation supports incremental row retrieval; the agent-facing manifest and budgets are original.

Read the primary technical reference

Reference retrieved October 3, 2026. Retrieval date is not an interview or source publication date. The reference supports the named concept; scenario, architecture, examples and rubric are original.

Continue with Handle oversized inputs in a shared AI playground; Design a constrained text-to-SQL assistant.

Continue your preparation

Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.