Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Batch operations in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Batch Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ethereum batch transaction simulation?

Consider a batch operation that sends 3 transactions in sequence. Each transaction adds 10 to a shared counter starting at 0. What is the final counter value after all transactions succeed?

Blockchain / Solidity
let counter = 0;
const batch = [
  () => { counter += 10; return counter; },
  () => { counter += 10; return counter; },
  () => { counter += 10; return counter; }
];
for (const tx of batch) {
  tx();
}
console.log(counter);
AError: counter is undefined
B30
C10
D0
Attempts:
2 left
💡 Hint

Each transaction adds 10 to the counter. Think about how many times this happens.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes batch operations in blockchain?

Choose the correct description of batch operations in blockchain systems.

ABatch operations execute multiple transactions atomically in one block to save gas and ensure all succeed or fail together.
BBatch operations split a single transaction into multiple smaller transactions to increase fees.
CBatch operations delay transactions indefinitely until network congestion clears.
DBatch operations are only used to mine new blocks faster.
Attempts:
2 left
💡 Hint

Think about why batching transactions is useful for cost and consistency.

🔧 Debug
advanced
2:30remaining
Why does this batch transaction fail to update all balances?

Given the following Solidity batch transfer function, why does only the first transfer succeed?

Blockchain / Solidity
function batchTransfer(address[] memory recipients, uint256[] memory amounts) public {
  require(recipients.length == amounts.length, "Mismatched arrays");
  for (uint i = 0; i < recipients.length; i++) {
    transfer(recipients[i], amounts[i]);
    revert("Batch failed");
  }
}
AThe loop condition is incorrect and never runs.
BThe require statement prevents any transfers from happening.
CThe function lacks a payable modifier, so transfers fail.
DThe revert inside the loop causes the entire batch to fail after the first transfer.
Attempts:
2 left
💡 Hint

Consider what happens when revert is called inside a loop.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a batch operation function in Solidity?

Choose the correct Solidity function syntax for a batch token transfer.

A
function batchTransfer(address[] recipients, uint256[] amounts) public {
  for (uint i = 0; i &lt; recipients.length; i++) {
    transfer(recipients[i], amounts[i]);
  }
}
B
function batchTransfer(address recipients[], uint256 amounts[]) public {
  for (uint i = 0; i &lt; recipients.length; i++) {
    transfer(recipients[i], amounts[i]);
  }
}
C
function batchTransfer(address[] memory recipients, uint256[] memory amounts) public {
  for (uint i = 0; i &lt; recipients.length; i++) {
    transfer(recipients[i], amounts[i]);
  }
}
D
function batchTransfer(address[] memory recipients, uint256[] amounts) public {
  for (uint i = 0; i &lt; recipients.length; i++) {
    transfer(recipients[i], amounts[i]);
  }
}
Attempts:
2 left
💡 Hint

Remember how to declare array parameters in Solidity functions.

🚀 Application
expert
3:00remaining
How many successful transfers occur in this batch with partial failure handling?

Consider a batch transfer function that attempts to send tokens to multiple recipients. If a transfer fails, it skips that recipient and continues. Given the code below, how many transfers succeed?

Blockchain / Solidity
uint successCount = 0;
for (uint i = 0; i < recipients.length; i++) {
  try this.transfer(recipients[i], amounts[i]) {
    successCount++;
  } catch {
    continue;
  }
}
return successCount;
A2
B3
C4
D0
Attempts:
2 left
💡 Hint

Assume the second transfer fails, others succeed.

Practice

(1/5)
1. What is the main benefit of using batch operations in blockchain?
easy
A. They allow only one task to run at a time for better security.
B. They increase the number of transactions to speed up the network.
C. They combine multiple tasks into one transaction to save time and fees.
D. They automatically fix errors in blockchain code.

Solution

  1. Step 1: Understand batch operations purpose

    Batch operations group many tasks into a single transaction.
  2. Step 2: Identify benefits

    This grouping saves time and reduces transaction fees by doing many tasks at once.
  3. Final Answer:

    They combine multiple tasks into one transaction to save time and fees. -> Option C
  4. Quick Check:

    Batch operations = save time and fees [OK]
Hint: Batch means many tasks in one go to save fees [OK]
Common Mistakes:
  • Thinking batch operations increase transactions
  • Believing batch operations run tasks one by one
  • Assuming batch operations fix code errors automatically
