Spell Out Every Keypad Word for a Digit Sequence With Vocabulary Pruning

Last updated: November 29, 2026

Quick Overview

Given a string of digits 2 to 9, return every letter string it can spell on a phone keypad. As a follow-on, given a vocabulary, return only spellings that are vocabulary words, pruning branches whose prefix matches no word.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a string of digits 2 to 9, return every letter string it can spell on a phone keypad. As a follow-on, given a vocabulary, return only spellings that are vocabulary words, pruning branches whose prefix matches no word.

WhatsApp and Messenger still serve feature-phone markets where T9-style input matters. Meta phone screens start with the plain enumeration and then add the vocabulary to see whether you prune early rather than filter at the end.

What the Interviewer Expects
  • Map digits to letters and branch per digit
  • Return an empty list for empty input
  • Explain the 4^n worst case
  • Prune with a prefix set or trie when a vocabulary is given
  • Keep the plain and pruned versions in one clean function
Key Topics to Cover
Backtracking
Hash map
Prefix pruning
Strings
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you rank vocabulary matches by word frequency?
  • How would you support digits 0 and 1 mapping to space or punctuation?
  • How would you replace the prefix set with a trie to save memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Digits only from 2 to 9, length up to 10.
  • Without a vocabulary, return all spellings.
  • With a vocabulary, return spellings that are full words of the same length....
High-Level Design

Recurse over digit positions, appending each letter mapped to the current digit and popping after the call. When the vocabulary is given, precompute the set of all its prefixes. At every node, if the ...


Submit Your Answer
Markdown supported