Minimum Number of Keypresses
A medium-tier problem at 71% community acceptance, tagged with Hash Table, String, Greedy. Reported in interviews at Snap and 0 others.
Minimum Number of Keypresses is a medium-difficulty problem that asks you to count the total keypresses needed to type a string on an old phone keypad, where repeated button presses cycle through characters. It's been asked at Snap and has a 71% acceptance rate, which sounds high until you realize most who pass either saw it before or got lucky with their greedy insight. The trap is thinking you need to simulate the typing; the actual win is recognizing that you're just sorting characters by frequency and assigning them to the cheapest slots. If this problem lands in your live assessment and you freeze on the pattern, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Minimum Number of Keypresses"
Minimum Number of Keypresses is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe core trick is greedy assignment. Each character has a frequency, and you pay 1 keypress per character per appearance. The nine keys on a phone keypad have different 'costs': key 2 has 3 characters (abc, 1 press each), key 3 has 3 characters (def, 2 presses each for d and e, 3 for f), and so on. To minimize total keypresses, assign the most frequent characters to the cheapest slots. Sort characters by frequency descending, then assign them to available slots in order of cost ascending. Common failure: trying to optimize the assignment dynamically or overthinking the keypad layout. The greedy approach is provably optimal because you're always minimizing the sum of (frequency * cost). Hash Table, Sorting, and Counting form the backbone: count frequencies, sort them, and compute the total. When you hit this live, StealthCoder hedges against the moment you blank on whether to sort ascending or descending.
Pattern tags
You know the problem.
Make sure you actually pass it.
Minimum Number of Keypresses recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Number of Keypresses interview FAQ
Is this problem actually a greedy problem or dynamic programming?+
Pure greedy. Once you accept that each character's cost is fixed (1 press, 2 presses, etc. per appearance), assigning high-frequency characters to low-cost slots is provably optimal. DP would waste time and space here. The insight that makes it click is treating it as a slot-filling problem, not a typing simulation.
How do I figure out the keypad cost structure without hardcoding?+
You can hardcode the costs or notice the pattern: keys 2-6 have 3 characters each, 7 has 4 (pqrs), 8 has 3 (tuv), 9 has 4 (wxyz). Most solutions hardcode a list like costs = [1,1,1, 2,2,2, 3,3,3,...]. That's cleaner than deriving it. The problem doesn't penalize you for explicit data.
Does it matter which character in 'abc' I assign to cost 1 vs. the other slots?+
No. Once you know cost 1 has three slots, assign the three most frequent characters to them in any order. The total keypresses depends only on frequency and cost pairing, not on which specific character gets which slot. That's why greedy works: you're optimizing the sum, not the individual mapping.
Is Snap still asking this or is it old interview data?+
Snap is listed as the only named company in available reports. One datapoint isn't enough to call it 'actively asked,' but it shows up in their pool. Medium-difficulty greedy problems like this one tend to rotate in and out. Treat it as 'possible, not certain.'
What's the real difference between this and a frequency-sort problem?+
Frequency sort counts occurrences and orders by count. This one takes that count and multiplies it by a cost per appearance. You're not just sorting; you're mapping frequencies to slots with different per-use costs. That multiplication step is what makes greedy assignment necessary.
Want the actual problem statement? View "Minimum Number of Keypresses" on LeetCode →