In this article
A timestamp alone cannot identify a page boundary when several rows share it. In an AI-permitted practice interview, use that missing row to explain your ordering rule, cursor fields, and consistency limits.
The third row disappears between two valid-looking pages
Three events share timestamp 100. The first page contains two of them. The next request asks for events created after timestamp 100 and skips the third. Both pages look individually reasonable; together they have lost a record. This is a useful practice problem because the failure forces you to explain what a cursor actually identifies.
Assume an interview exercise that explicitly permits a coding assistant. The fixture and dialogue here are illustrative, not claims about any employer assessment. You can ask the assistant for an implementation, but you should choose the expected pages yourself. Start by writing the four IDs in order on paper: 1, 2, 3, 4. If ID 3 vanishes, the explanation needs to account for it.
Give equal timestamps different IDs
Use numeric timestamp ticks to keep parsing and time zones outside this exercise. Rows have validated integer timestamps and unique positive integer IDs. Both fields remain unchanged while paging. The input arrives out of order, so the implementation also needs a stated sort rule. A page size of two makes the boundary cut directly through the three-way timestamp tie.
This broken helper sorts by timestamp, then filters using only that timestamp. MDN documents that sort preserves the original order of entries with equal comparison keys. That stability does not invent an ID tie-breaker or make the next-page filter include the remaining tied row. The copy before sort protects the original array order. MDN: Array.sort comparators and stability
import assert from 'node:assert/strict';
const rows = Object.freeze([
Object.freeze({ id: 4, createdAt: 101 }),
Object.freeze({ id: 1, createdAt: 100 }),
Object.freeze({ id: 2, createdAt: 100 }),
Object.freeze({ id: 3, createdAt: 100 }),
]);
function timestampPage(input, after = null, size = 2) {
return [...input].sort((a, b) => a.createdAt - b.createdAt)
.filter(row => after === null || row.createdAt > after)
.slice(0, size);
}
const brokenFirst = timestampPage(rows);
const brokenSecond = timestampPage(rows, brokenFirst.at(-1).createdAt);
assert.deepEqual(brokenFirst.map(row => row.id), [1, 2]);
assert.deepEqual(brokenSecond.map(row => row.id), [4]);
assert.throws(() => assert.deepEqual(
brokenSecond.map(row => row.id), [3, 4]
), { code: 'ERR_ASSERTION' });
Order by timestamp, then by ID
A concise explanation is: timestamp chooses the group; ID chooses a position within the group. The cursor must carry both values. After (100, 2), a row qualifies when its timestamp is greater than 100, or when its timestamp equals 100 and its ID is greater than 2. That admits both (100, 3) and (101, 4).
Changing greater-than to greater-than-or-equal on the timestamp is not enough. It admits the first page again and can prevent progress. Sorting by ID without changing the cursor is also insufficient: the filter still throws away the remaining tied records. State the ordering and the eligibility predicate together, then check that they describe the same boundary.
Use the same comparison for sorting and continuation
The comparator below puts timestamp first and ID second. Reusing it in the continuation predicate keeps the two rules aligned. Array.filter retains rows that satisfy that predicate; the slice then chooses the requested number from the ordered eligible rows. The fixture is small enough to inspect directly, so a database query would obscure the point being tested. MDN: Array.filter selection behavior
The returned cursor copies the last row key. It is a boundary value rather than a lookup requiring that row to remain present. Page size is validated here, while row and cursor fields are assumed to have passed the exercise input checks. An API must validate decoded cursors separately. This helper returns a cursor for every nonempty page, so callers make one final empty request to discover exhaustion.
function compareRows(a, b) {
return a.createdAt - b.createdAt || a.id - b.id;
}
function page(input, after = null, size = 2) {
if (!Number.isSafeInteger(size) || size <= 0) {
throw new RangeError('page size must be a positive safe integer');
}
const items = [...input].sort(compareRows)
.filter(row => after === null || compareRows(row, after) > 0)
.slice(0, size);
const last = items.at(-1);
return {
items,
nextCursor: last ? { createdAt: last.createdAt, id: last.id } : null,
};
}
Check page contents and a deleted boundary row
These assertions expect page one to contain IDs 1 and 2 and page two to contain 3 and 4. They also check the cursor itself, reversed input order, exhaustion, and the original array order. Exact IDs matter: an assertion that both pages have at most two items would accept the missing-row bug. Comparator support in sort gives the implementation its mechanism; the literal expected pages supply an independent check. MDN: Array.sort comparators and stability
The deleted-row case removes ID 2 after page one. Continuation should still produce IDs 3 and 4 because its boundary values remain meaningful. This is a narrow guarantee about deletion of the boundary row. It does not promise to return an event that has itself been deleted before the next request. Make that distinction when explaining what the test establishes.
const firstPage = page(rows);
assert.deepEqual(firstPage.items.map(row => row.id), [1, 2]);
assert.deepEqual(firstPage.nextCursor, { createdAt: 100, id: 2 });
const secondPage = page(rows, firstPage.nextCursor);
assert.deepEqual(secondPage.items.map(row => row.id), [3, 4]);
assert.deepEqual(page([...rows].reverse()).items.map(row => row.id), [1, 2]);
assert.deepEqual(page(rows.filter(row => row.id !== 2), firstPage.nextCursor)
.items.map(row => row.id), [3, 4]);
assert.deepEqual(page([...rows, { id: 5, createdAt: 99 }], firstPage.nextCursor)
.items.map(row => row.id), [3, 4]);
assert.deepEqual(page([...rows, { id: 6, createdAt: 100 }], firstPage.nextCursor)
.items.map(row => row.id), [3, 6]);
assert.deepEqual(page(rows, secondPage.nextCursor), { items: [], nextCursor: null });
assert.deepEqual(page([]), { items: [], nextCursor: null });
assert.deepEqual(rows.map(row => row.id), [4, 1, 2, 3]);
assert.throws(() => page(rows, null, 0), RangeError);
console.log('pagination boundary checks passed');
Review the assistant through the missing record
An illustrative review request is: show why ID 3 appears on the second page, and show why IDs 1 and 2 cannot appear there. That asks for evidence about both inclusion and exclusion. If the assistant proposes a different cursor encoding, decode it and inspect the two underlying fields before accepting a longer implementation.
When speaking to the interviewer, connect each change to the failure: I added ID to the ordering and cursor because timestamp alone could not represent where we stopped inside a tie. Then point to the two page assertions. You do not need a rehearsed speech about every pagination technique. Explain this boundary, the result you checked, and the next requirement that would change the design.
Deterministic continuation is different from a fixed snapshot
Suppose a new row appears at (99, 5) after the first page. It sorts before the saved boundary and will not appear in later pages. A new row at (100, 6) sorts after the boundary and can appear. Both cases are checked above. The helper has no record of what the dataset contained at the beginning. Adding a tie-breaker fixes the tied-row gap for a fixed collection; it does not freeze a changing collection.
Ask what the caller needs: a continuing live feed or every row that belonged to one fixed export. A fixed export requires an additional consistency design, such as a stored snapshot or a suitable database transaction strategy. The exercise does not implement either. For a large feed, move ordering and selection to an appropriately indexed data store; do not describe this in-memory sort as a database performance solution.
Common questions about the pagination boundary
Could the cursor contain just the ID? Only if the next request can reconstruct the exact ordering key and the contract handles missing or changed rows. Carrying the pair avoids that lookup in this exercise. Does reversing the sort make a newest-first feed? Change the continuation comparison too, and write new expected pages for the descending order.
Do I need an opaque cursor? An API may encode the fields so callers treat them as one token, but encoding alone provides neither validation nor authorization. Why not use offsets? They can be a reasonable product choice. This exercise asks you to explain a cursor boundary; changing pagination style would require a new discussion about inserts, deletes, access patterns, and caller expectations.
Sources & editorial notes
This article was authored with AI using primary-source research and an automated technical review. It has not been reviewed by a human. Code examples are illustrative; run and verify them in your own environment.
- MDN: Array.sort comparators and stability
Checked November 30, 2026. Documents comparator ordering, stable ties, and mutation by Array.sort; the cursor policy is illustrative analysis.
- MDN: Array.filter selection behavior
Checked November 30, 2026. Documents predicate-based array selection used before taking the requested page size.
Put the reading into practice
Work through an existing repository, review AI suggestions, and test the decisions behind your changes.
Explore practice problems