Challenge - 5 Problems
Pagination Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI pagination endpoint?
Consider this FastAPI endpoint that returns a paginated list of items. What will be the JSON response when requesting page=2 with size=3?
FastAPI
from fastapi import FastAPI, Query from typing import List app = FastAPI() items = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"] @app.get("/items/") async def get_items(page: int = Query(1, ge=1), size: int = Query(3, ge=1)) -> List[str]: start = (page - 1) * size end = start + size return items[start:end]
Attempts:
2 left
💡 Hint
Calculate the start and end indices for page 2 with size 3.
✗ Incorrect
Page 2 with size 3 means items from index 3 to 5 (0-based). The items list sliced from 3 to 6 is ["date", "elderberry", "fig"].
📝 Syntax
intermediate2:00remaining
Which option correctly implements limit-offset pagination in FastAPI?
You want to implement limit-offset pagination in FastAPI. Which code snippet correctly returns the paginated items?
FastAPI
items = ["a", "b", "c", "d", "e", "f", "g"] @app.get("/items/") async def get_items(limit: int = 3, offset: int = 0): # return paginated items here
Attempts:
2 left
💡 Hint
Remember slicing syntax: list[start:end]
✗ Incorrect
To get items starting at offset and up to limit count, slice from offset to offset+limit.
🔧 Debug
advanced2:00remaining
Why does this cursor pagination code raise an error?
This FastAPI cursor pagination code raises a TypeError. What is the cause?
FastAPI
from fastapi import FastAPI app = FastAPI() items = ["x", "y", "z"] @app.get("/items/") async def get_items(cursor: int = None, size: int = 2): start = cursor or 0 end = start + size return items[start:end] # Calling /items/?cursor=abc&size=2 causes the error
Attempts:
2 left
💡 Hint
Check how FastAPI handles query parameter types.
✗ Incorrect
FastAPI automatically validates query parameters. Passing 'abc' to an int parameter causes a validation error before the function executes.
🧠 Conceptual
advanced2:00remaining
What is a key advantage of cursor pagination over limit-offset pagination?
In FastAPI or any web API, why might you choose cursor pagination instead of limit-offset pagination?
Attempts:
2 left
💡 Hint
Think about data consistency when items are added or removed.
✗ Incorrect
Cursor pagination uses a stable reference point, so it prevents skipping or duplicating items if the data changes while paginating.
❓ state_output
expert2:00remaining
What is the value of 'next_cursor' after this FastAPI cursor pagination call?
Given this code, what is the value of 'next_cursor' returned when calling /items/?cursor=1&size=2?
FastAPI
from fastapi import FastAPI from typing import Optional app = FastAPI() items = ["a", "b", "c", "d", "e"] @app.get("/items/") async def get_items(cursor: Optional[int] = None, size: int = 2): start = cursor or 0 end = start + size page_items = items[start:end] next_cursor = end if end < len(items) else None return {"items": page_items, "next_cursor": next_cursor}
Attempts:
2 left
💡 Hint
Calculate end index and compare with list length.
✗ Incorrect
Cursor=1, size=2 means start=1, end=3. Since 3 < 5 (length), next_cursor is 3.