AlgoMaster Logo
AlgoMasterCompare Two Vector Clocksmedium

Compare Two Vector Clocks

medium

Vector clocks record one logical counter per process. Comparing two clocks reveals whether one event causally precedes the other, whether they are equal, or whether they represent concurrent events.

Design a VectorClockComparator class:

  • VectorClockComparator() creates a stateless comparator.
  • String compare(int[] a, int[] b) returns the relationship of a to b.

Return exactly one of these strings:

  • "before" when a[i] <= b[i] for every i and at least one component is smaller.
  • "after" when a[i] >= b[i] for every i and at least one component is greater.
  • "equal" when every component matches.
  • "concurrent" when neither vector dominates the other.
Example 1:

Input:

Output:

Explanation: a never exceeds b, and its second component is smaller.

Example 2:

Input:

Output:

Explanation: a is greater in component 1 while b is greater in component 0. Neither vector dominates the other.

Constraints

  • 1 <= a.length == b.length <= 10^5
  • 0 <= a[i], b[i] <= 10^9
  • Return one of the four lowercase relationship strings exactly as specified.
  • At most 100 calls are made to compare.
Hints

Loading...
CallReturns
new VectorClockComparator()null
compare([1,0,0], [1,1,0])"before"

Every component of a is at most the corresponding component of b, and component 1 is smaller.

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