3. Given this Firebase Realtime Database structure:
{
"likes": {
"post1": 3,
"post2": 5
}
}
If a user likes post1 and the app uses a transaction to increment the count, what will be the new value of likes/post1?
medium
A. 3
B. Error
C. 5
D. 4
Solution
Step 1: Understand current value
The current like count for post1 is 3.
Step 2: Apply transaction increment
The transaction safely adds 1 to the current count, so 3 + 1 = 4.
Final Answer:
4 -> Option D
Quick Check:
3 + 1 = 4 [OK]
Hint: Increment count by 1 inside transaction [OK]
Common Mistakes:
Assuming value stays the same
Adding 2 instead of 1
Expecting an error without reason
4. You wrote this transaction code to update a total count:
const totalRef = firebase.database().ref('totalCount');
totalRef.transaction(current => current + 1);
But sometimes the count becomes NaN. What is the likely cause?
medium
A. The database path is incorrect
B. The initial value of totalCount is null or undefined
C. Transactions cannot increment numbers
D. Firebase does not support transactions
Solution
Step 1: Analyze transaction function
The function adds 1 to current value. If current is null or undefined, adding 1 results in NaN.
Step 2: Identify cause of NaN
If totalCount is not set initially, current is null, so current + 1 = NaN.
Final Answer:
The initial value of totalCount is null or undefined -> Option B
Quick Check:
Null + 1 = NaN [OK]
Hint: Initialize count before incrementing [OK]
Common Mistakes:
Believing transactions can't increment
Assuming path error causes NaN
Thinking Firebase lacks transaction support
5. You want to keep track of the total number of comments across all posts in Firebase. Which approach best uses data aggregation patterns to keep this count updated efficiently?
hard
A. Use Cloud Storage to save comment counts in a file
B. Query all comments each time and count them on the client
C. Store a totalComments value and update it inside a transaction whenever a comment is added or removed
D. Create a separate collection with one document per comment
Solution
Step 1: Evaluate counting methods
Querying all comments each time is slow and inefficient for large data.
Step 2: Use aggregation with transactions
Updating a totalComments value inside a transaction ensures safe, fast aggregation without repeated counting.
Step 3: Reject unrelated options
Cloud Storage is not for real-time counts; separate collection per comment does not aggregate counts.
Final Answer:
Store a totalComments value and update it inside a transaction whenever a comment is added or removed -> Option C
Quick Check:
Aggregate with transaction = Store a totalComments value and update it inside a transaction whenever a comment is added or removed [OK]
Hint: Update total count inside transaction on each change [OK]
Common Mistakes:
Counting all comments on client every time
Using storage files for counts
Not using transactions for safe updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.