0
0
Firebasecloud~20 mins

Cursor-based pagination (startAfter, endBefore) in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Cursor Pagination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Understanding startAfter behavior in Firestore queries

You have a Firestore collection of documents ordered by a timestamp field. You want to fetch the next page of 5 documents after a specific document snapshot using startAfter. What will be the result of the query?

Firebase
const query = firestore.collection('posts').orderBy('createdAt').startAfter(lastDoc).limit(5);
const snapshot = await query.get();
const results = snapshot.docs.map(doc => doc.data());
AThe query returns 5 documents strictly after the document represented by lastDoc, excluding lastDoc itself.
BThe query returns 5 documents including the document represented by lastDoc.
CThe query returns 5 documents before the document represented by lastDoc.
DThe query returns all documents in the collection ignoring lastDoc.
Attempts:
2 left
💡 Hint

Think about how startAfter works in Firestore pagination.

service_behavior
intermediate
2:00remaining
Effect of endBefore in Firestore pagination

You want to fetch the previous page of 5 documents before a specific document snapshot using endBefore. What does the query return?

Firebase
const query = firestore.collection('posts').orderBy('createdAt').endBefore(firstDoc).limitToLast(5);
const snapshot = await query.get();
const results = snapshot.docs.map(doc => doc.data());
AThe query returns 5 documents strictly before the document represented by firstDoc, excluding firstDoc.
BThe query returns 5 documents including the document represented by firstDoc.
CThe query returns all documents in the collection ignoring firstDoc.
DThe query returns 5 documents after the document represented by firstDoc.
Attempts:
2 left
💡 Hint

Remember how endBefore works with limitToLast in Firestore.

Architecture
advanced
3:00remaining
Designing efficient cursor-based pagination for large Firestore collections

You have a Firestore collection with millions of documents. You want to implement cursor-based pagination for a mobile app to load data efficiently. Which approach is best to minimize read costs and latency?

AFetch all documents at once and paginate on the client side to avoid multiple queries.
BUse <code>orderBy</code> on an indexed field and paginate with <code>startAfter</code> using document snapshots as cursors.
CUse <code>limit</code> without any cursor and fetch the first N documents repeatedly for each page.
DUse <code>orderBy</code> on a non-indexed field and paginate with <code>startAt</code> using field values as cursors.
Attempts:
2 left
💡 Hint

Think about Firestore indexing and how cursors work for efficient queries.

security
advanced
3:00remaining
Securing Firestore cursor-based pagination queries

You want to ensure users can only paginate through documents they have access to in Firestore. Which security rule approach correctly enforces this during cursor-based pagination?

AUse client-side filtering after fetching all documents to enforce access control.
BWrite security rules that allow access to all documents in the collection during pagination to avoid errors.
CDisable security rules for pagination queries to improve performance.
DWrite security rules that check user permissions on each document individually, regardless of pagination cursors.
Attempts:
2 left
💡 Hint

Consider how Firestore security rules apply to queries and documents.

Best Practice
expert
4:00remaining
Handling inconsistent data during Firestore cursor-based pagination

While paginating through a Firestore collection using startAfter, some documents are added or deleted concurrently. What is the best practice to ensure consistent pagination results?

AUse random ordering to distribute load and avoid conflicts.
BReload the entire collection on each page request to get the latest data.
CUse a stable, immutable field like a timestamp or unique ID for ordering and cursors to minimize inconsistencies.
DIgnore concurrent changes and rely on client-side merging of results.
Attempts:
2 left
💡 Hint

Think about how ordering fields affect pagination stability.