Pagination links in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with pagination links in a REST API, it's important to understand how the time to generate these links changes as the number of pages grows.
We want to know how the work needed to create pagination links grows when there are more pages to show.
Analyze the time complexity of the following code snippet.
// Assume totalPages is the total number of pages
// currentPage is the page currently viewed
function generatePaginationLinks(totalPages, currentPage) {
let links = [];
for (let i = 1; i <= totalPages; i++) {
links.push({ page: i, active: i === currentPage });
}
return links;
}
This code creates a list of page links from 1 to totalPages, marking the current page as active.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single loop that runs from 1 to totalPages.
- How many times: The loop runs once for each page, so totalPages times.
As the number of pages increases, the number of operations grows in a straight line.
| Input Size (totalPages) | Approx. Operations (loop runs) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of pages. Double the pages, double the work.
Time Complexity: O(n)
This means the time to generate pagination links grows linearly with the number of pages.
[X] Wrong: "Generating pagination links is always a constant time operation because we only show a few links."
[OK] Correct: If the code creates links for every page, the time grows with the total number of pages, not just a few.
Understanding how pagination link generation scales helps you design APIs that stay fast even with many pages, a useful skill in real projects.
"What if we only generated links for a fixed number of pages around the current page? How would the time complexity change?"
Practice
Solution
Step 1: Understand pagination concept
Pagination divides large data sets into smaller, manageable pages.Step 2: Identify purpose of pagination links
Pagination links help clients navigate between these pages easily.Final Answer:
To split large data into smaller pages for easier access -> Option AQuick Check:
Pagination = split data into pages [OK]
- Confusing pagination with data encryption
- Thinking pagination speeds up server response
- Mixing pagination with authentication
Solution
Step 1: Review correct Link header format
The URL must be enclosed in angle brackets <> and rel value in quotes.Step 2: Match syntax with options
Link: ; rel="next" correctly uses <URL> and rel="next" with quotes.Final Answer:
Link: <https://api.example.com/items?page=2>; rel="next" -> Option AQuick Check:
Link header syntax = <URL>; rel="value" [OK]
- Omitting angle brackets around URL
- Not quoting the rel attribute value
- Missing semicolon between URL and rel
Link: <https://api.example.com/items?page=3>; rel="next", <https://api.example.com/items?page=1>; rel="prev"What URL should the client use to get the previous page?
Solution
Step 1: Identify rel attributes in Link header
Rel="next" points to page 3, rel="prev" points to page 1.Step 2: Find URL for previous page
The client should use the URL with rel="prev", which is page 1.Final Answer:
https://api.example.com/items?page=1 -> Option CQuick Check:
Prev page URL = page=1 [OK]
- Choosing the next page URL instead of previous
- Confusing page numbers in URLs
- Ignoring rel attribute values
Link: https://api.example.com/items?page=2; rel="next"Why might this cause an error when parsing pagination links?
Solution
Step 1: Check Link header syntax rules
URLs must be enclosed in angle brackets <> for correct parsing.Step 2: Identify error in given header
The URL is not inside <>, which can cause parsing errors.Final Answer:
The URL is not enclosed in angle brackets -> Option DQuick Check:
URL must be in <> for Link header [OK]
- Forgetting angle brackets around URLs
- Assuming quotes around rel are optional
- Misplacing semicolons in header
Solution
Step 1: Calculate next and previous pages for page 5
Next page after 5 is 6, previous page before 5 is 4.Step 2: Match correct URLs with rel attributes
Link: ; rel="next", ; rel="prev" correctly assigns page=6 to rel="next" and page=4 to rel="prev".Final Answer:
Link: <https://api.example.com/items?page=6>; rel="next", <https://api.example.com/items?page=4>; rel="prev" -> Option BQuick Check:
Page 5 next=6, prev=4 [OK]
- Swapping next and prev URLs
- Using current page number for both next and prev
- Incorrect page numbers outside valid range
