Lamport Clock Ticks

easy · distributed-systems, clocks, ordering

Lamport Clock Ticks

You are given a sequence of events at a single node. Each event is one of:

  • local: a local computation
  • send: a message is sent
  • recv: a message is received (with the sender's Lamport timestamp)

The node maintains a Lamport clock that starts at 0.

Rules:

  • local / send: clock = clock + 1
  • recv: clock = max(clock, msgTimestamp) + 1

Return the Lamport timestamp after each event.

Types

type Event struct {
    Kind         string
    MsgTimestamp int
}

Function signature

func LamportTimestamps(events []Event) []int

Example

Input events:

[
  {Kind:"local"},
  {Kind:"send"},
  {Kind:"recv", MsgTimestamp:5},
  {Kind:"local"}
]

Output:

[1, 2, 6, 7]

Constraints

  • 0 <= len(events) <= 100000
  • For recv events, MsgTimestamp >= 0
  • Kind is always one of local, send, recv

Notes

  • This problem models a single node updating its local clock.
Run tests to see results
No issues detected