Implement a multi-button click detector
Problem: Multi-Button Click Detector
Implement a system to detect and classify clicks from multiple physical buttons. You will receive a continuous stream of button state updates. Each update provides the button_id (an integer) and its state (where 1 indicates pressed and 0 indicates released).
Initially, you may assume that updates for each button arrive every 10 ms and are ordered chronologically.
A "click" is defined as a button press followed by a release. For each completed click, classify it based on its press duration and output the classification. The classifications are:
- Single Click: The press duration was less than or equal to 200 ms.
- Long Click: The press duration was greater than or equal to 500 ms.
Example Output:
Button 1: Single ClickButton 2: Long Click
Requirements & Edge Cases:
- Multiple Buttons: The system must support detecting clicks from multiple buttons concurrently.
- Back-to-Back Clicks: Correctly handle consecutive press/release cycles on the same button over time.
- No Click: Do not produce any output if no complete click is detected (e.g., a button is pressed but never released, or only release events are received).
- Duration Gap: Clearly define and handle the classification for press durations that fall strictly between 200 ms and 500 ms.
Follow-up Question:
If state updates do not arrive at fixed 10 ms intervals (i.e., variable intervals), how would you modify your implementation? Assume that you can obtain precise timestamps (e.g., via a system call like now_ms()) for each incoming update.
AI interview prep
Core dilemma
Managing concurrent state and precise timing for multiple independent entities (buttons) based on an asynchronous event stream, while accurately classifying events within specific time windows and handling various edge cases and potential timing inaccuracies.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a multi-button click detector
Problem: Multi-Button Click Detector
Implement a system to detect and classify clicks from multiple physical buttons. You will receive a continuous stream of button state updates. Each update provides the button_id (an integer) and its state (where 1 indicates pressed and 0 indicates released).
Initially, you may assume that updates for each button arrive every 10 ms and are ordered chronologically.
A "click" is defined as a button press followed by a release. For each completed click, classify it based on its press duration and output the classification. The classifications are:
- Single Click: The press duration was less than or equal to 200 ms.
- Long Click: The press duration was greater than or equal to 500 ms.
Example Output:
Button 1: Single ClickButton 2: Long Click
Requirements & Edge Cases:
- Multiple Buttons: The system must support detecting clicks from multiple buttons concurrently.
- Back-to-Back Clicks: Correctly handle consecutive press/release cycles on the same button over time.
- No Click: Do not produce any output if no complete click is detected (e.g., a button is pressed but never released, or only release events are received).
- Duration Gap: Clearly define and handle the classification for press durations that fall strictly between 200 ms and 500 ms.
Follow-up Question:
If state updates do not arrive at fixed 10 ms intervals (i.e., variable intervals), how would you modify your implementation? Assume that you can obtain precise timestamps (e.g., via a system call like now_ms()) for each incoming update.