Magicsheet logo

Button with Longest Push Time

Easy
70.4%
Updated 6/1/2025

Asked by 3 Companies

Topics

Button with Longest Push Time

What is this problem about?

The Button with Longest Push Time interview question asks you to process a series of timestamps from a keypad. You are given a 2D array logs where each entry is [button_index, time_released]. Each button is pushed one after another. The "push time" for a button is the difference between its release time and the release time of the previous button. For the first button, the push time is simply its release time. You need to find the button with the maximum push time. This Button with Longest Push Time coding problem is a fundamental array iteration task.

Why is this asked in interviews?

Companies like J.P. Morgan and Amazon use this as an entry-level coding task. it tests basic loop logic, state tracking (storing the previous release time), and tie-breaking conditions (usually returning the smallest index if multiple buttons have the same longest time). It ensures a candidate has the basic syntax and logic skills required for backend development.

Algorithmic pattern used

This utilizes the Array interview pattern. It's a simple linear scan where you maintain:

  1. max_duration: The longest push time found so far.
  2. best_index: The index of the button associated with that duration.
  3. prev_time: The release time of the last processed button.

Example explanation

Logs: [[1, 5], [2, 12], [1, 15]]

  1. First button (1): Push time = 5. max_duration = 5, best_index = 1.
  2. Second button (2): Push time = 12 - 5 = 7. max_duration = 7, best_index = 2.
  3. Third button (1): Push time = 15 - 12 = 3. Result: Button 2.

Common mistakes candidates make

  • Miscalculating the first button: Forgetting that the first button's push time starts from time 0.
  • Tie-breaking errors: Not checking the index when push times are equal (e.g., returning the last seen index instead of the smallest).
  • Hardcoding array indices: Getting confused between the button index (value at logs[i][0]) and the log index (i).

Interview preparation tip

Always pay close attention to "tie-breaker" requirements in "find the max/min" problems. Often, the problem specifies returning the smallest index or the first occurrence, which changes your comparison logic from > to >= or requires an extra if condition.

Similar Questions