KatInterview

Timestamped Press-and-Release Event Classifier

Data Structure DesignAuto-graded · 23 test cases
HardAuto-gradedData Structure Designhash-mapevent-streamstate-machine

Last updated

Tests per-button state tracking and timing logic over an ordered event stream to classify completed press-release pairs by hold duration.

Problem

You are given a stream of hardware button events as a list of updates. Each update is a triple:

[button_id, state, timestamp_ms]
  • button_id — an integer identifying which button the event belongs to.
  • state — 1 means the button just transitioned to pressed; 0 means it just transitioned to released.
  • timestamp_ms — the time of the event in milliseconds. Updates are guaranteed to be in non-decreasing order of time.

Multiple buttons can be active simultaneously, and any button may go through several press-release cycles over the course of the stream.

Click classification

A click is one complete press-then-release of the same button. Once a release is observed, compute:

duration = release_timestamp - press_timestamp   (in ms)

Classify based on duration:

DurationLabel
≤ 200 msSingle Click
≥ 500 msLong Click
201 – 499 ms(no output — ambiguous)

For every classified click, append the string "Button {button_id}: {label}" to the output list, in the order the releases are processed.

Edge-case rules

  • Repeated press samples. The stream may contain consecutive state = 1 updates for the same button (the button is still held). Only the first such event starts the timer; later ones are ignored.
  • Repeated release samples. Consecutive state = 0 updates for the same button after it has already been released are silently ignored — they must not produce duplicate output.
  • Release with no prior press. If a release arrives for a button that was never seen pressing (e.g. the stream starts mid-hold), ignore it.
  • Press never released. If the stream ends while a button is still held, no click is emitted for it.
  • Back-to-back clicks. After a release closes one click, the next press on the same button starts a fresh timer.
  • Zero-duration click. A press and release at the identical timestamp has duration 0, which is ≤ 200 ms and therefore counts as a Single Click.

Constraints

  • 0 ≤ len(updates) ≤ 10^5
  • 1 ≤ button_id ≤ 10^4
  • state ∈ {0, 1}
  • 0 ≤ timestamp_ms ≤ 10^9
  • Updates are in non-decreasing order of timestamp_ms
  • At most 100 distinct buttons appear in a single input

Examples

Example 1

Input: classify_key_clicks([[1, 1, 0], [1, 0, 100]])

Output: ['Button 1: Single Click']

Example 2

Input: classify_key_clicks([[1, 1, 0], [2, 1, 20], [1, 0, 200], [2, 0, 620]])

Output: ['Button 1: Single Click', 'Button 2: Long Click']

Example 3

Input: classify_key_clicks([[3, 1, 0], [3, 0, 300], [3, 1, 1000], [3, 0, 1500]])

Output: ['Button 3: Long Click']

Example 4

Input: classify_key_clicks([[4, 1, 0], [4, 0, 350]])

Output: []

Hints

Hint 1

Track only the buttons that are currently held down. A dictionary mapping button_id to its press timestamp is all the state you need — if the button is absent, it is up; if it is present, it is down.

Hint 2

Act on transitions, not on levels. A press event matters only when the button was previously up (insert only if absent). A release event matters only when the button was previously down (pop and check).

Hint 3

Duration lives in wall-clock time, not sample count. Subtract the stored press timestamp from the release timestamp, then apply the three-way threshold: ≤ 200 → Single Click, ≥ 500 → Long Click, anything in between → emit nothing.