Listing Pagination - Implement Cursor-Based Pagination for a Product Listing API
Interview Experience
Round 1 Coding / System Design
Problem
Implement a ListingService that supports cursor-based pagination over a sorted collection of product listings. Clients request a page size and an optional cursor; the service returns the next N items and a cursor to fetch the following page.
python
class ListingService:
def __init__(self, listings: list[dict]):
# listings: [{"id": str, "price": float, "name": str}]
# pre-sorted by (price asc, id asc)
pass
def get_page(
self,
page_size: int,
cursor: str | None = None
) -> dict:
**Returns** {"items": [...], "next_cursor": str | None}
pass
Example
listings = [{"id":"a","price":10},{"id":"b","price":20},{"id":"c","price":30}]
svc = ListingService(listings)
page1 = svc.get_page(2) # items=[a,b], next_cursor="<cursor>"
page2 = svc.get_page(2, cursor) # items=[c], next_cursor=None
Follow-ups
- Why is cursor-based pagination preferred over OFFSET/LIMIT for large datasets?
- How do you encode the cursor so it is opaque to clients but decodable server-side?
- How do you handle items being inserted or deleted between page fetches?
- What changes if the sort order is by a non-unique column like
price?
Full Details
Round 1 Coding / System Design
Problem
Implement a ListingService that supports cursor-based pagination over a sorted collection of product listings. Clients request a page size and an optional cursor; the service returns the next N items and a cursor to fetch the following page.
python
class ListingService:
def __init__(self, listings: list[dict]):
# listings: [{"id": str, "price": float, "name": str}]
# pre-sorted by (price asc, id asc)
pass
def get_page(
self,
page_size: int,
cursor: str | None = None
) -> dict:
**Returns** {"items": [...], "next_cursor": str | None}
pass
Example
listings = [{"id":"a","price":10},{"id":"b","price":20},{"id":"c","price":30}]
svc = ListingService(listings)
page1 = svc.get_page(2) # items=[a,b], next_cursor="<cursor>"
page2 = svc.get_page(2, cursor) # items=[c], next_cursor=None
Follow-ups
- Why is cursor-based pagination preferred over OFFSET/LIMIT for large datasets?
- How do you encode the cursor so it is opaque to clients but decodable server-side?
- How do you handle items being inserted or deleted between page fetches?
- What changes if the sort order is by a non-unique column like
price?
About This Question
This is a candidate experience report from a mavenclinic interview during the phone round.
It covers the following topics: System Design, Coding, Phone, Onsite .