AlgoMaster Logo
AlgoMasterUpdate a Vector Clock on Receivemedium

Update a Vector Clock on Receive

medium

When a process receives a message, its vector clock must absorb every event known to the sender and then record the receive as a new local event.

Design a VectorClockReceiver class:

  • VectorClockReceiver() creates a stateless updater.
  • int[] update(int[] local, int[] received, int index) returns the receiver's new clock.

The two clocks have the same length. Build the result in two steps:

  1. Set each component to max(local[i], received[i]).
  2. Increment the component at index by 1.

Do not modify either input clock.

Example 1:

Input:

Output:

Explanation: Merging produces [1,1,0]. The receiver is process 0, so its component becomes 2.

Example 2:

Input:

Output:

Explanation: The component-wise maximum is [2,3,1]. Incrementing component 2 records the receive event.

Constraints

  • 1 <= local.length == received.length <= 10^5
  • 0 <= local[i], received[i] <= 10^9
  • 0 <= index < local.length
  • The incremented component fits in a signed 32-bit integer.
  • At most 100 calls are made to update.
Hints

Loading...
CallReturns
new VectorClockReceiver()null
update([1,0,0], [0,1,0], 0)[2,1,0]

The component-wise maximum is [1,1,0]. Incrementing receiver component 0 produces [2,1,0].

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