AlgoMaster Logo
AlgoMasterDetect Missed Heartbeatseasy

Detect Missed Heartbeats

easy

A heartbeat detector records the last time each node reported that it was alive. At time now, a node is considered failed when its heartbeat age exceeds the configured timeout.

Design a HeartbeatFailureDetector class:

  • HeartbeatFailureDetector() creates a stateless detector.
  • int[] failedNodes(int[] lastSeen, int now, int timeout) returns the indices of failed nodes in ascending order.

Node i is failed when:

The comparison is strict. A heartbeat exactly timeout time units old is still valid. Treat each call independently and do not mutate lastSeen.

Example 1:

Input:

Output:

Explanation: Nodes 2 and 3 have been silent for 20 and 30 time units, both greater than 15.

Example 2:

Input:

Output:

Explanation: Node 3 has age 5, equal to the timeout, so it is not returned.

Constraints

  • 1 <= lastSeen.length <= 10^5
  • 0 <= lastSeen[i] <= now <= 10^9
  • 0 <= timeout <= 10^9
  • Return zero-based node indices in ascending order.
  • At most 100 calls are made to failedNodes.
Hints

Loading...
CallReturns
new HeartbeatFailureDetector()null
failedNodes([100,90,80,70], 100, 15)[2,3]

The elapsed times are 0, 10, 20, and 30. Only nodes 2 and 3 exceed the timeout of 15.

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