0
0
Rest APIprogramming~20 mins

Pagination links in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.