Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
time.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:
5 - 2 = 3 units of time.9 - 5 = 4 units of time.15 - 9 = 6 units of time.Example 2:
Input: events = [[10,5],[1,7]]
Output: 10
Explanation:
7 - 5 = 2 units of time.Constraints:
1 <= events.length <= 1000events[i] == [indexi, timei]1 <= indexi, timei <= 105events is sorted in increasing order of timei.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,2],[2,5],[3,9],[1,15]]
[[10,5],[1,7]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3386: Button with Longest Push Time
class Solution {
public int buttonWithLongestTime(int[][] events) {
int ans = events[0][0], t = events[0][1];
for (int k = 1; k < events.length; ++k) {
int i = events[k][0], t2 = events[k][1], t1 = events[k - 1][1];
int d = t2 - t1;
if (d > t || (d == t && ans > i)) {
ans = i;
t = d;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3386: Button with Longest Push Time
func buttonWithLongestTime(events [][]int) int {
ans, t := events[0][0], events[0][1]
for k, e := range events[1:] {
i, t2, t1 := e[0], e[1], events[k][1]
d := t2 - t1
if d > t || (d == t && i < ans) {
ans, t = i, d
}
}
return ans
}
# Accepted solution for LeetCode #3386: Button with Longest Push Time
class Solution:
def buttonWithLongestTime(self, events: List[List[int]]) -> int:
ans, t = events[0]
for (_, t1), (i, t2) in pairwise(events):
d = t2 - t1
if d > t or (d == t and i < ans):
ans, t = i, d
return ans
// Accepted solution for LeetCode #3386: Button with Longest Push Time
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3386: Button with Longest Push Time
// class Solution {
// public int buttonWithLongestTime(int[][] events) {
// int ans = events[0][0], t = events[0][1];
// for (int k = 1; k < events.length; ++k) {
// int i = events[k][0], t2 = events[k][1], t1 = events[k - 1][1];
// int d = t2 - t1;
// if (d > t || (d == t && ans > i)) {
// ans = i;
// t = d;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3386: Button with Longest Push Time
function buttonWithLongestTime(events: number[][]): number {
let [ans, t] = events[0];
for (let k = 1; k < events.length; ++k) {
const [i, t2] = events[k];
const d = t2 - events[k - 1][1];
if (d > t || (d === t && i < ans)) {
ans = i;
t = d;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.