0
0
Firebasecloud~20 mins

Query optimization in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Understanding Firestore Query Index Requirements
You have a Firestore collection with documents containing fields: age (number), city (string), and status (string). You run this query:
collection.where('age', '>', 25).where('city', '==', 'Seattle').orderBy('status')
What will happen when you run this query without any additional setup?
AThe query fails with an error asking to create a composite index.
BThe query runs successfully without any errors.
CThe query returns documents but ignores the orderBy clause.
DThe query returns documents but only filters by age.
Attempts:
2 left
💡 Hint
Firestore requires composite indexes for queries with multiple where filters and orderBy on different fields.
Architecture
intermediate
2:00remaining
Optimizing Firestore Queries for Large Collections
You have a Firestore collection with millions of documents. You want to efficiently fetch documents where status is 'active' and order them by createdAt descending. Which approach is best to optimize query performance?
AUse multiple queries for each status value and merge results in the app.
BUse a query with where('status', '==', 'active') and orderBy('createdAt', 'desc') with a composite index on status and createdAt.
CUse orderBy('createdAt', 'desc') only and filter status after fetching.
DFetch all documents and filter 'active' status in your application code.
Attempts:
2 left
💡 Hint
Filtering and ordering in the query with proper indexes is more efficient than client-side filtering.
security
advanced
2:00remaining
Securing Firestore Queries with Rules and Indexes
You want to allow users to query their own orders by userId and filter by orderStatus. You create a composite index on userId and orderStatus. Which Firestore security rule best ensures users can only query their own orders?
Aallow read: if request.auth.token.admin == true;
Ballow read: if request.auth.uid != null;
Callow read: if resource.data.orderStatus == 'completed';
Dallow read: if request.auth.uid == resource.data.userId;
Attempts:
2 left
💡 Hint
Security rules should restrict access based on user identity matching document fields.
Best Practice
advanced
2:00remaining
Reducing Firestore Query Costs with Pagination
You have a Firestore query that returns thousands of documents. To reduce cost and improve performance, what is the best practice to paginate results?
AUse offset() to skip documents and limit() to fetch pages.
BFetch all documents at once and slice the results in the client.
CUse limit() with startAfter() or startAt() cursors to fetch pages incrementally.
DRun multiple queries with different where filters to split data.
Attempts:
2 left
💡 Hint
Firestore charges for documents read, so fetching only needed documents is best.
🧠 Conceptual
expert
2:00remaining
Analyzing Firestore Query Execution Behavior
Consider a Firestore collection with documents having fields: category, price, and rating. You run this query:
collection.where('category', '==', 'books').orderBy('price').orderBy('rating')
Which statement best describes how Firestore executes this query?
AFirestore uses a composite index on category, price, and rating to efficiently filter and order results.
BFirestore orders by price only and ignores the rating orderBy clause.
CFirestore filters by category first, then orders by price and rating without needing an index.
DFirestore requires separate queries for price and rating ordering.
Attempts:
2 left
💡 Hint
Multiple orderBy clauses require a composite index covering all fields in order.