Bird
Raised Fist0
Rest APIprogramming~20 mins

Pagination links 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
🎖️
Pagination Pro
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 pagination link generation code?

Consider a REST API that returns paginated results. The following Python code generates pagination links for a given page and total pages.

def get_pagination_links(current_page, total_pages):
    base_url = 'https://api.example.com/items?page='
    links = {}
    if current_page > 1:
        links['prev'] = base_url + str(current_page - 1)
    if current_page < total_pages:
        links['next'] = base_url + str(current_page + 1)
    links['self'] = base_url + str(current_page)
    return links

print(get_pagination_links(3, 5))

What will be printed?

Rest API
def get_pagination_links(current_page, total_pages):
    base_url = 'https://api.example.com/items?page='
    links = {}
    if current_page > 1:
        links['prev'] = base_url + str(current_page - 1)
    if current_page < total_pages:
        links['next'] = base_url + str(current_page + 1)
    links['self'] = base_url + str(current_page)
    return links

print(get_pagination_links(3, 5))
A{"prev": "https://api.example.com/items?page=2", "next": "https://api.example.com/items?page=4", "self": "https://api.example.com/items?page=3"}
B{"prev": "https://api.example.com/items?page=3", "next": "https://api.example.com/items?page=4", "self": "https://api.example.com/items?page=3"}
C{"prev": "https://api.example.com/items?page=2", "self": "https://api.example.com/items?page=3"}
D{"next": "https://api.example.com/items?page=4", "self": "https://api.example.com/items?page=3"}
Attempts:
2 left
💡 Hint

Think about which pages exist before and after the current page 3 when total pages are 5.

Predict Output
intermediate
2:00remaining
What error does this pagination link code raise?

Look at this JavaScript function that generates pagination links:

function getPaginationLinks(currentPage, totalPages) {
  const baseUrl = 'https://api.example.com/items?page=';
  const links = {};
  if (currentPage > 1) {
    links.prev = baseUrl + (currentPage - 1);
  }
  if (currentPage < totalPages) {
    links.next = baseUrl + (currentPage + 1);
  }
  links.self = baseUrl + currentPage;
  return links;
}

console.log(getPaginationLinks(0, 5));

What error or output will this code produce?

Rest API
function getPaginationLinks(currentPage, totalPages) {
  const baseUrl = 'https://api.example.com/items?page=';
  const links = {};
  if (currentPage > 1) {
    links.prev = baseUrl + (currentPage - 1);
  }
  if (currentPage < totalPages) {
    links.next = baseUrl + (currentPage + 1);
  }
  links.self = baseUrl + currentPage;
  return links;
}

console.log(getPaginationLinks(0, 5));
ATypeError: Cannot read property 'prev' of undefined
B{"prev": "https://api.example.com/items?page=-1", "next": "https://api.example.com/items?page=1", "self": "https://api.example.com/items?page=0"}
C{"next": "https://api.example.com/items?page=1", "self": "https://api.example.com/items?page=0"}
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint

Check the conditions for adding 'prev' and 'next' links when currentPage is 0.

🔧 Debug
advanced
2:00remaining
Why does this pagination link code raise a KeyError?

In Python, this code tries to access pagination links but raises a KeyError:

def get_links(page, total):
    base = 'https://api.example.com/items?page='
    links = {'self': base + str(page)}
    if page > 1:
        links['prev'] = base + str(page - 1)
    if page < total:
        links['next'] = base + str(page + 1)

print(links['prev'])

Why does this raise a KeyError?

Rest API
def get_links(page, total):
    base = 'https://api.example.com/items?page='
    links = {'self': base + str(page)}
    if page > 1:
        links['prev'] = base + str(page - 1)
    if page < total:
        links['next'] = base + str(page + 1)

print(links['prev'])
ABecause 'links' is defined inside the function but accessed outside, causing NameError
BBecause 'links' is a list, not a dictionary, so keys are invalid
CBecause 'links' dictionary is empty, so 'prev' key is missing
DBecause 'prev' key may not exist if page is 1, causing KeyError
Attempts:
2 left
💡 Hint

Check where the variable 'links' is defined and where it is accessed.

Predict Output
advanced
2:00remaining
What is the output of this SQL query for pagination links?

Given a table items with 100 rows, this SQL query tries to get pagination info for page 3 with 10 items per page:

WITH total_count AS (
  SELECT COUNT(*) AS total FROM items
),
page_info AS (
  SELECT 3 AS current_page, 10 AS per_page FROM dual
)
SELECT
  current_page,
  per_page,
  total,
  CASE WHEN current_page > 1 THEN current_page - 1 ELSE NULL END AS prev_page,
  CASE WHEN current_page * per_page < total THEN current_page + 1 ELSE NULL END AS next_page
FROM page_info, total_count;

What will be the values of prev_page and next_page in the result?

Rest API
WITH total_count AS (
  SELECT COUNT(*) AS total FROM items
),
page_info AS (
  SELECT 3 AS current_page, 10 AS per_page FROM dual
)
SELECT
  current_page,
  per_page,
  total,
  CASE WHEN current_page > 1 THEN current_page - 1 ELSE NULL END AS prev_page,
  CASE WHEN current_page * per_page < total THEN current_page + 1 ELSE NULL END AS next_page
