0
0
Rest APIprogramming~20 mins

Keyset pagination for performance in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Keyset Pagination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of keyset pagination query

Consider a REST API that uses keyset pagination on a table sorted by created_at and id. What is the output of the following SQL snippet when last_seen_created_at = '2024-01-01 10:00:00' and last_seen_id = 100?

SELECT * FROM posts WHERE (created_at, id) < (:last_seen_created_at, :last_seen_id) ORDER BY created_at DESC, id DESC LIMIT 3;
Rest API
last_seen_created_at = '2024-01-01 10:00:00'
last_seen_id = 100

-- Assume posts table has these rows sorted by created_at DESC, id DESC:
-- (2024-01-01 10:00:00, 100), (2024-01-01 09:59:59, 105), (2024-01-01 09:59:59, 102), (2024-01-01 09:59:58, 110), (2024-01-01 09:59:57, 101)
ARows with (2024-01-01 10:00:00, 101), (2024-01-01 09:59:59, 105), (2024-01-01 09:59:58, 110)
BRows with (2024-01-01 10:00:00, 100), (2024-01-01 09:59:59, 105), (2024-01-01 09:59:59, 102)
CRows with (2024-01-01 09:59:59, 105), (2024-01-01 09:59:59, 102), (2024-01-01 09:59:58, 110)
DRows with (2024-01-01 09:59:58, 110), (2024-01-01 09:59:57, 101), (2024-01-01 09:59:59, 102)
Attempts:
2 left
💡 Hint

Remember that keyset pagination uses the tuple comparison to get rows strictly less than the last seen key.

🧠 Conceptual
intermediate
1:30remaining
Why use keyset pagination over offset pagination?

Which of the following is the main reason to prefer keyset pagination over offset pagination in REST APIs?

AKeyset pagination avoids skipping rows and is faster on large datasets.
BKeyset pagination allows jumping to any page number instantly.
CKeyset pagination requires less client-side state management.
DKeyset pagination always returns results in random order.
Attempts:
2 left
💡 Hint

Think about how offset pagination slows down as the offset grows.

🔧 Debug
advanced
2:00remaining
Identify the bug in keyset pagination query

Find the bug in this keyset pagination SQL query snippet:

SELECT * FROM messages WHERE created_at < :last_seen_created_at ORDER BY created_at DESC LIMIT 5;

Assume last_seen_created_at is the timestamp of the last message from the previous page.

AIt can skip messages with the same created_at timestamp as last_seen_created_at.
BIt will return duplicate messages from the previous page.
CIt uses ascending order instead of descending order.
DIt does not limit the number of rows returned.
Attempts:
2 left
💡 Hint

Think about what happens if multiple messages share the same timestamp.

📝 Syntax
advanced
1:30remaining
Correct keyset pagination WHERE clause syntax

Which option correctly implements a keyset pagination WHERE clause for sorting by created_at DESC and id DESC with last seen values :last_created_at and :last_id?

AWHERE created_at &gt; :last_created_at AND id &gt; :last_id
BWHERE (created_at, id) &lt; (:last_created_at, :last_id)
CWHERE created_at &gt; :last_created_at OR (created_at = :last_created_at AND id &gt; :last_id)
DWHERE created_at &lt; :last_created_at OR (created_at = :last_created_at AND id &lt; :last_id)
Attempts:
2 left
💡 Hint

Remember that for descending order, smaller tuples come after larger ones.

🚀 Application
expert
2:30remaining
Implement keyset pagination in REST API response

You have a REST API endpoint that returns a list of products sorted by updated_at DESC and product_id DESC. You want to implement keyset pagination. Which approach correctly returns the next page token and the next page of results?

AReturn the current page number and use <code>OFFSET page_number * page_size</code> in the next request to fetch the next page.
BReturn the last product's <code>updated_at</code> and <code>product_id</code> as a cursor token. Use them in the next request to filter with <code>WHERE (updated_at, product_id) &lt; (:last_updated_at, :last_product_id)</code> and order by <code>updated_at DESC, product_id DESC</code>.
CReturn the total count of products and calculate the next page by dividing count by page size.
DReturn the first product's <code>updated_at</code> and <code>product_id</code> as a cursor token and filter with <code>WHERE (updated_at, product_id) &gt; (:first_updated_at, :first_product_id)</code>.
Attempts:
2 left
💡 Hint

Keyset pagination uses the last seen key to fetch the next page efficiently.