0
0
Rest APIprogramming~20 mins

Page-based pagination in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Page-based Pagination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this page-based pagination response?

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?

A["item6", "item7", "item8"]
B["item7", "item8", "item9"]
C["item7", "item8"]
D[]
Attempts:
2 left
💡 Hint

Think about how many items are left after page 2 and the page size.

🧠 Conceptual
intermediate
1:30remaining
Which parameter is essential for page-based pagination?

In page-based pagination, which parameter is necessary to specify which set of results to retrieve?

Acursor
Bpage
Climit
Doffset
Attempts:
2 left
💡 Hint

Page-based pagination uses page numbers to navigate.

Predict Output
advanced
2:00remaining
What is the output of this pagination calculation?

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?

Rest API
def total_pages(total_items, page_size):
    return (total_items + page_size - 1) // page_size

print(total_pages(25, 10))
A4
B2
CError
D3
Attempts:
2 left
💡 Hint

Think about how integer division rounds down and how the formula accounts for leftover items.

🔧 Debug
advanced
2:30remaining
Why does this pagination code return an empty list for page 1?

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?

Rest API
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))
ABecause the start index calculation should be (page - 1) * page_size
BBecause the items list is empty
CBecause the end index is calculated incorrectly and should be page * page_size
DBecause page indexing starts at 0, so page 1 is actually the second page
Attempts:
2 left
💡 Hint

Think about whether page numbers start at 0 or 1.

🚀 Application
expert
2:00remaining
How many pages will be returned for 103 items with page size 20?

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?

A6
B5
C7
D8
Attempts:
2 left
💡 Hint

Divide total items by page size and round up.