KatInterview

Rolling Forklift Ping Counts with Late Arrivals and Shift Tracking

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designsliding windowheaphash map

Last updated

Simulate a fleet telemetry service that tracks per-forklift pings in a global 15-minute window, handles late pings, and counts clocked-in forklifts.

You are given a sequence of operations on a forklift telemetry system. Process them in order and return one result per operation.

Each forklift is identified by the pair (depot, forklift), where depot is a string and forklift is an integer that is unique only within its depot. The operations are:

  • "ping" with arguments [depot, forklift, ts]: records a ping from that forklift at minute ts (an integer). Returns None.
  • "count_recent" with arguments [depot, forklift]: returns how many pings from that forklift are currently inside the window. Returns an integer.
  • "clock_in" with arguments [depot, forklift]: marks that forklift as on shift. Clocking in an already-active forklift changes nothing. Returns None.
  • "clock_out" with arguments [depot, forklift]: removes that forklift from the on-shift set. Clocking out a forklift that is not on shift does nothing. Returns None.
  • "active_shift_count" with arguments []: returns how many distinct forklifts are currently on shift. Returns an integer.

The window. Let T be the largest ts seen so far in any ping operation, across all depots and forklifts. Before any ping has been processed, T is undefined and every count_recent returns 0. Once T exists, the window is the closed interval [T - 14, T]. A ping counts toward the window if and only if its ts lies in that interval. For example, with T = 40 the window is [26, 40], so a ping at minute 26 counts and a ping at minute 25 does not.

Key rules:

  • T is global. A ping from one forklift can advance the window and cause other forklifts' old pings to fall out of it.
  • Pings may arrive out of order. A ping whose ts is earlier than T is still recorded if ts is inside the current window. A ping whose ts is already before T - 14 is outside the window and contributes nothing to any count.
  • Duplicate timestamps are allowed. Each ping counts separately, even if it has the same forklift and the same minute as another ping.
  • Shift state does not depend on the window or on pings.

Output: return a list with one entry per operation, in the same order. Use None for ping, clock_in, and clock_out. Use an integer for count_recent and active_shift_count.

Constraints

  • 1 <= len(operations) == len(arguments) <= 2 * 10^5
  • Each operation is one of "ping", "count_recent", "clock_in", "clock_out", "active_shift_count".
  • depot is a non-empty string; forklift is an integer.
  • ts is an integer.
  • Argument lists match the operation as described above.
  • Memory use should depend on the pings currently inside the window, not on the full history of the stream.

Examples

Example 1

Input: telemetry_session(['ping', 'ping', 'ping', 'ping', 'count_recent', 'count_recent'], [['D1', 1, 1], ['D1', 1, 3], ['D1', 2, 5], ['D1', 1, 20], ['D1', 1], ['D1', 2]])

Output: [None, None, None, None, 1, 0]

Example 2

Input: telemetry_session(['clock_in', 'clock_in', 'active_shift_count', 'clock_out', 'active_shift_count'], [['D1', 1], ['D1', 1], [], ['D1', 1], []])

Output: [None, None, 1, None, 0]

Hints

Hint 1

Track the global maximum `T` as pings arrive. A ping older than `T - 14` can be discarded immediately, since it will never fall inside the window again.

Hint 2

Store the live pings in a min-heap ordered by timestamp, alongside a per-forklift count. When `T` advances, pop every heap entry with `ts < T - 14` and decrement that forklift's count, removing the forklift when its count reaches zero.

Hint 3

Shift tracking is independent of the window: use a plain set for clock-in and clock-out, and return its size for `active_shift_count`.