Slowest Key - Problem

A newly designed keypad was tested, where a tester pressed a sequence of n keys, one at a time.

You are given a string keysPressed of length n, where keysPressed[i] was the ith key pressed in the testing sequence, and a sorted list releaseTimes, where releaseTimes[i] was the time the ith key was released. Both arrays are 0-indexed. The 0th key was pressed at the time 0, and every subsequent key was pressed at the exact time the previous key was released.

The tester wants to know the key of the keypress that had the longest duration. The ith keypress had a duration of releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress had a duration of releaseTimes[0].

Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key may not have had the same duration.

Return the key of the keypress that had the longest duration. If there are multiple such keypresses, return the lexicographically largest key of the keypresses.

Input & Output

Example 1 — Basic Case
$ Input: releaseTimes = [9,12,16,20], keysPressed = "abcd"
Output: c
💡 Note: Durations: a=9, b=12-9=3, c=16-12=4, d=20-16=4. Maximum duration is 4. Both 'c' and 'd' have duration 4, but 'c' < 'd' lexicographically, so we return 'c'.
Example 2 — Lexicographical Tie
$ Input: releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
Output: a
💡 Note: Durations: s=12, p=11, u=13, d=10, a=16. Maximum duration is 16 for key 'a'.
Example 3 — First Key Longest
$ Input: releaseTimes = [10,15,18], keysPressed = "xyz"
Output: x
💡 Note: Durations: x=10, y=15-10=5, z=18-15=3. Maximum duration is 10 for key 'x'.

Constraints

  • 1 ≤ releaseTimes.length ≤ 1000
  • 0 < releaseTimes[i] ≤ 109
  • releaseTimes[i-1] < releaseTimes[i] for all valid i
  • keysPressed.length == releaseTimes.length
  • keysPressed contains only lowercase English letters

Visualization

Tap to expand
Slowest Key: Find Longest DurationRelease Times91216Keys PressedabcCalculate Durations9349-012-916-12Maximum duration is 9 for key 'a'aResult
Understanding the Visualization
1
Input
Release times and corresponding keys
2
Calculate Durations
Find time difference for each keypress
3
Find Maximum
Identify key with longest duration
Key Takeaway
🎯 Key Insight: Calculate key press duration as the difference between consecutive release times, then track the maximum duration with lexicographically largest key for ties
Asked in
Amazon 15 Apple 8
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen