Bird
Raised Fist0
Rest APIprogramming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of page-based pagination in REST APIs?
easy
A. To split large data into smaller pages for easier loading
B. To combine all data into one large response
C. To sort data alphabetically
D. To encrypt data before sending

Solution

  1. Step 1: Understand pagination concept

    Pagination divides data into smaller parts called pages to avoid sending everything at once.
  2. Step 2: Identify purpose in REST APIs

    Page-based pagination uses page number and limit to load data in chunks, improving performance and user experience.
  3. Final Answer:

    To split large data into smaller pages for easier loading -> Option A
  4. Quick Check:

    Pagination = split data into pages [OK]
Hint: Pagination means splitting data into pages [OK]
Common Mistakes:
  • Thinking pagination sorts data
  • Confusing pagination with encryption
  • Assuming pagination combines all data
2. Which of the following is the correct way to request page 3 with 10 items per page using query parameters?
easy
A. /items?size=10&page=3
B. /items?limit=3&page=10
C. /items?page=10&limit=3
D. /items?page=3&limit=10

Solution

  1. Step 1: Identify standard query parameters

    Page-based pagination commonly uses page for page number and limit for items per page.
  2. Step 2: Match parameters to values

    Requesting page 3 with 10 items means page=3 and limit=10.
  3. Final Answer:

    /items?page=3&limit=10 -> Option D
  4. Quick Check:

    page=3 and limit=10 [OK]
Hint: Use page=number and limit=items per page [OK]
Common Mistakes:
  • Swapping page and limit values
  • Using wrong parameter names like size
  • Mixing up page number with limit count
3. Given the API endpoint /products?page=2&limit=5 and a total of 12 products, how many products will be returned in the response?
medium
A. 2
B. 7
C. 5
D. 12

Solution

  1. Step 1: Calculate items per page

    The limit is 5, so each page should have up to 5 products.
  2. Step 2: Determine products on page 2

    Page 1 has products 1-5, page 2 has products 6-10, so page 2 returns 5 products.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    limit = 5 products per page [OK]
Hint: Page 2 with limit 5 returns 5 items if available [OK]
Common Mistakes:
  • Counting all 12 products instead of page limit
  • Assuming leftover products on page 2
  • Confusing page number with total items
4. You have this code snippet for pagination parameters:
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('limit', 10))
start = (page - 1) * limit
end = page * limit
items = data[start:end]

What is the error if page is 0?
medium
A. It causes negative start index, returning wrong items
B. It returns the last page instead of first
C. It raises a syntax error
D. It ignores the limit parameter

Solution

  1. Step 1: Calculate start index with page=0

    start = (0 - 1) * limit = -1 * limit = negative number.
  2. Step 2: Understand slicing with negative start

    Negative start index in slicing returns items from the end, causing wrong data to be returned.
  3. Final Answer:

    It causes negative start index, returning wrong items -> Option A
  4. Quick Check:

    page=0 causes negative start index [OK]
Hint: Page must be >= 1 to avoid negative start index [OK]
Common Mistakes:
  • Assuming page=0 is valid
  • Expecting syntax error instead of logic error
  • Ignoring negative slicing effects
5. You want to implement page-based pagination for an API returning 23 items with a limit of 7 per page. How many pages will the client need to request to get all items?
hard
A. 3
B. 4
C. 5
D. 7

Solution

  1. Step 1: Calculate full pages

    Each page holds 7 items, so 3 full pages hold 21 items (3 * 7 = 21).
  2. Step 2: Calculate remaining items

    23 total items - 21 = 2 items remain, needing one more page.
  3. Step 3: Total pages needed

    3 full pages + 1 partial page = 4 pages total.
  4. Final Answer:

    4 -> Option B
  5. Quick Check:

    23 items / 7 per page = 4 pages [OK]
Hint: Divide total items by limit, round up for pages [OK]
Common Mistakes:
  • Ignoring leftover items needing extra page
  • Rounding down instead of up
  • Assuming pages equal limit count