The 4 Keys Keyboard coding problem describes a specialized keyboard with four keys:
This problem is a classic at Microsoft and Google because it tests Dynamic Programming (DP) skills. It requires the candidate to identify the "optimal substructure"—recognizing that the best way to get a large number of 'A's is to print some, then repeatedly select, copy, and paste.
This is a Dynamic Programming interview pattern. You build a dp table where dp[i] represents the maximum 'A's possible with i keystrokes. To calculate dp[i], you either:
dp[i-1] + 1).j where you performed a "Select + Copy" and then performed "Paste" times.For :
In DP problems involving sequences of actions, try to find the "jump" point. Here, the jump is the Copy-Paste sequence. Visualize how the buffer changes to understand the state transitions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Egg Drop With 2 Eggs and N Floors | Medium | Solve | |
| 2 Keys Keyboard | Medium | Solve | |
| Integer Break | Medium | Solve | |
| Rotated Digits | Medium | Solve | |
| Number of Ways to Build House of Cards | Medium | Solve |