0
0
Firebasecloud~20 mins

Increment operations in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firebase Increment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Incrementing a Firestore field atomically

You want to increase a numeric field likes in a Firestore document by 1 without overwriting other fields. Which code snippet correctly performs this atomic increment?

Firebase
const docRef = firestore.collection('posts').doc('post1');

// Which option correctly increments 'likes' by 1?
AdocRef.update({ likes: likes + 1 });
BdocRef.set({ likes: likes + 1 });
CdocRef.set({ likes: firebase.firestore.FieldValue.increment(1) });
DdocRef.update({ likes: firebase.firestore.FieldValue.increment(1) });
Attempts:
2 left
💡 Hint

Use Firestore's atomic increment operation to avoid race conditions.

Configuration
intermediate
2:00remaining
Configuring Firebase Realtime Database for atomic increments

In Firebase Realtime Database, which method correctly increments a numeric value at path /counters/visits atomically?

Firebase
const ref = firebase.database().ref('/counters/visits');

// Choose the correct increment method
Aref.increment(1);
Bref.set(ref.val() + 1);
Cref.transaction(current => (current || 0) + 1);
Dref.update({ visits: ref.val() + 1 });
Attempts:
2 left
💡 Hint

Use a transaction to safely update values in Realtime Database.

Architecture
advanced
2:30remaining
Designing scalable counters with Firestore increments

You expect very high traffic incrementing a counter in Firestore. Which architecture best avoids contention and throttling?

AUse a single document with <code>FieldValue.increment(1)</code> for all increments.
BUse multiple shard documents each incremented independently, then aggregate counts.
CUse Firestore batch writes to update the counter document every 100 increments.
DUse Firestore transactions to read and write the counter document on each increment.
Attempts:
2 left
💡 Hint

Think about how to reduce write contention on a single document.

security
advanced
2:30remaining
Securing increment operations in Firestore rules

Which Firestore security rule snippet correctly allows increments on a likes field but prevents arbitrary overwrites?

Firebase
match /posts/{postId} {
  allow update: if ... ;
}
Arequest.resource.data.likes == resource.data.likes + 1 && request.resource.data.diff(resource.data).affectedKeys().hasOnly(['likes']);
Brequest.resource.data.likes > resource.data.likes;
Crequest.resource.data.likes == resource.data.likes + 1;
Drequest.resource.data.keys().hasOnly(['likes']);
Attempts:
2 left
💡 Hint

Ensure only the likes field changes and it increments by exactly 1.

Best Practice
expert
3:00remaining
Choosing the best method for high-frequency increments in Firebase

You need to implement a view counter that increments thousands of times per second in Firebase. Which approach is best to ensure accuracy, scalability, and minimal cost?

AImplement sharded counters in Firestore with periodic aggregation via Cloud Functions.
BUse Realtime Database transactions on a single counter node for each view.
CStore each view as a separate document and count them with a query.
DUse Firestore <code>FieldValue.increment(1)</code> on a single document for each view.
Attempts:
2 left
💡 Hint

Consider write limits and cost efficiency for very high write rates.