Skip to content
← Bug of the week

Bug of the week · 2026-10-26

Pagination that loses the last item

A list endpoint in a REST API · Logic · difficulty 1/3

A colleague asked the AI assistant: "Write a function paginate(items, page, pageSize) for our list endpoint. Pages start at 1. Return the items of the requested page plus pagination info."

The code came back in thirty seconds, with passing tests. The contract promised by the docstring:

  • page starts at 1; pageSize is the maximum number of items per page.
  • items contains the items of the requested page, in their original order.
  • totalPages is the number of pages needed for all items.
  • hasNext is true when there is a page after the requested one.

Review it before it goes in.

No account needed. No hints. The clock is running.0 s

Click the line (or lines) where the defect is, pick its category, then submit. With the keyboard: arrow keys to move, Space to mark. The code is read-only in this mode.

1export type Page<T> = {
2 items: T[];
3 page: number;
4 pageSize: number;
5 totalItems: number;
6 totalPages: number;
7 hasNext: boolean;
8};
9
10/**
11 * Returns page `page` (starting at 1) of `items`, with `pageSize` items per page.
12 */
13export function paginate<T>(items: T[], page: number, pageSize: number): Page<T> {
14 const totalItems = items.length;
15 const totalPages = Math.floor(totalItems / pageSize);
16 const start = (page - 1) * pageSize;
17 const end = start + pageSize;
18 return {
19 items: items.slice(start, end),
20 page,
21 pageSize,
22 totalItems,
23 totalPages,
24 hasNext: page < totalPages,
25 };
26}
0 lines marked