0
0
FastAPIframework~20 mins

Pagination patterns in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pagination Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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]
A["banana", "cherry", "date"]
B["date", "elderberry", "fig"]
C["elderberry", "fig", "grape"]
D["apple", "banana", "cherry"]
Attempts:
2 left
💡 Hint
Calculate the start and end indices for page 2 with size 3.
📝 Syntax
intermediate
2: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
Areturn items[offset:offset+limit]
Breturn items[limit:offset+limit]
Creturn items[offset:limit]
Dreturn items[limit:offset]
Attempts:
2 left
💡 Hint
Remember slicing syntax: list[start:end]
🔧 Debug
advanced
2: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
AThe items list is empty causing an IndexError
BThe code tries to add an int and a string causing a TypeError inside the function
Ccursor is declared as int but receives a string, causing a validation error before the function runs
DThe default value for cursor is None which cannot be used in arithmetic
Attempts:
2 left
💡 Hint
Check how FastAPI handles query parameter types.
🧠 Conceptual
advanced
2: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?
ACursor pagination avoids skipping or repeating items when data changes during pagination
BCursor pagination is simpler to implement and requires less code
CCursor pagination always returns the total count of items
DCursor pagination allows random access to any page by number
Attempts:
2 left
💡 Hint
Think about data consistency when items are added or removed.
state_output
expert
2: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}
A2
BNone
C1
D3
Attempts:
2 left
💡 Hint
Calculate end index and compare with list length.