Skip to main content

Slowest Key - Solution & Explanation

EasyArrayString13 min readAsked at: Jpmorgan
Practice this problem

Problem Statement

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.

 

Example 1:

Input: releaseTimes = [9,29,49,50], keysPressed = "cbcd"
Output: "c"
Explanation: The keypresses were as follows:
Keypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).
Keypress for 'b' had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).
Keypress for 'c' had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).
Keypress for 'd' had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).
The longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20.
'c' is lexicographically larger than 'b', so the answer is 'c'.

Example 2:

Input: releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
Output: "a"
Explanation: The keypresses were as follows:
Keypress for 's' had a duration of 12.
Keypress for 'p' had a duration of 23 - 12 = 11.
Keypress for 'u' had a duration of 36 - 23 = 13.
Keypress for 'd' had a duration of 46 - 36 = 10.
Keypress for 'a' had a duration of 62 - 46 = 16.
The longest of these was the keypress for 'a' with duration 16.

 

Constraints:

  • releaseTimes.length == n
  • keysPressed.length == n
  • 2 <= n <= 1000
  • 1 <= releaseTimes[i] <= 109
  • releaseTimes[i] < releaseTimes[i+1]
  • keysPressed contains only lowercase English letters.

Approach Overview

Problem Overview: You receive an array releaseTimes and a string keysPressed. Each index represents a key press event. The duration of a key press equals the difference between the current release time and the previous release time. The task is to find the key with the longest press duration. If multiple keys share the same duration, return the lexicographically largest key.

Approach 1: Using Temporary Array for Duration Calculation (O(n) time, O(n) space)

This approach explicitly computes the duration of each key press and stores it in a temporary array. The first duration is simply releaseTimes[0], since the first key starts at time 0. For every other index i, compute releaseTimes[i] - releaseTimes[i-1]. After building the duration array, iterate through it to track the maximum duration and update the corresponding key. If two durations are equal, compare characters and keep the lexicographically larger key. This method separates computation and evaluation, which can make debugging easier when you want to inspect intermediate values.

Since you process the input twice (once to compute durations and once to find the answer), the time complexity remains O(n). The additional array introduces O(n) extra space. This approach is useful when clarity or intermediate inspection is preferred over minimal memory usage.

Approach 2: Simple Iteration with Maximum Duration Tracking (O(n) time, O(1) space)

This is the optimal solution used in most interview settings. Instead of storing all durations, compute each duration on the fly while iterating through the input. Maintain two variables: the current maximum duration and the result key. For index 0, the duration is releaseTimes[0]. For every other index, compute releaseTimes[i] - releaseTimes[i-1] and immediately compare it with the current maximum.

If the new duration is greater than the maximum, update both the maximum and the result key. If the duration is equal, choose the lexicographically larger character using a direct character comparison. Because each element is processed once and no additional structures are allocated, the algorithm runs in O(n) time with O(1) space.

This solution mainly relies on simple iteration over an array and comparison of characters from a string. The key insight is that the duration of each press depends only on the previous timestamp, so storing all durations is unnecessary.

Recommended for interviews: The single-pass duration tracking approach is what interviewers expect. It demonstrates that you recognize the sequential relationship between timestamps and avoid unnecessary memory usage. Mentioning the temporary-array version first can show your reasoning process, but implementing the O(1) space solution highlights stronger problem-solving skills.

Approach 1: Simple Iteration with Maximum Duration Tracking

This approach involves iterating through both releaseTimes and keysPressed to calculate the duration of each keypress. We'll track the maximum duration found so far and update our result each time we find a keypress with a longer duration. If two keypresses have the same duration, we choose the lexicographically larger key.

We initialize the maximum duration with the duration of the first keypress and iteratively check each subsequent duration. The key with the longest duration is tracked and returned. The code checks if the current duration is greater than the maximum duration found so far or if the key is lexicographically larger when durations are equal.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the releaseTimes array. We iterate through the list once.
Space Complexity: O(1), as we only use constant extra space for variables.

Try this approach in the editor →

Approach 2: Using Temporary Array for Duration Calculation

In this approach, we create a temporary array to store the duration of each key press. We then iterate over this array to find the maximum duration. This solution separates the concerns of calculation and comparison for more clarity.

This approach uses an array to store durations, which is iterated through twice. This can make the logic a bit clearer by separating computation and decision logic.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n) due to additional array usage.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Simple Iteration with Maximum Duration Tracking

Time Complexity: O(n), where n is the length of the releaseTimes array. We iterate through the list once.
Space Complexity: O(1), as we only use constant extra space for variables.

Using Temporary Array for Duration Calculation

Time Complexity: O(n)
Space Complexity: O(n) due to additional array usage.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Using Temporary Array for Duration CalculationO(n)O(n)When you want clearer step-by-step computation or need to inspect all durations separately.
Simple Iteration with Maximum Duration TrackingO(n)O(1)Best general solution. Minimal memory usage and expected approach in interviews.

Video Solution

Leetcode 1629. Slowest Key • Fraz • 2,836 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Slowest Key easy or hard?
Slowest Key is categorized as an Easy problem on LeetCode with an acceptance rate around 59%. The challenge mainly involves correctly computing durations and handling the lexicographical tie-breaking condition. It is commonly used as a warm-up problem for array iteration and string comparison.
Slowest Key Python/Java solution
In both Python and Java, the solution iterates through releaseTimes, computes the duration for each key press, and updates the maximum duration and result character. Character comparison handles tie-breaking when durations match. The implementation remains O(n) time and O(1) space in both languages.
How to solve Slowest Key in O(n)?
Iterate through releaseTimes and compute each key's press duration using the difference between consecutive timestamps. Track the maximum duration and the corresponding key while scanning the array. If two durations match, return the lexicographically larger character. Because the array is processed once, the algorithm achieves O(n) time complexity.
What is the best approach for Slowest Key?
The best approach is a single-pass iteration that tracks the maximum key press duration while scanning the input. For each index, compute the duration as releaseTimes[i] minus releaseTimes[i-1] (or releaseTimes[0] for the first key). Update the maximum duration and result key accordingly, breaking ties using lexicographical comparison. This solution runs in O(n) time and O(1) space.
Is Slowest Key asked at Google/Amazon/Meta?
Slowest Key is a typical easy-level array and string problem used in coding interviews to test basic iteration and comparison logic. Variants of duration tracking and event-based array problems have appeared in interviews at companies like Amazon and Google, especially for early interview rounds.
What data structure is used in Slowest Key?
The solution primarily uses an array for the release times and a string for the pressed keys. The algorithm performs sequential iteration and simple arithmetic to compute durations. No advanced data structures such as hash maps or heaps are required for the optimal solution.
What is the time complexity of Slowest Key?
The optimal solution runs in O(n) time because each key press is processed exactly once. Only constant-time operations are performed during the iteration, such as subtraction and character comparison. Space complexity is O(1) since no additional data structures are required beyond a few variables.

Ready to solve this problem?

Practice Slowest Key with our built-in code editor and test cases.

Practice on FleetCode