📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How do you support a 'previous page' link with keyset pagination?

Mid-Level Coding Intermediate

🎯 Key points

  • Keep first and last keys in prev/next cursors
  • Reverse the comparison and the ORDER BY for the previous page
  • Reverse the fetched page in memory before returning it
  • Take(limit + 1) tells you if there is an earlier page, keeping the read bounded
  • A unique tiebreaker handles duplicate sort values in both directions
  • One index serves both directions (backward scan)

⏱ 30-second answer

Return two cursors per page: prevCursor from the first row's key and nextCursor from the last. For the previous page, flip the comparison to rows before the first key (CreatedAt > @c OR (CreatedAt = @c AND Id > @i) for a newest-first list), flip the ORDER BY to ascending, Take(limit + 1) to know whether an earlier page exists, then reverse the rows in memory so they display newest first. Duplicate sort values are handled by the unique tiebreaker (Id) in both the predicate and the ORDER BY; without it, rows sharing a timestamp at the page edge are skipped or repeated. The same (CreatedAt DESC, Id DESC) index serves both directions because SQL Server can scan a B-tree backwards, so each page is still one seek plus limit + 1 rows.

🔍 What the interviewer is checking

Checks they can reverse the predicate and order, then restore the display order

↩ Usually asked after

More in EF Core Performance

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me