2. Which of the following is the correct syntax to start a batch operation in a blockchain smart contract (pseudocode)?
easy
A. batch { /* tasks */ }
B. start batch { /* tasks */ }
C. beginBatch() /* tasks */ endBatch()
D. batch.start() { /* tasks */ }

Solution

  1. Step 1: Recognize common batch syntax

    Batch operations often use a block or function named batch enclosing tasks.
  2. Step 2: Compare options

    batch { /* tasks */ } uses batch { /* tasks */ } which is a common and clean way to group tasks.
  3. Final Answer:

    batch { /* tasks */ } -> Option A
  4. Quick Check:

    Batch block syntax = batch { } [OK]
Hint: Batch usually wraps tasks inside curly braces [OK]
Common Mistakes:
  • Using incorrect keywords like start or beginBatch
  • Missing curly braces for grouping tasks
  • Confusing batch syntax with function calls
3. Given the following pseudocode for a batch operation:
batch {
  transfer(from: A, to: B, amount: 10)
  transfer(from: B, to: C, amount: 5)
  transfer(from: C, to: A, amount: 3)
}

What happens if the second transfer fails due to insufficient funds?
medium
A. All transfers are rolled back; none are applied.
B. Only the second transfer fails; the others succeed.
C. The batch skips the failed transfer and continues.
D. The batch completes but logs an error for the second transfer.

Solution

  1. Step 1: Understand atomicity of batch operations

    Batch operations run all tasks together or none at all to keep data consistent.
  2. Step 2: Apply failure effect

    If one task fails (second transfer), the entire batch is rolled back, so no transfers happen.
  3. Final Answer:

    All transfers are rolled back; none are applied. -> Option A
  4. Quick Check:

    Batch atomicity = all or nothing [OK]
Hint: If one fails, batch rolls back all tasks [OK]
Common Mistakes:
  • Thinking partial batch tasks succeed
  • Assuming batch skips failed tasks
  • Believing batch logs errors but applies others
4. Consider this batch operation pseudocode:
batch {
  mintTokens(user: X, amount: 100)
  burnTokens(user: X, amount: 50)
  transferTokens(from: X, to: Y, amount: 60)
}

The batch fails with an error. What is the most likely cause?
medium
A. Minting tokens always fails in batch operations.
B. Trying to transfer more tokens than user X has after burning.
C. Burning tokens cannot be done inside a batch.
D. Batch operations do not support token transfers.

Solution

  1. Step 1: Calculate user X's token balance after mint and burn

    User X mints 100 tokens, then burns 50, so balance is 50 tokens.
  2. Step 2: Check transfer amount validity

    Transfer tries to send 60 tokens, which is more than 50 available, causing failure.
  3. Final Answer:

    Trying to transfer more tokens than user X has after burning. -> Option B
  4. Quick Check:

    Transfer > balance causes batch failure [OK]
Hint: Check token balances after each batch step [OK]
Common Mistakes:
  • Assuming minting always fails
  • Believing burning is not allowed in batch
  • Thinking batch disallows transfers
5. You want to update multiple user balances atomically in a blockchain. Which approach best uses batch operations to ensure either all updates succeed or none do?
function updateBalances(updates) {
  batch {
    for (update in updates) {
      setBalance(user: update.user, amount: update.amount)
    }
  }
}

What is a key consideration to avoid silent failures in this batch?
hard
A. Use multiple batches for each user update.
B. Run each update outside batch to isolate errors.
C. Ignore errors inside batch to continue all updates.
D. Validate each update's amount before batch to prevent invalid data.

Solution

  1. Step 1: Understand batch atomicity and error handling

    Batch runs all updates together; if one fails, all rollback. Silent failures can happen if invalid data is inside batch.
  2. Step 2: Importance of pre-validation

    Validating each update before batch ensures no invalid data causes failure, avoiding silent rollback.
  3. Final Answer:

    Validate each update's amount before batch to prevent invalid data. -> Option D
  4. Quick Check:

    Pre-validate data to avoid batch rollback [OK]
Hint: Check data before batch to prevent rollback [OK]
Common Mistakes:
  • Running updates outside batch loses atomicity
  • Ignoring errors causes silent rollback
  • Splitting updates into many batches loses efficiency