Ctrl K

The Longest-Held Key

Easy
A tester tried out a new keyboard by pressing keys one at a time with no pauses. The string keys lists the keys in the order they were pressed, and release_times[i] is the moment the i-th key was released (0-based). The first key went down at time 0, and each next key went down exactly when the previous one was released. So the i-th press lasted release_times[i] - release_times[i - 1], and the very first one lasted release_times[0]. Return the key whose press lasted the longest. If several presses tie, return the key that comes latest in the alphabet among them. The same key may have been pressed more than once, with a different duration each time.
Example 1
release_times=[4, 6, 12, 13], keys='mkqm' → 'q'

The presses last 4, 2, 6 and 1. The 'q' was held the longest.

Example 2
release_times=[5, 10, 13], keys='bab' → 'b'

Both 'b' and 'a' were held for 5. On a tie we pick the letter later in the alphabet, which is 'b'.

Example 3
release_times=[1, 100], keys='zz' → 'z'
Constraints
  • 2 ≤ len(keys) = len(release_times) ≤ 1000
  • 1 ≤ release_times[i] ≤ 10⁹
  • release_times is strictly increasing
  • keys consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Words, Closest Apart EasyOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate Easy
loading Python…
Input
release_times=[4, 6, 12, 13], keys='mkqm'
Expected
'q'

Run checks the first 3 tests in your browser. Submit checks all 30 tests on the server, hidden ones included.