Page-based pagination in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using page-based pagination in APIs, it's important to know how the time to get data changes as the page number or page size grows.
We want to understand how the work done by the server grows when fetching different pages.
Analyze the time complexity of the following code snippet.
GET /items?page=3&size=10
// Server side:
// 1. Calculate offset = (page - 1) * size
// 2. Query database: SELECT * FROM items LIMIT size OFFSET offset
// 3. Return the selected items
This code fetches a specific page of items by skipping some items and returning a fixed number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Retrieving a fixed number of items (page size) from the database.
- How many times: The database scans or skips items up to the offset, then reads the page size items.
As the page number grows, the server must skip more items before returning the page.
| Input Size (page number) | Approx. Operations |
|---|---|
| 10 | Skip 90 items, read 10 items |
| 100 | Skip 990 items, read 10 items |
| 1000 | Skip 9990 items, read 10 items |
Pattern observation: The work grows roughly in proportion to the page number times the page size, because the server skips items before reading.
Time Complexity: O(n)
This means the time to get a page grows linearly with how far into the list the page is.
[X] Wrong: "Fetching any page always takes the same time because the page size is fixed."
[OK] Correct: The server must skip all items before the page, so higher page numbers take more time.
Understanding how pagination affects performance helps you design APIs that stay fast even with lots of data. This skill shows you can think about real user experience and server work.
"What if we changed from page-based pagination to cursor-based pagination? How would the time complexity change?"
Practice
page-based pagination in REST APIs?Solution
Step 1: Understand pagination concept
Pagination divides data into smaller parts called pages to avoid sending everything at once.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.Final Answer:
To split large data into smaller pages for easier loading -> Option AQuick Check:
Pagination = split data into pages [OK]
- Thinking pagination sorts data
- Confusing pagination with encryption
- Assuming pagination combines all data
Solution
Step 1: Identify standard query parameters
Page-based pagination commonly usespagefor page number andlimitfor items per page.Step 2: Match parameters to values
Requesting page 3 with 10 items meanspage=3andlimit=10.Final Answer:
/items?page=3&limit=10 -> Option DQuick Check:
page=3 and limit=10 [OK]
- Swapping page and limit values
- Using wrong parameter names like size
- Mixing up page number with limit count
/products?page=2&limit=5 and a total of 12 products, how many products will be returned in the response?Solution
Step 1: Calculate items per page
The limit is 5, so each page should have up to 5 products.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.Final Answer:
5 -> Option CQuick Check:
limit = 5 products per page [OK]
- Counting all 12 products instead of page limit
- Assuming leftover products on page 2
- Confusing page number with total items
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?Solution
Step 1: Calculate start index with page=0
start = (0 - 1) * limit = -1 * limit = negative number.Step 2: Understand slicing with negative start
Negative start index in slicing returns items from the end, causing wrong data to be returned.Final Answer:
It causes negative start index, returning wrong items -> Option AQuick Check:
page=0 causes negative start index [OK]
- Assuming page=0 is valid
- Expecting syntax error instead of logic error
- Ignoring negative slicing effects
Solution
Step 1: Calculate full pages
Each page holds 7 items, so 3 full pages hold 21 items (3 * 7 = 21).Step 2: Calculate remaining items
23 total items - 21 = 2 items remain, needing one more page.Step 3: Total pages needed
3 full pages + 1 partial page = 4 pages total.Final Answer:
4 -> Option BQuick Check:
23 items / 7 per page = 4 pages [OK]
- Ignoring leftover items needing extra page
- Rounding down instead of up
- Assuming pages equal limit count
