Implement basic pagination for a list
CoinbaseSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's competence in array indexing, boundary-condition handling, and simple pagination logic, focusing on correct subarray extraction and off-by-one considerations within given size constraints.
You are given:
- A zero-indexed array of items
items(you can assume it fits in memory). - An integer
pageSize > 0.
Design and implement a pagination helper that supports the following operations:
- Initialize the paginator with the array and
pageSize. - Given a page number
pageIndex(0-based), return the subarray of items that belongs to that page.
- Page 0 contains items with indices
[0, pageSize - 1](if they exist), - Page 1 contains
[pageSize, 2 * pageSize - 1], and so on.
- If
pageIndexis out of range (no items on that page), return an empty list.
Assume:
0 <= pageIndex1 <= pageSize <= 10^50 <= len(items) <= 10^6
Define the interface and implement the core method(s) for pagination. Focus on clean indexing and handling boundary cases correctly.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.