Check Whether Handle Words Follow a Pattern Bijectively
Last updated: November 29, 2026
Quick Overview
Given a pattern string like "abba" and a space-separated list of handle words, return True if there is a one-to-one mapping between pattern letters and words such that the words follow the pattern.
Meta
November 29, 20260
0
0 solved
Given a pattern string like "abba" and a space-separated list of handle words, return True if there is a one-to-one mapping between pattern letters and words such that the words follow the pattern.
A short hashing question used in Meta phone screens, here framed as validating vanity handle templates on Instagram. The interviewer is checking that you enforce the mapping in both directions, which is the bug most candidates ship.
What the Interviewer Expects
- Uses two dictionaries (letter to word and word to letter) or an equivalent check
- Rejects inputs where the counts of letters and words differ
- Catches the case where two letters map to the same word
- Gives O(n) time in the total input length
- Walks through a failing example such as "abba" with "dog dog dog dog"
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if words are concatenated with no spaces, so you must also find the split?
- How would you group a large set of handles by their pattern shape?
- Can you solve it with a single dictionary by mapping both sides to first-seen index?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: pattern of lowercase letters and words, a string of tokens separated by single spaces. Output: True if each letter maps to exactly one word and each word is mapped from exactly one letter...
High-Level Design
Split the words. If the lengths differ, return False. Walk position by position with two maps. For letter c and word w: if c already maps to something other than w, fail; if w already maps...