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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
Hash Map
Palindromes
String Splitting
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 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 Problems
Sample Answer
Requirements
  • Input: list terms of unique strings, possibly including ''.
  • Output: list of [i, j] with i != j and terms[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 pre is a palindrome and ...

Submit Your Answer
Markdown supported