Implement a multi-button click detector

Quick Overview

This question evaluates competency in real-time event stream processing, per-button state management, and timing-based classification of input events such as distinguishing single versus long presses.

Implement a multi-button click detector

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Implement a click detector for multiple physical buttons. You receive button state updates as a stream. Each update provides: - `button_id` (integer) - `state` where `1 = pressed`, `0 = released` - (initially) updates arrive every **10 ms** per button (you may assume updates are ordered in time). A “click” is a press followed by a release. Classify each completed click as: - **Single Click**: press duration **≤ 200 ms** - **Long Click**: press duration **≥ 500 ms** For each detected click, output something like: - `Button 1: Single Click` - `Button 2: Long Click` Requirements / edge cases: - Support **multiple buttons** concurrently. - Handle **back-to-back clicks** on the same button (press/release cycles repeated over time). - Handle **no click** (e.g., no transitions) without producing output. - Clarify/handle what to do for press durations in the **(200 ms, 500 ms)** gap. Follow-up: If state updates do **not** arrive every 10 ms (variable intervals), how would you modify the implementation? Assume you can obtain timestamps (e.g., via a system call like `now_ms()`) for each update.

Quick Answer: This question evaluates competency in real-time event stream processing, per-button state management, and timing-based classification of input events such as distinguishing single versus long presses.

|Home/Coding & Algorithms
Feb 3, 2026, 12:00 AM
hardSoftware EngineerTechnical ScreenCoding & Algorithms
6
0

Implement a click detector for multiple physical buttons.

You receive button state updates as a stream. Each update provides:

  • button_id (integer)
  • state where 1 = pressed , 0 = released
  • (initially) updates arrive every 10 ms per button (you may assume updates are ordered in time).

A “click” is a press followed by a release. Classify each completed click as:

  • Single Click : press duration ≤ 200 ms
  • Long Click : press duration ≥ 500 ms

For each detected click, output something like:

  • Button 1: Single Click
  • Button 2: Long Click

Requirements / edge cases:

  • Support multiple buttons concurrently.
  • Handle back-to-back clicks on the same button (press/release cycles repeated over time).
  • Handle no click (e.g., no transitions) without producing output.
  • Clarify/handle what to do for press durations in the (200 ms, 500 ms) gap.

Follow-up: If state updates do not arrive every 10 ms (variable intervals), how would you modify the implementation? Assume you can obtain timestamps (e.g., via a system call like now_ms()) for each update.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...