Cursor Pagination
Description
A list endpoint pages through records with a cursor instead of an offset, so that records inserted or deleted between two requests are never skipped or repeated. ids holds the record ids in strictly increasing order. A client asks for the page after cursor, the largest id it has already seen (0 when it has seen nothing), with at most pageSize records. Return the ids on that page: the records whose id is greater than cursor, in ascending order, at most pageSize of them. The cursor does not have to be an id that still exists.
Examples
ids = [3,8,12,20,21,30], cursor = 8, pageSize = 3[12,20,21]The client has seen everything up to id 8, so the page starts at the first id above 8, which is 12. The page size of 3 takes 12, 20 and 21, leaving 30 for the next request.
ids = [3,8,12,20,21,30], cursor = 0, pageSize = 4[3,8,12,20]A cursor of 0 means nothing has been seen yet, so the page starts at the beginning and holds the first four ids.
ids = [3,8,12,20,21,30], cursor = 15, pageSize = 10[20,21,30]No record has id 15, perhaps because it was deleted after the client saw it, but the cursor still works: the page holds every id above 15. Only three remain, which is fewer than the page size.
Constraints
- •
0 ≤ ids.length ≤ 10⁵ - •
1 ≤ ids[i] ≤ 10⁹, strictly increasing - •
0 ≤ cursor ≤ 10⁹ - •
1 ≤ pageSize ≤ 10⁵
Ready to solve this problem?
Practice solo and sharpen your skills for technical interviews.