AlgoMaster Logo
AlgoMasterAdvance a Hybrid Logical Clockhard

Advance a Hybrid Logical Clock

hard

A hybrid logical clock combines a wall-clock component with a logical counter. The pair preserves causality even when the local physical clock stalls or moves backward.

Design a HybridLogicalClock class:

  • HybridLogicalClock() starts at timestamp [0, 0].
  • int[] local(int physicalTime) records a local event and returns the updated timestamp.
  • int[] receive(int physicalTime, int remoteWall, int remoteLogical) records receipt of a remote timestamp and returns the updated timestamp.

For a local event:

  • If physicalTime is greater than the stored wall component, set the wall component to physicalTime and reset the logical component to 0.
  • Otherwise, keep the stored wall component and increment the logical component.

For a received event, let nextWall be the maximum of physicalTime, the stored wall component, and remoteWall. Compute the new logical component as follows:

  • If both the stored wall and remoteWall equal nextWall, use max(storedLogical, remoteLogical) + 1.
  • Otherwise, if only the stored wall equals nextWall, use storedLogical + 1.
  • Otherwise, if only remoteWall equals nextWall, use remoteLogical + 1.
  • Otherwise the physical clock is strictly greatest, so use 0.

Store and return [nextWall, nextLogical].

Example 1:

Input:

Output:

Explanation: The first event follows physical time. The second event uses the logical counter because physical time is unchanged. The received timestamp then advances beyond logical values 1 and 4.

Example 2:

Input:

Output:

Explanation: Physical time 50 is strictly greater than both wall components, so the logical component resets to 0.

Constraints

  • 0 <= physicalTime, remoteWall <= 10^9
  • 0 <= remoteLogical <= 10^9
  • Physical times supplied to one clock may stay unchanged or move backward.
  • Every returned component fits in a signed 32-bit integer.
  • At most 10^5 method calls are made per clock.
Hints

Loading...
CallReturns
new HybridLogicalClock()null
local(100)[100,0]
local(100)[100,1]
receive(100, 100, 4)[100,5]

The first event follows physical time, the second breaks a tie logically, and the received timestamp advances beyond both logical components.

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