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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
palindromes
hash map
string splitting
trie
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 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 Problems
Sample Answer
Requirements
  • Input: distinct lowercase strings, possibly including "".
  • Output: list of [i, j] pairs with i != 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):

  1. If left is a palindrome and reverse(right) is another word `...

Submit Your Answer
Markdown supported