Match Each Query to the Word With Longest Common Suffix
Last updated: November 29, 2026
Quick Overview
Given container words and query words, for each query return the index of the container word sharing the longest common suffix with it, breaking ties by shortest length and then smallest index.
0
0
0 solved
Given container words and query words, for each query return the index of the container word sharing the longest common suffix with it, breaking ties by shortest length and then smallest index.
A Google onsite string problem framed around matching file names or domains by their endings. The interviewer checks a reversed trie that stores the best candidate index at every node, with the tie-breaking rule precomputed during insertion.
What the Interviewer Expects
- Reverse words so suffixes become prefixes
- Store at each node the best index by (length, index)
- Initialize the root with the globally best word for zero-length matches
- Answer each query by walking as far as possible and reading the deepest node
- Discuss total input size bounds
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 you return all indices tied for the best match?
- How would you support adding container words online?
- What changes if ties should prefer the longest word?
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: container and queries, lists of lowercase strings. Output: for each query, the index i in container maximizing the common suffix length with the query; among ties, smallest `len(contain...
High-Level Design
Reverse every container word and insert it into a trie. At every node along its path, including the root, store the best index so far under the ordering (len(word), index): replace the stored index ...