0
0
Node.jsframework~20 mins

Pagination patterns in Node.js - 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!
component_behavior
intermediate
2:00remaining
What is the output of this cursor-based pagination snippet?

Consider this Node.js function that fetches items using cursor-based pagination. What will be the value of nextCursor after fetching the first page?

Node.js
async function fetchPage(cursor = null) {
  const pageSize = 3;
  const allItems = ['a', 'b', 'c', 'd', 'e', 'f'];
  let startIndex = 0;
  if (cursor) {
    startIndex = allItems.indexOf(cursor) + 1;
  }
  const pageItems = allItems.slice(startIndex, startIndex + pageSize);
  const nextCursor = pageItems.length === pageSize ? pageItems[pageItems.length - 1] : null;
  return { pageItems, nextCursor };
}

(async () => {
  const result = await fetchPage();
  console.log(result.nextCursor);
})();
A"c"
B"d"
Cnull
D"f"
Attempts:
2 left
💡 Hint

Look at how nextCursor is assigned based on the length of pageItems.

📝 Syntax
intermediate
2:00remaining
Which option correctly implements offset-based pagination in Node.js?

Given a list of items, which code snippet correctly returns a page of items using offset and limit?

Node.js
const items = ['x', 'y', 'z', 'w', 'v'];
function getPage(offset, limit) {
  // return a slice of items
}
Areturn items.slice(limit, offset + limit);
Breturn items.slice(offset, limit);
Creturn items.slice(offset + limit);
Dreturn items.slice(offset, offset + limit);
Attempts:
2 left
💡 Hint

Remember that slice(start, end) uses start inclusive and end exclusive.

🔧 Debug
advanced
2:00remaining
Why does this cursor pagination code cause an infinite loop?

Examine the following code snippet for cursor pagination. Why might it cause an infinite loop when fetching pages?

Node.js
async function fetchPages() {
  let cursor = null;
  while (true) {
    const { items, nextCursor } = await fetchPage(cursor);
    if (!nextCursor) break;
    cursor = cursor;
  }
}
AThe cursor is never updated, so the loop never ends.
BThe fetchPage function returns an empty array causing no break.
CThe break condition is incorrect; it should check items length.
DThe while loop syntax is invalid causing runtime error.
Attempts:
2 left
💡 Hint

Check how the cursor variable changes inside the loop.

🧠 Conceptual
advanced
2:00remaining
What is a key advantage of cursor-based pagination over offset-based pagination?

Choose the best explanation for why cursor-based pagination is often preferred for large datasets.

ACursor-based pagination requires no state to be stored between requests.
BCursor-based pagination avoids skipping or duplicating items when data changes during pagination.
COffset-based pagination uses less memory than cursor-based pagination.
DCursor-based pagination always loads all data at once, improving speed.
Attempts:
2 left
💡 Hint

Think about what happens if data is added or removed while paginating.

state_output
expert
3:00remaining
What is the final value of page after this asynchronous offset pagination loop?

Given the following code, what will be the value of page after the loop finishes?

Node.js
const items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const pageSize = 2;
let offset = 0;
let page = [];

async function getPage(offset, limit) {
  return items.slice(offset, offset + limit);
}

(async () => {
  while (true) {
    page = await getPage(offset, pageSize);
    if (page.length === 0) break;
    offset += pageSize;
  }
  console.log(page);
})();
A["f", "g"]
B["g"]
C[]
D["e", "f"]
Attempts:
2 left
💡 Hint

Consider what happens when the last page is fetched and the loop breaks.