AlgoMaster Logo
AlgoMasterDecide a Two-Phase Commit Outcomeeasy

Decide a Two-Phase Commit Outcome

easy

Two-phase commit coordinates one transaction across multiple participants. During the prepare phase, each participant reports whether it is ready to commit. The coordinator may commit only after every participant has voted "yes".

Design a TwoPhaseCommitCoordinator class:

  • TwoPhaseCommitCoordinator() creates a stateless coordinator.
  • String decide(String[] votes) returns the final decision for one prepare phase.

Each entry in votes is one of:

  • "yes": the participant prepared successfully.
  • "no": the participant cannot commit.
  • "timeout": the participant did not return a vote.

Return "commit" only when every vote is "yes". Return "abort" when any participant votes "no" or times out. A timeout is not a negative vote from the participant, but the coordinator still cannot safely commit without its explicit agreement.

Example 1:

Input:

Output:

Explanation: All three participants voted yes, so the unanimous commit rule is satisfied.

Example 2:

Input:

Output:

Explanation: The coordinator never received explicit agreement from the second participant, so committing would be unsafe.

Constraints

  • 1 <= votes.length <= 100
  • votes[i] is "yes", "no", or "timeout".
  • Return exactly "commit" or "abort" in lowercase.
  • At most 100 calls are made to decide per object.
  • Calls are independent; a previous decision must not affect a later one.
Hints

Loading...
CallReturns
new TwoPhaseCommitCoordinator()null
decide(["yes","yes","yes"])"commit"

Every participant prepared successfully, so the coordinator can commit.

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