Bird
0
0

You have an API that returns data with cursor-based pagination. The API returns the following responses in sequence:

hard📝 Application Q15 of 15
Rest API - Pagination Patterns

You have an API that returns data with cursor-based pagination. The API returns the following responses in sequence:

1. {"data": ["a", "b"], "next_cursor": "c1"}
2. {"data": ["c", "d"], "next_cursor": "c2"}
3. {"data": ["e"], "next_cursor": null}

You want to collect all items without duplicates even if the API sometimes returns overlapping data due to data changes. Which approach is best?

ACollect all items in a list and remove duplicates after fetching all pages
BFetch only the first page to avoid duplicates
CUse a set to store items as you fetch each page to avoid duplicates immediately
DIgnore duplicates and trust API never repeats data
Step-by-Step Solution
Solution:
  1. Step 1: Understand overlapping data issue

    Cursor-based pagination can return overlapping items if data changes during pagination, causing duplicates.
  2. Step 2: Choose a method to avoid duplicates

    Using a set to store items as they are fetched avoids duplicates immediately and efficiently.
  3. Step 3: Evaluate other options

    Removing duplicates after fetching all pages (list) is less efficient. Ignoring duplicates or fetching only first page is incorrect.
  4. Final Answer:

    Use a set to store items as you fetch each page to avoid duplicates immediately -> Option C
  5. Quick Check:

    Use set to avoid duplicates during fetch [OK]
Quick Trick: Use set to track unique items during pagination [OK]
Common Mistakes:
  • Assuming API never returns duplicates
  • Removing duplicates only after all data fetched
  • Fetching only first page to avoid duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes