0
0
Rest APIprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Offset 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 offset-based pagination calculation?

Given a REST API that uses offset-based pagination, the client requests page 3 with a page size of 10. What is the offset value sent to the server?

Rest API
page = 3
page_size = 10
offset = (page - 1) * page_size
print(offset)
A20
B30
C3
D10
Attempts:
2 left
💡 Hint

Offset is how many items to skip before starting to collect the page.

🧠 Conceptual
intermediate
2:00remaining
Which statement best describes offset-based pagination?

Choose the statement that correctly describes offset-based pagination in REST APIs.

AIt only works with APIs that return data sorted by timestamp.
BIt uses a page number and page size to calculate how many items to skip before fetching data.
CIt fetches all data at once and then slices it on the client side.
DIt uses a cursor to mark the last item fetched and fetches the next set after it.
Attempts:
2 left
💡 Hint

Think about how offset is calculated using page number and size.

🔧 Debug
advanced
2:30remaining
Why does this offset-based pagination code return an empty list on page 1?

Look at the code below. It is supposed to return the first page of results but returns an empty list. What is the cause?

Rest API
def get_page(data, page, page_size):
    offset = page * page_size
    return data[offset:offset + page_size]

items = list(range(5))
print(get_page(items, 1, 10))
AThe slice syntax is incorrect; it should be data[offset:page_size].
BThe data list is empty, so it always returns an empty list.
CThe offset calculation should subtract 1 from page before multiplying by page_size.
DThe page_size should be added to offset twice.
Attempts:
2 left
💡 Hint

Remember that page 1 means no items skipped.

📝 Syntax
advanced
2:00remaining
Which option correctly implements offset-based pagination in Python?

Choose the code snippet that correctly calculates offset and slices the data list for pagination.

Rest API
data = list(range(100))
page = 4
page_size = 15
A
offset = (page - 1) * page_size
page_data = data[offset:offset + page_size]
B
offset = page * page_size
page_data = data[offset:offset + page_size]
C
offset = (page + 1) * page_size
page_data = data[offset:offset + page_size]
D
offset = page_size - page
page_data = data[offset:offset + page_size]
Attempts:
2 left
💡 Hint

Offset must skip items from previous pages.

🚀 Application
expert
3:00remaining
How many items will be returned by this offset-based pagination request?

Assume a dataset of 95 items. A client requests page 10 with a page size of 10 using offset-based pagination. How many items will the server return?

A0
B10
C15
D5
Attempts:
2 left
💡 Hint

Calculate offset and see how many items remain after skipping.