Timestamped Press-and-Release Event Classifier
Data Structure DesignAuto-graded · 23 test casesLast 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—1means the button just transitioned to pressed;0means 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:
| Duration | Label |
|---|---|
| ≤ 200 ms | Single Click |
| ≥ 500 ms | Long 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 = 1updates 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 = 0updates 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^51 ≤ button_id ≤ 10^4state ∈ {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.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Model each button as a two-state machine: Released and Held. One dictionary, press_start, captures this state for every button that is currently held by mapping button_id → press timestamp. The absence of a button means it is released; the presence of a button means it is held with the stored timestamp as the start of the current press.
Walk through the updates in order and react only to transitions:
Press event (state = 1) Check whether the button already has an entry. If it does not, this is a Released → Held transition: record the timestamp and enter the Held state. If it already has an entry, the button is still held from a previous press — do nothing. This guarantees that repeated "still pressed" samples never reset the clock.
Release event (state = 0)
Pop the button's entry. dict.pop(key, None) returns None without raising an error if the key is absent, which handles stray releases and repeated release samples in one line. When a start time is found, compute duration = timestamp_ms - start and apply the threshold:
duration ≤ 200→"Button {button_id}: Single Click"duration ≥ 500→"Button {button_id}: Long Click"200 < duration < 500→ no output (ambiguous gap)
Popping the button on release resets the state back to Released, so the very next press on that button starts a brand-new hold. That is why back-to-back clicks on the same button are handled correctly without any extra logic.
Why this is correct
- Per-button independence. All state is indexed by
button_id, so interleaved events from many buttons never interfere with each other. - Edge-triggered. We act only when the state changes, not on every sample. Duplicate
1s cannot inflate the duration; duplicate0s cannot produce duplicate clicks. - Wall-clock duration. The result is the difference of two timestamps, so it remains correct regardless of how often updates arrive.
- Inclusive boundaries. Exactly 200 ms is Single Click; exactly 500 ms is Long Click; 0 ms (same timestamp for press and release) is Single Click.
- Unclosed holds. Any button still in
press_startwhen the loop ends simply has no release event, so no click is emitted — correct per spec.
Complexity
- Time: O(n) — one O(1) dictionary operation per update.
- Space: O(b) for
press_start, where b is the number of buttons held simultaneously (at most the number of distinct buttons), plus O(k) for the k output strings.
Full solution
from typing import List
def classify_key_clicks(updates: List[List[int]]) -> List[str]:
press_start = {}
result = []
for button_id, state, timestamp_ms in updates:
if state == 1:
if button_id not in press_start:
press_start[button_id] = timestamp_ms
elif state == 0:
start = press_start.pop(button_id, None)
if start is None:
continue
duration = timestamp_ms - start
if duration <= 200:
result.append(f"Button {button_id}: Single Click")
elif duration >= 500:
result.append(f"Button {button_id}: Long Click")
return result