Given a REST API endpoint that returns paginated results with the following JSON response for page 2 with page size 3:
{
"page": 2,
"page_size": 3,
"total_items": 8,
"items": ["item4", "item5", "item6"]
}What will be the value of items in the response for page 3?
Think about how many items are left after page 2 and the page size.
Page size is 3, total items are 8. Page 1 has items 1-3, page 2 has items 4-6, so page 3 has items 7 and 8 only.
In page-based pagination, which parameter is necessary to specify which set of results to retrieve?
Page-based pagination uses page numbers to navigate.
The page parameter tells the API which page of results to return.
Consider this Python function that calculates the total number of pages:
def total_pages(total_items, page_size):
return (total_items + page_size - 1) // page_size
print(total_pages(25, 10))What is the output?
def total_pages(total_items, page_size): return (total_items + page_size - 1) // page_size print(total_pages(25, 10))
Think about how integer division rounds down and how the formula accounts for leftover items.
The formula rounds up the division to count partial pages. 25 items with page size 10 means 3 pages.
Given this Python code snippet for page-based pagination:
def get_page(items, page, page_size):
start = page * page_size
end = start + page_size
return items[start:end]
items = [1,2,3,4,5]
print(get_page(items, 1, 2))Why does it return [3,4] instead of the expected [1,2] for page 1?
def get_page(items, page, page_size): start = page * page_size end = start + page_size return items[start:end] items = [1,2,3,4,5] print(get_page(items, 1, 2))
Think about whether page numbers start at 0 or 1.
The function treats page 1 as the second page because it multiplies page by page_size directly. To get the first page, it should subtract 1 from page.
You have a dataset with 103 items. You want to paginate them with a page size of 20 items per page.
How many pages will the API return?
Divide total items by page size and round up.
103 divided by 20 is 5.15, so you need 6 pages to cover all items.