Find Pairs of Search Terms That Concatenate Into a Palindrome
Last updated: November 29, 2026
Quick Overview
Given a list of unique search terms, return all ordered index pairs (i, j), i != j, such that terms[i] + terms[j] is a palindrome.
0
0
0 solved
Given a list of unique search terms, return all ordered index pairs (i, j), i != j, such that terms[i] + terms[j] is a palindrome.
A Google onsite string hashing problem framed around a playful Search feature. The brute force is O(n^2 * L); the interviewer wants the reversed-word hash map and the prefix/suffix split argument, with careful handling of empty strings and duplicate pairs.
What the Interviewer Expects
- Builds a map from reversed term to index
- Splits each term at every position into prefix and suffix
- Pairs on a palindromic prefix with the reversed suffix, and vice versa
- Avoids duplicate pairs when the split is at an end
- Handles the empty string term
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 speed up the palindrome checks?
- Return only pairs where both terms are longer than one character.
- What if the term list is too big for one machine?
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: list
termsof unique strings, possibly including''. - Output: list of
[i, j]withi != jandterms[i] + terms[j]a palindrome; order of pairs does not matter. - n up to around 5,00...
High-Level Design
Index every term by its reverse: rev[t[::-1]] = index. For term w at index i, consider every split w = pre + suf (including the empty prefix and the empty suffix).
- If
preis a palindrome and ...