Magicsheet logo

Minimum Distance to Type a Word Using Two Fingers

Hard
25%
Updated 8/1/2025

Asked by 1 Company

Minimum Distance to Type a Word Using Two Fingers

1. What is this problem about?

The Minimum Distance to Type a Word Using Two Fingers problem is a complex optimization challenge set on a virtual keyboard. The keyboard is arranged in a grid (usually 6 columns). You use two fingers to type a word, and you want to minimize the total distance moved by both fingers. The distance between two keys is the Manhattan distance between their coordinates. You can start with your fingers on any two keys at zero cost. This problem requires balancing the movements of two independent agents (fingers) to achieve a global minimum.

2. Why is this asked in interviews?

Google often asks the Minimum Distance to Type a Word Using Two Fingers interview question to evaluate a candidate's mastery of Dynamic Programming (DP). It tests your ability to define a state that captures all necessary information to make future decisions. This problem is particularly interesting because it involves "state compression"—you don't need to know the entire history, just where both fingers currently are.

3. Algorithmic pattern used

The core algorithmic pattern used is Dynamic Programming. The state can be defined as dp(index, finger1, finger2), representing the minimum distance to type the rest of the word starting from index with the fingers at the specified positions. An important optimization is realizing that one finger must always be at the character typed at index - 1. Thus, the state can be simplified to dp(index, other_finger). This "String, Dynamic Programming interview pattern" is crucial for solving multi-agent optimization problems.

4. Example explanation

Consider typing "CA". Keyboard layout: A(0,0), B(0,1), C(0,2)...

  • First finger types 'C' (cost 0, finger1 at 'C').
  • To type 'A', you have two choices:
    • Move finger1 from 'C' to 'A': distance 00+20=2|0-0| + |2-0| = 2.
    • Use finger2 to type 'A' (cost 0, finger2 at 'A'). The minimum distance for "CA" is 0 if you use two fingers for the two letters. For longer words like "CAKE", the choice of which finger to move at each step affects all subsequent costs.

5. Common mistakes candidates make

In the Minimum Distance to Type a Word Using Two Fingers coding problem, candidates often struggle with the initial finger placement. They might think they need to try all possible starting pairs, but the problem allows starting at zero cost, which can be handled by a special "null" state for the fingers. Another mistake is using a greedy approach—always moving the closer finger. Greedy fails because moving a finger a bit further now might put it in a much better position for several upcoming letters.

6. Interview preparation tip

For DP problems involving multiple "pointers" or "fingers," always try to reduce the dimensions of your DP table. If one pointer's position is always known from the current step, you can omit it from the state. This is a common "Dynamic Programming interview pattern" that separates senior candidates from juniors. Practice mapping 1D indexes (0-25 for A-Z) to 2D grid coordinates (row=char/6,col=char%6)(row = char/6, col = char\%6).

Similar Questions