Middle · Explain · API design

What does the keyset continuation mean, and why does a bounded result page not bound this in-memory query work?

Think through your answer before opening the explanation.

Coding interviews

Reveal answer and reasoning

A continuation token describes the last ordered key returned, not a page number. This in-memory fixture assumes unique immutable integer IDs and returns IDs strictly greater than after, ordered ascending. Page size is constrained to one through three. The result array is bounded, but this LINQ implementation may still enumerate and sort a much larger input; it is not an indexed database query.

The unique ID is both an ordering key and a continuation boundary in this fixture. Strict greater-than means the previous last row is not repeated. An empty result has no last row, so a caller must not derive a continuation by blindly indexing the final element of every response.

For n source rows and m matching rows, filtering examines O(n) values and ordered-prefix evaluation buffers O(m) candidates. A full-sort baseline costs O(m log m), while .NET LINQ can optimize Take with partial selection; exact comparison behavior is implementation-dependent. An indexed ordered data source has another cost model. The small result page does not establish bounded total memory or an index seek.

Describe input size, matching-row count and requested page size separately. The accepted page limit is a response bound; it does not prevent OrderBy from buffering qualifying values. An interview answer should avoid claiming database-index complexity for a LINQ-to-Objects pipeline that has no database or index.

What a strong answer covers

  • A continuation token describes the last ordered key returned, not a page number.
  • Sorting occurs after filtering, which means the continuation is interpreted against the entire current source rather than against its incidental array position.
  • For n source rows and m matching rows, filtering examines O(n) values and ordered-prefix evaluation buffers O(m) candidates.

Common pitfall

Nonunique ordering keys can skip or repeat rows across pages unless a unique secondary key becomes part of the token. Concurrent inserts and deletes also require an explicit consistency policy. Local arrays provide no snapshot token, authorization boundary or tamper-resistant continuation encoding.

Follow-up questions

  1. Insert a new row below and above the first continuation, then predict the next page. Hint: strict greater-than excludes earlier keys; that behavior is not the same as a frozen snapshot. Test the final empty page without indexing its last element.
  2. Which data must a continuation token bind when rows belong to different tenants?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions