Magicsheet logo

Minimum Number of Pushes to Type Word II

Medium
49%
Updated 6/1/2025

Minimum Number of Pushes to Type Word II

1. What is this problem about?

This is an advanced version of the keypad mapping problem. Unlike the first version, the word can contain repeating letters. You still have 8 keys (2-9), but now you must decide which letters to place in the first, second, third, etc., positions on these keys to minimize the total number of key presses. Letters that appear more frequently in the word should obviously take fewer pushes.

2. Why is this asked in interviews?

This "Minimum Number of Pushes to Type Word II interview question" is popular at DE Shaw and Meta because it combines frequency counting with greedy optimization. It tests if a candidate can recognize that the most frequent characters should be assigned to the most "accessible" positions (the 1st position on any key). It's a classic Huffman-style greedy problem.

3. Algorithmic pattern used

The pattern is Sorting combined with Greedy logic.

  1. Count the frequency of each letter in the word using a hash map or a frequency array.
  2. Sort these frequencies in descending order.
  3. Assign the top 8 most frequent letters to the 1st position (1 push each).
  4. Assign the next 8 most frequent to the 2nd position (2 pushes each), and so on.

4. Example explanation

Word: "aabbccddeeffgghhiii"

  • 'i' appears 3 times, others appear 2.
  • Frequencies: i:3, a:2, b:2, c:2, d:2, e:2, f:2, g:2, h:2.
  • 1st position (8 keys): 'i' (3x1), 'a' (2x1), 'b' (2x1), 'c' (2x1), 'd' (2x1), 'e' (2x1), 'f' (2x1), 'g' (2x1).
  • 2nd position: 'h' (2x2). Total = (3+2+2+2+2+2+2+2) + (4) = 17 + 4 = 21 pushes.

5. Common mistakes candidates make

A common mistake is failing to sort the frequencies. If you assign a rare letter to the 1st position and a frequent letter to the 4th position, your total cost will be much higher. Another error is not correctly incrementing the "multiplier" (the push count) after every 8 unique letters are processed.

6. Interview preparation tip

Whenever a problem involves "cost" and "frequency," sorting is almost always involved. Practice using frequency arrays for character-based problems, as they are often more efficient than hash maps when dealing only with the 26 lowercase English letters.

Similar Questions