AlgoMaster Logo
AlgoMasterAnalyze Transaction Serializabilityhard

Analyze Transaction Serializability

hard

A transaction schedule interleaves operations from several transactions. It is conflict-serializable when those operations have the same effect as some serial execution of the transactions.

Design a TransactionScheduleAnalyzer class:

  • TransactionScheduleAnalyzer() creates a stateless analyzer.
  • boolean isSerializable(int[] transactionIds, String[] operations, String[] items) reports whether the schedule is conflict-serializable.
  • int[] serialOrder(...) returns the lexicographically smallest valid serial transaction order, or an empty array when the schedule is not serializable.

The three arrays are aligned. Entry i is one operation by transactionIds[i], where operations[i] is "R" or "W" and items[i] is the accessed data item.

Two operations conflict when they:

  1. Belong to different transactions.
  2. Access the same item.
  3. Include at least one write.

For every conflicting pair, add a precedence edge from the earlier transaction to the later transaction. The schedule is serializable exactly when this graph is acyclic.

Example 1:

Input:

Output:

Explanation: Transaction 1 writes x before transaction 2 reads it, so the graph contains only 1 -> 2.

Example 2:

Input:

Output:

Explanation: The conflict on x requires 1 before 2, while the conflict on y requires 2 before 1. No serial order can satisfy both.

Constraints

  • 1 <= transactionIds.length == operations.length == items.length <= 500
  • 1 <= transactionIds[i] <= 10^9
  • operations[i] is "R" or "W".
  • Item names are non-empty lowercase strings.
  • Every transaction ID appears in at least one operation.
  • At most 100 total method calls are made.
Hints

Loading...
CallReturns
new TransactionScheduleAnalyzer()null
isSerializable([1,2], ["W","R"], ["x","x"])true
serialOrder([1,2], ["W","R"], ["x","x"])[1,2]

The write by transaction 1 precedes transaction 2's read of x, creating the single edge 1 to 2.

Run checks these cases. Submit also runs a larger hidden set.