A. The calculation of skip should be (page - 1) * size
B. The function should use skip and limit parameters instead of page and size
C. The slice should be users[skip : size] instead of users[skip : skip + size]
D. The default size should be 1, not 10
Solution
Step 1: Analyze skip calculation
Skip should start from zero on page 1, so skip = (page - 1) * size.
Step 2: Check slicing correctness
Current skip calculation skips too many items, causing wrong slice.
Final Answer:
The calculation of skip should be (page - 1) * size -> Option A
Quick Check:
skip = (page - 1) * size [OK]
Hint: Page 1 means skip zero items [OK]
Common Mistakes:
Using page * size instead of (page - 1) * size
Confusing slice end index
Ignoring zero-based indexing
5. You want to create a FastAPI endpoint that returns paginated results along with total count of items. Which of the following code snippets correctly implements this pattern?
hard
A. def get_paginated(page: int = 0, size: int = 10):
data = get_all_data()
skip = (page - 1) * size
return {"total": len(data), "items": data[skip:skip+size]}
B. def get_paginated(skip: int = 0, limit: int = 10):
data = get_all_data()
return {"total": len(data), "items": data[skip:skip+limit]}
C. def get_paginated(offset: int = 0, count: int = 10):
data = get_all_data()
return {"total": len(data), "items": data[offset:count]}
D. def get_paginated(page: int = 1, size: int = 10):
data = get_all_data()
skip = page * size
return {"count": len(data), "results": data[skip:skip+size]}
Solution
Step 1: Check pagination calculation
def get_paginated(skip: int = 0, limit: int = 10):
data = get_all_data()
return {"total": len(data), "items": data[skip:skip+limit]} correctly uses skip and limit with default values and slices data properly.
Step 2: Verify total count and returned keys
def get_paginated(skip: int = 0, limit: int = 10):
data = get_all_data()
return {"total": len(data), "items": data[skip:skip+limit]} returns total count and paginated items with clear keys "total" and "items".
Final Answer:
def get_paginated(skip: int = 0, limit: int = 10):\n data = get_all_data()\n return {"total": len(data), "items": data[skip:skip+limit]} -> Option B
Quick Check:
Skip/limit with total count returned [OK]
Hint: Return total count and slice with skip/limit [OK]
Common Mistakes:
Wrong skip calculation with page * size
Incorrect slice indices causing empty results
Using page=0 instead of page=1 for pagination
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.