Name the Anomaly in an Interleaved Two-Transaction Schedule
SQL & DataAuto-graded · 17 test casesLast updated
Identify which classic concurrency anomaly a fixed read/read/write/write schedule on one shared value produces, and return the matching option letter.
Interleaved Updates on a Shared Value
Two database transactions, T1 and T2, run at the same time against a single stored value A. Their steps happen in this exact order:
T1readsAand sees100.T2readsAand sees100.T1computesA + 10and writes110.T2computesA - 10from the value it read earlier (100) and writes90.
Decide which of the following best describes the concurrency problem in this schedule:
- A. A transaction gets different results when it reads the same item twice (a non-repeatable read).
- B. The schedule has no concurrency problem.
- C. A transaction reads data written by another transaction that has not yet committed (a dirty read).
- D. One transaction's update is overwritten and therefore lost (a lost update).
Task
Implement a function that takes no arguments and returns the letter of the correct option as an uppercase one-character string ("A", "B", "C", or "D").
Constraints
- The function takes no parameters.
- The return value is a
strcontaining exactly one of"A","B","C","D". - The schedule is the fixed four-step one above. Each transaction reads
Aexactly once. - The option letters keep the meanings listed above.
Examples
Example 1
Input: identify_schedule_anomaly()
Output: 'D'
Hints
Hint 1
Write the schedule as a sequence like R1(A), R2(A), W1(A), W2(A) and ask which transaction's result is still visible at the end.
Hint 2
Compare the final value with the results of the two serial orders (T1 then T2, and T2 then T1). Do they match?
Hint 3
Check each wrong option against its definition. Does any transaction read the same item twice, or read something an uncommitted writer produced?
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Walk through the schedule and check which transaction's effect survives, then compare against the definition of each listed anomaly.
| Step | T1 | T2 | Stored A |
|---|---|---|---|
| 1 | reads 100 | 100 | |
| 2 | reads 100 | 100 | |
| 3 | writes 110 | 110 | |
| 4 | writes 90 (from stale 100) | 90 |
The two serial orders give:
- T1 then T2: 100 -> 110 -> 100
- T2 then T1: 100 -> 90 -> 100
Both end at 100, but the interleaved schedule ends at 90. T2 computed its write from a value read before T1's write, so its write replaces T1's change as if T1 had never run. This is a lost update, so the answer is D.
Why the other options are wrong
- A (non-repeatable read): needs one transaction to read the same item twice and get different values. Each transaction here reads
Aonly once. - B (no problem): the final value
90matches neither serial outcome, so the schedule is not serializable. - C (dirty read): needs a read of another transaction's uncommitted write. Both reads occur before any write and see the original value
100.
Why it is correct
The pattern read, read, write, write on the same item, where the second write is based on a stale read, is the textbook definition of a lost update. The definitions of the other anomalies require conditions that never occur in this schedule.
Complexity
The answer is a constant classification: time O(1), space O(1).
Reference solution
def identify_schedule_anomaly() -> str:
# Schedule: R1(A)=100, R2(A)=100, W1(A)=110, W2(A)=90.
# T2 writes a value derived from a stale read, wiping out T1's +10.
# Serial executions both end at 100; this interleaving ends at 90,
# so the update from T1 is lost.
# Not a non-repeatable read: no transaction reads A twice.
# Not a dirty read: both reads happen before any write.
return "D"