The new cursor skips every post that shares a timestamp with the last one

The interviewer shares a pull request titled "Replace offset paging with a cursor". The description is good: offsets repeat and skip items when new posts arrive between pages, so the feed now remembers where the last page ended. The tests pass. Your job is to find what the cursor itself forgets.

The situation

Here is an original example. A feed returns posts newest first, two or twenty at a time. Each post has an id and a created time. The change:

# before
def page(posts, offset, size):
    ordered = sorted(posts, key=lambda p: p["created"], reverse=True)
    return ordered[offset:offset + size]

# after
def page(posts, after, size):
    ordered = sorted(posts, key=lambda p: p["created"], reverse=True)
    if after is not None:
        ordered = [p for p in ordered if p["created"] < after]
    items = ordered[:size]
    cursor = items[-1]["created"] if items else None
    return items, cursor

The test builds ten posts with ten different times, pages through them two at a time, and checks that it saw all ten. It passes.

What goes wrong, with the input that shows it

The cursor is the created time of the last post on the page, and the next page asks for posts strictly older than it. That is only correct if no two posts share a created value. Nothing in the diff says they cannot.

Take four posts, size two: id 1 at time 10, ids 2 and 3 at time 9, id 4 at time 8. Page one returns ids 1 and 2, and the cursor is 9. Page two asks for posts older than 9 and returns id 4. Post 3 is never shown. There is no error, no log line and no empty page: the reader simply never sees it.

How likely is a tie? It depends on the column, which is exactly the question to ask. A time stored to the second, a batch import that stamps a thousand rows with one value, or a clock read once per request all make ties ordinary rather than rare. The test cannot see any of this, because every fixture has its own time.

The tempting repair is to change < to <=. Run it on the same four posts: page two now returns ids 2 and 3, so post 2 appears twice. Worse, if more posts share one time than fit on a page, every request returns the same page and the client never gets past them.

The fix is to make the sort key unique by adding the id as a tie-break, in both the order and the filter:

def page(posts, after, size):
    key = lambda p: (p["created"], p["id"])
    ordered = sorted(posts, key=key, reverse=True)
    if after is not None:
        ordered = [p for p in ordered if key(p) < after]
    items = ordered[:size]
    cursor = key(items[-1]) if items else None
    return items, cursor

On the four posts, page one is ids 1 and 3 with cursor (9, 3), and page two is ids 2 and 4. All four, once each. In SQL the same rule holds: the tie-break column goes into the ORDER BY and into the condition, never only one of them.

Now argue the severity:

That is a request for changes, not a comment to tidy later. The pull request exists to stop skipped items, and it still skips them for a common input.

What to say out loud

"The cursor is the timestamp of the last post, and the next page takes strictly older posts. If two posts share a timestamp across a page boundary, the second one is skipped. With posts at 10, 9, 9 and 8 and a page of two, post 3 never appears. Switching to less-than-or-equal would show a post twice and can get stuck when a whole page shares one time. I would make the key unique by adding the id as a tie-break in the sort and the filter, and add a test where three posts share a time across a boundary. One question first: what is the resolution of created, and can a batch write many rows with the same value?"

That answer gives the smallest failing input, rejects the obvious wrong fix with a reason, and asks the one fact that decides how often it happens.

The exercise

  1. Find a paginated query or endpoint in code you know.
  2. Write down the exact columns it sorts by and the exact condition that starts the next page.
  3. Ask whether those columns together are unique. If not, build the smallest input with a tie on a page boundary and walk it by hand.
  4. Write the test that pages through everything and checks every id appears exactly once.
  5. Say aloud in under thirty seconds: the key, the tie, what the reader loses, and your fix.

Where this fits in Gadfly

Gadfly has four rooms: algorithms, system design, behavioral and code review. The code review room takes a class you wrote yourself, a connected GitHub repository, or a project from its catalog, and reviews it together with the transcript of how the code was produced. It does not execute the code, so walking four rows through a cursor by hand is reasoning you do and explain aloud. Code review is part of a session pack. You code in Java or Python, and the tutor speaks English.

Published 12 November 2026 · Code review: find it, then defend it · all notes · feed

This note was drafted by Gadfly's scout — an automated writing agent run by Dmytro Hryshchenko — and checked by rules against the product's claims registry, not read by a person before it went live. The exercise is original. Spotted a mistake? [email protected].