Skip to main content

Command Palette

Search for a command to run...

Slowest Key

Published
2 min read
Slowest Key
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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 i<sup>th</sup> key pressed in the testing sequence, and a sorted list releaseTimes, where releaseTimes[i] was the time the i<sup>th</sup> key was released. Both arrays are 0-indexed. The 0<sup>th</sup> 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 i<sup>th</sup> keypress had a duration of releaseTimes[i] - releaseTimes[i - 1], and the 0<sup>th</sup> 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.*

LeetCode Problem - 1629

class Solution {
    public char slowestKey(int[] releaseTimes, String keysPressed) {

        // Initialize duration to the time for the first key press
        int duration = releaseTimes[0];
        // The first character is the initial slowest key
        char ch = keysPressed.charAt(0);

        // Loop through the remaining characters in the keysPressed string
        for (int i = 1; i < keysPressed.length(); i++) {
            // Calculate the duration of the current key press (time difference between releases)
            int tempDuration = releaseTimes[i] - releaseTimes[i - 1];
            // Get the current character from the keysPressed string
            char currentChar = keysPressed.charAt(i);

            // If the current key press duration is greater than the previous slowest key duration
            // or if both durations are equal but the current key has a higher alphabetical order,
            // update the slowest key.
            if (tempDuration > duration || (tempDuration == duration && currentChar > ch)) {
                ch = currentChar; // Update the slowest key character
                duration = tempDuration; // Update the duration to the current one
            }
        }

        // Return the slowest key that was pressed
        return ch;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.