Uber OA Interview Question: Count Key Changes While Typing

20 Views
No Comments

You are given an array of uppercase and lowercase English letters, recording, representing a sequence of letters typed by the user.

Your task is to count the number of times that the user changed keys while typing the sequence, considering that the uppercase and lowercase letters for a given letter require the user to press the same letter key, ignoring modifiers like Shift or Caps Lock.

For example, typing 'w' and 'W' require the user to press the same key, whereas typing 'w' and 'E' or typing 'w' and 'e' require the user to change keys.

Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(recording.length^2) will fit within the execution time limit.

Example

For recording = ['w', 'w', 'a', 'A', 'a', 'b', 'B'], the output should be solution(recording) = 2.

Explanation:

    The key idea is to normalize every character to a single case, then compare each letter with the previous typed key. If the normalized current key differs from the normalized previous key, the user changed keys and the counter increases by one. This yields a simple O(n) scan with O(1) extra space, which easily fits the constraints. The examples show that uppercase and lowercase variants of the same letter are treated as the same key, so only transitions between different letters count.

END
 0