AlgoMaster Logo
AlgoMasterValidate Fencing Tokensmedium

Validate Fencing Tokens

medium

A lease can expire while its holder is paused. If that old holder resumes after another client has acquired the lease, both clients may try to write. A fencing token lets the resource reject the delayed client's stale operation.

Design a FencingTokenValidator class:

  • FencingTokenValidator() creates a stateless validator.
  • boolean[] validate(int[] tokens) reports whether each write is accepted.

For each call, the resource's highest accepted token starts at 0. Process tokens in order. Accept a token only when it is strictly greater than the highest token accepted earlier, then make it the new maximum. Reject a token that is less than or equal to the current maximum without changing that maximum.

Example 1:

Input:

Output:

Explanation: Token 5 is accepted. Token 3 is stale. Token 6 becomes the new maximum, the repeated 6 is rejected, and token 7 is accepted.

Example 2:

Input:

Output:

Explanation: Acceptance is strict. Once token 2 has been accepted, later writes with the same token are stale.

Constraints

  • 1 <= tokens.length <= 10^5
  • 1 <= tokens[i] <= 2^31 - 1
  • Each call starts with a highest accepted token of 0.
  • Preserve the input order in the returned array.
  • At most 100 calls are made to validate.
Hints

Loading...
CallReturns
new FencingTokenValidator()null
validate([5,3,6,6,7])[true,false,true,false,true]

Tokens 5, 6, and 7 establish new maxima. Token 3 and the repeated 6 are stale.

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