Cursor Pagination for a Comments API That Survives New Inserts

Last updated: November 29, 2026

Quick Overview

Design and implement a newest-first comments endpoint that returns pages with an opaque cursor, so clients never see duplicates or skip comments even when new comments arrive while they scroll.

Meta
Software Engineering Fundamentals
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Medium

0

0

0 solved


Design and implement a newest-first comments endpoint that returns pages with an opaque cursor, so clients never see duplicates or skip comments even when new comments arrive while they scroll.

The Graph API and internal GraphQL endpoints at Meta page through comments, reactions and feed items with cursors rather than offsets. In an API design round the interviewer checks whether you understand why offset pagination breaks under concurrent inserts, how to pick a unique sort key, and what goes into a stable cursor contract.

What the Interviewer Expects
  • Explains concretely how OFFSET produces duplicates when new rows are inserted at the top
  • Uses a unique, stable ordering key such as (created_at, id)
  • Makes the cursor opaque so the server can change its encoding later
  • Fetches limit + 1 rows to compute has_more without a count query
  • Validates and caps the limit and rejects malformed cursors with a clear error
  • Gives the matching SQL with a row-value comparison and a supporting index
Key Topics to Cover
cursor pagination
API design
stable sort keys
keyset pagination
opaque tokens
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you support paging in both directions (before and after)?
  • What happens if a comment on the current page is deleted or edited?
  • How would you paginate a ranked list (top comments) whose scores change between requests?
  • Should the cursor be signed or encrypted, and why?
Sharpen Your Skills on Codemia

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

Practice System Design Problems
Sample Answer
Requirements
  • GET /posts/{id}/comments?limit=20&after=<cursor> returns {data, paging: {next}}.
  • Newest first. Inserting new comments must not shift what the next page returns.
  • next is null when there are...
High-Level Design

With OFFSET 20, a comment posted between page 1 and page 2 pushes everything down one slot, so the first item of page 2 is a repeat. Keyset pagination fixes this by remembering where the client st...


Submit Your Answer
Markdown supported