AlgoMaster Logo
AlgoMasterFind WAL Transactions to Undomedium

Find WAL Transactions to Undo

medium

A write-ahead log records database activity before modified data pages are persisted. After a crash, recovery distinguishes winners, which committed, from losers, whose partial effects must be undone.

Design a WALRecoveryPlanner class:

  • WALRecoveryPlanner() creates a stateless planner.
  • int[] transactionsToUndo(int[][] records) returns the loser transaction IDs in ascending order.

Each record is [transactionId, type]:

  • type = 0: begin
  • type = 1: update
  • type = 2: commit

A transaction is a winner if any record for it has type 2. Every transaction that appears but has no commit record is a loser.

Example 1:

Input:

Output:

Explanation: Transaction 1 commits and is a winner. Transaction 2 is still in flight at the crash and must be undone.

Example 2:

Input:

Output:

Explanation: No transaction has a commit record, so every transaction is a loser. The returned IDs are sorted.

Constraints

  • 0 <= records.length <= 10^5
  • records[i].length == 2
  • 1 <= records[i][0] <= 10^9
  • 0 <= records[i][1] <= 2
  • At most 100 calls are made to transactionsToUndo.
Hints

Loading...
CallReturns
new WALRecoveryPlanner()null
transactionsToUndo([[1,0],[1,1],[2,0],[1,2],[2,1]])[2]

Transaction 1 has a commit record, while transaction 2 reaches the crash without one and must be undone.

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