0
0
Firebasecloud~20 mins

Transaction basics in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firebase Transaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What is the output of this Firebase transaction code?

Consider this Firebase transaction on a document with initial field count: 5. What will be the final value of count after this transaction completes successfully?

Firebase
const docRef = firestore.collection('counters').doc('counter1');

await firestore.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  const newCount = (doc.data()?.count ?? 0) + 3;
  transaction.update(docRef, { count: newCount });
});
A0
B3
C5
D8
Attempts:
2 left
💡 Hint

Think about how the transaction reads the current value and adds 3.

🧠 Conceptual
intermediate
1:30remaining
Which statement about Firebase transactions is true?

Choose the correct statement about Firebase transactions.

ATransactions can only update one document at a time.
BTransactions automatically retry if data changes during the transaction.
CTransactions do not guarantee atomicity across multiple documents.
DTransactions can only read data but cannot write updates.
Attempts:
2 left
💡 Hint

Think about how Firebase handles concurrent updates inside transactions.

Configuration
advanced
2:00remaining
What error occurs with this Firebase transaction code?

What error will this Firebase transaction code raise when executed?

Firebase
const docRef = firestore.collection('users').doc('user1');

await firestore.runTransaction(async (transaction) => {
  transaction.update(docRef, { score: 10 });
  const doc = await transaction.get(docRef);
  console.log(doc.data());
});
ATransactionError: Cannot update before reading document
BSyntaxError: Missing await before transaction.update
CNo error, transaction runs successfully
DTypeError: transaction.update is not a function
Attempts:
2 left
💡 Hint

Check the order of operations allowed in Firebase transactions.

security
advanced
1:30remaining
Which Firebase security rule prevents unauthorized transaction writes?

Given a Firestore collection orders, which security rule correctly prevents users from updating orders they do not own, even inside transactions?

Aallow update: if request.auth.uid == resource.data.ownerId;
Ballow update: if request.auth.uid != resource.data.ownerId;
Callow update: if request.auth.token.admin == true;
Dallow update: if request.time < timestamp.date(2025, 1, 1);
Attempts:
2 left
💡 Hint

Think about how to restrict updates to only the owner of the document.

Architecture
expert
2:30remaining
What is the best explanation for why Firebase transactions retry automatically?

Firebase transactions retry automatically when data changes during the transaction. Why is this behavior important in distributed systems?

ATo ensure eventual consistency by resolving conflicts caused by concurrent updates.
BTo reduce network traffic by batching multiple writes into one request.
CTo prevent any writes from happening if the user is offline.
DTo guarantee that transactions always complete within a fixed time limit.
Attempts:
2 left
💡 Hint

Consider how multiple users might update the same data at the same time.