FROM page_info, total_count;
Aprev_page = 2, next_page = NULL
Bprev_page = 2, next_page = 4
Cprev_page = 3, next_page = 4
Dprev_page = NULL, next_page = 4
Attempts:
2 left
💡 Hint

Calculate if current_page * per_page is less than total rows to determine next_page.

🚀 Application
expert
2:00remaining
How many pagination links are generated for the last page?

In a REST API, a function generates pagination links for a list of 50 items with 10 items per page. The current page is 5 (the last page). The function adds 'prev' if current page > 1, 'next' if current page < total pages, and always adds 'self'.

How many links will be in the returned dictionary for page 5?

Rest API
def pagination_links(current_page, total_items, per_page):
    total_pages = (total_items + per_page - 1) // per_page
    base = 'https://api.example.com/items?page='
    links = {}
    if current_page > 1:
        links['prev'] = base + str(current_page - 1)
    if current_page < total_pages:
        links['next'] = base + str(current_page + 1)
    links['self'] = base + str(current_page)
    return links

result = pagination_links(5, 50, 10)
print(len(result))
A4
B1
C3
D2
Attempts:
2 left
💡 Hint

Calculate total pages and check conditions for 'prev' and 'next' links on the last page.

Practice

(1/5)
1. What is the main purpose of pagination links in a REST API?
easy
A. To split large data into smaller pages for easier access
B. To encrypt the data sent from the server
C. To speed up the server response time by caching
D. To validate user authentication tokens

Solution

  1. Step 1: Understand pagination concept

    Pagination divides large data sets into smaller, manageable pages.
  2. Step 2: Identify purpose of pagination links

    Pagination links help clients navigate between these pages easily.
  3. Final Answer:

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

    Pagination = split data into pages [OK]
Hint: Pagination means breaking data into pages for easy reading [OK]
Common Mistakes:
  • Confusing pagination with data encryption
  • Thinking pagination speeds up server response
  • Mixing pagination with authentication
2. Which of the following is the correct syntax for a pagination link in an HTTP header?
easy
A. Link: ; rel="next"
B. Link: https://api.example.com/items?page=2 rel=next
C. Link: rel=next
D. Link: https://api.example.com/items?page=2; rel="next"

Solution

  1. Step 1: Review correct Link header format

    The URL must be enclosed in angle brackets <> and rel value in quotes.
  2. Step 2: Match syntax with options

    Link: ; rel="next" correctly uses <URL> and rel="next" with quotes.
  3. Final Answer:

    Link: <https://api.example.com/items?page=2>; rel="next" -> Option A
  4. Quick Check:

    Link header syntax = <URL>; rel="value" [OK]
Hint: Use angle brackets for URL and quotes for rel value [OK]
Common Mistakes:
  • Omitting angle brackets around URL
  • Not quoting the rel attribute value
  • Missing semicolon between URL and rel
3. Given the HTTP Link header:
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?
medium
A. https://api.example.com/items?page=3
B. https://api.example.com/items?page=4
C. https://api.example.com/items?page=1
D. https://api.example.com/items?page=2

Solution

  1. Step 1: Identify rel attributes in Link header

    Rel="next" points to page 3, rel="prev" points to page 1.
  2. Step 2: Find URL for previous page

    The client should use the URL with rel="prev", which is page 1.
  3. Final Answer:

    https://api.example.com/items?page=1 -> Option C
  4. Quick Check:

    Prev page URL = page=1 [OK]
Hint: Look for rel="prev" to find previous page URL [OK]
Common Mistakes:
  • Choosing the next page URL instead of previous
  • Confusing page numbers in URLs
  • Ignoring rel attribute values
4. You receive this Link header:
Link: https://api.example.com/items?page=2; rel="next"
Why might this cause an error when parsing pagination links?
medium
A. The page number is invalid
B. The rel attribute value is missing quotes
C. The semicolon is missing between URL and rel
D. The URL is not enclosed in angle brackets

Solution

  1. Step 1: Check Link header syntax rules

    URLs must be enclosed in angle brackets <> for correct parsing.
  2. Step 2: Identify error in given header

    The URL is not inside <>, which can cause parsing errors.
  3. Final Answer:

    The URL is not enclosed in angle brackets -> Option D
  4. Quick Check:

    URL must be in <> for Link header [OK]
Hint: Always put URLs in angle brackets in Link headers [OK]
Common Mistakes:
  • Forgetting angle brackets around URLs
  • Assuming quotes around rel are optional
  • Misplacing semicolons in header
5. You want to implement pagination links for an API returning 100 items with 10 items per page. Which Link header correctly provides navigation for page 5?
hard
A. Link: ; rel="next", ; rel="prev"
B. Link: ; rel="next", ; rel="prev"
C. Link: ; rel="next", ; rel="prev"
D. Link: ; rel="next", ; rel="prev"

Solution

  1. Step 1: Calculate next and previous pages for page 5

    Next page after 5 is 6, previous page before 5 is 4.
  2. 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".
  3. Final Answer:

    Link: <https://api.example.com/items?page=6>; rel="next", <https://api.example.com/items?page=4>; rel="prev" -> Option B
  4. Quick Check:

    Page 5 next=6, prev=4 [OK]
Hint: Next page = current +1, prev page = current -1 in Link header [OK]
Common Mistakes:
  • Swapping next and prev URLs
  • Using current page number for both next and prev
  • Incorrect page numbers outside valid range