Find palindrome-forming string pairs
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates string-processing and algorithmic design skills, including knowledge of efficient lookup data structures, palindrome properties, handling of edge cases, and time/space complexity analysis.
Constraints
- 0 <= len(words) <= 100000
- All words are distinct and contain only lowercase English letters
- 0 <= len(words[i])
- The sum of all string lengths is at most 200000
Examples
Input: (["bat", "tab", "cat"],)
Expected Output: [[0, 1], [1, 0]]
Explanation: "bat" + "tab" and "tab" + "bat" are palindromes; no pair involving "cat" works.
Input: (["abcd", "dcba", "lls", "s", "sssll"],)
Expected Output: [[0, 1], [1, 0], [2, 4], [3, 2]]
Explanation: The reverse-word pairs are [0,1] and [1,0]. Also, "lls" + "sssll" = "llssssll" and "s" + "lls" = "slls", both palindromes.
Hints
- Comparing every pair is too slow. Try indexing reversed words so that matching candidates can be found while scanning a word character by character.
- A trie of reversed words works well if each node also remembers which words have a palindromic remaining prefix. Precompute palindromic prefixes and suffixes in linear time per word.