Design Opaque Cursors for a Paginated Video Comments API

Last updated: November 29, 2026

Quick Overview

Design the pagination contract for GET /videos/{id}/comments, which returns comments ordered by creation time descending while new comments keep arriving. Implement encoding and decoding of an opaque cursor and the page function over an in-memory store.

TikTok
Software Engineering Fundamentals
Software Engineer
TikTok
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Design the pagination contract for GET /videos/{id}/comments, which returns comments ordered by creation time descending while new comments keep arriving. Implement encoding and decoding of an opaque cursor and the page function over an in-memory store.

Comment sections on popular TikTok videos receive new comments every second, so offset-based pagination shows duplicates or skips items as users scroll. This fundamentals question checks API contract design: keyset pagination, stable tie-breaking, opaque cursors and validation.

What the Interviewer Expects
  • Explains why offset pagination breaks when new items are inserted at the top
  • Uses keyset pagination on (created_at, comment_id) for a stable total order
  • Makes the cursor opaque (encoded) and tamper-evident or at least validated
  • Defines limit bounds, empty pages, and the end-of-list signal in the response
  • Mentions the database index that makes each page query efficient
Key Topics to Cover
API design
keyset pagination
cursors
idempotency
indexes
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 'Top comments' ordering by likes, which change over time?
  • How do you handle deleted comments that a cursor points to?
  • How would you version the cursor format without breaking old app versions?
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
  • Request: video_id, optional cursor, limit (default 20, max 50).
  • Response: {items, next_cursor} where next_cursor is null on the last page.
  • Order: newest first, stable even with equal t...
High-Level Design

With offsets, if 5 new comments arrive between page 1 and page 2, OFFSET 20 re-shows the last 5 items of page 1. Keyset pagination instead says "give me items strictly older than the last one I saw"...


Submit Your Answer
Markdown supported