Skip to main content

Button with Longest Push Time - Solution & Explanation

EasyArray8 min readAsked at: Amazon, Jpmorgan
Practice this problem

Problem Statement

You are given a 2D array events which represents a sequence of events where a child pushes a series of buttons on a keyboard.

Each events[i] = [indexi, timei] indicates that the button at index indexi was pressed at time timei.

  • The array is sorted in increasing order of time.
  • The time taken to press a button is the difference in time between consecutive button presses. The time for the first button is simply the time at which it was pressed.

Return the index of the button that took the longest time to push. If multiple buttons have the same longest time, return the button with the smallest index.

 

Example 1:

Input: events = [[1,2],[2,5],[3,9],[1,15]]

Output: 1

Explanation:

  • Button with index 1 is pressed at time 2.
  • Button with index 2 is pressed at time 5, so it took 5 - 2 = 3 units of time.
  • Button with index 3 is pressed at time 9, so it took 9 - 5 = 4 units of time.
  • Button with index 1 is pressed again at time 15, so it took 15 - 9 = 6 units of time.

Example 2:

Input: events = [[10,5],[1,7]]

Output: 10

Explanation:

  • Button with index 10 is pressed at time 5.
  • Button with index 1 is pressed at time 7, so it took 7 - 5 = 2 units of time.

 

Constraints:

  • 1 <= events.length <= 1000
  • events[i] == [indexi, timei]
  • 1 <= indexi, timei <= 105
  • The input is generated such that events is sorted in increasing order of timei.

Approach Overview

Problem Overview: You are given a list of button press events where each entry contains a button ID and the time the press ended. The duration of a press is the difference between the current timestamp and the previous one. Your task is to determine which button had the longest push duration.

Approach 1: Brute Force Duration Recalculation (O(n^2) time, O(1) space)

A naive approach recomputes durations by scanning backward for every event to determine when the previous press ended. For each index i, calculate the push duration and compare it with the current maximum. This repeatedly revisits earlier entries, leading to unnecessary work. The idea works but scales poorly because every event may trigger another scan of the array.

This approach demonstrates the core insight: the push time is simply the difference between consecutive timestamps. Once you recognize that durations only depend on the previous event, the quadratic work becomes unnecessary.

Approach 2: Single Pass Duration Tracking (O(n) time, O(1) space)

The optimal solution iterates through the event log once. The duration of the first press equals its timestamp because it starts at time 0. For every subsequent event, compute the duration using logs[i][1] - logs[i-1][1]. Track the maximum duration seen so far and update the result when a longer press appears.

If two presses share the same duration, choose the smaller button ID to maintain deterministic output. The algorithm only keeps a few variables: previous timestamp, current duration, and the best button so far. Because the data is already ordered by time, a single forward iteration over the array is enough. This pattern is common in simulation-style problems where events occur sequentially.

Recommended for interviews: Interviewers expect the single-pass solution. The brute force explanation shows you understand how durations are derived, but the optimal approach demonstrates that you can reduce redundant work and exploit sequential ordering. Recognizing that each event only depends on the previous one leads directly to the O(n) scan.

Solution

We define two variables ans and t, representing the index of the button with the longest press time and the press time, respectively.

Next, we start traversing the array events from index k = 1. For each k, we calculate the press time of the current button d = t2 - t1, where t2 is the press time of the current button and t1 is the press time of the previous button. If d > t or d = t and the index i of the current button is less than ans, we update ans = i and t = d.

Finally, we return ans.

The time complexity is O(n), where n is the length of the array events. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Duration RecalculationO(n^2)O(1)Conceptual understanding of duration calculation; not practical for large inputs
Single Pass Duration TrackingO(n)O(1)Optimal approach when events are already sorted by time

Video Solution

3386. Button with Longest Push Time (Leetcode Easy) • Programming Live with Larry • 302 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Button with Longest Push Time easy or hard?
Button with Longest Push Time is classified as an Easy problem. The main skill tested is recognizing that the duration of each press depends only on the previous timestamp and implementing a clean O(n) scan.
Button with Longest Push Time Python/Java solution
The implementation is straightforward in Python, Java, C++, Go, or TypeScript. Iterate through the array, compute the duration using timestamp differences, and update the result when a longer press is found. The logic stays identical across languages.
How to solve Button with Longest Push Time in O(n)?
Iterate through the log once and compute each press duration as the difference between the current timestamp and the previous one. Track the maximum duration and update the button ID when a longer press appears. Because every entry is processed once, the total time complexity remains O(n).
What is the best approach for Button with Longest Push Time?
The best approach is a single pass over the event log. Compute the duration of each button press using the difference between consecutive timestamps and track the maximum duration. This runs in O(n) time with O(1) extra space and works because the events are already ordered by time.
Is Button with Longest Push Time asked at Google/Amazon/Meta?
Array-based event processing problems like this commonly appear in interviews at companies such as Amazon, Google, and Meta. While the exact problem may vary, the pattern of scanning logs and computing differences between timestamps is a frequent interview theme.
What data structure is used in Button with Longest Push Time?
The primary data structure is an array that stores button IDs and timestamps. The algorithm processes the array sequentially and keeps a few variables to track the longest duration and corresponding button.
What is the time complexity of Button with Longest Push Time?
The optimal solution runs in O(n) time where n is the number of button events. Each event is processed exactly once to compute its duration. The algorithm only keeps a few variables, so the space complexity is O(1).

Ready to solve this problem?

Practice Button with Longest Push Time with our built-in code editor and test cases.

Practice on FleetCode