Pairs of Usernames That Concatenate Into a Palindrome
Last updated: November 29, 2026
Quick Overview
Given a list of distinct usernames, return every ordered pair of indices (i, j), i != j, such that names[i] + names[j] reads the same forwards and backwards.
Meta
November 29, 20260
0
0 solved
Given a list of distinct usernames, return every ordered pair of indices (i, j), i != j, such that names[i] + names[j] reads the same forwards and backwards.
This tests whether a candidate can break a pairwise string problem into prefix and suffix cases instead of checking all n^2 concatenations. Meta interviewers expect careful handling of empty strings and double counting, which is where most solutions go wrong.
What the Interviewer Expects
- Explain the split: a word pairs with another when one part is a palindrome and the other part reversed exists
- Use a hash map from word to index (or a trie of reversed words)
- Handle the empty-string username explicitly
- Avoid emitting the same pair twice when a split is at either end
- Analyze time in terms of n and word length k
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
- How would a trie of reversed words change the complexity?
- How would you count pairs instead of listing them?
- What if usernames are not distinct?
- How would you scale this to millions of handles?
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: distinct lowercase strings, possibly including
"". - Output: list of
[i, j]pairs withi != j, in any order, no duplicates. - Words have length up to
k, so we want roughly O(n * k^2) ...
High-Level Design
Put each word's index in a map keyed by the word. For a word w, try every split w = left + right for cut positions 0..len(w):
- If
leftis a palindrome andreverse(right)is another word `...