Search Files by Both Name Prefix and Extension Suffix
Last updated: November 29, 2026
Quick Overview
Build a structure over a list of file names that, given a prefix and a suffix, returns the largest index of a name that starts with the prefix and ends with the suffix, or -1.
0
0
0 solved
Build a structure over a list of file names that, given a prefix and a suffix, returns the largest index of a name that starts with the prefix and ends with the suffix, or -1.
A Google Drive flavored onsite question where search filters combine a name prefix with an extension. The interviewer wants a single trie that answers both constraints at once, usually via the suffix#word insertion trick, and an honest memory analysis.
What the Interviewer Expects
- Explain why separate prefix and suffix tries need an intersection step
- Insert suffix + '#' + word for every suffix of every word
- Store the latest index at each node
- Answer a query with one walk over suffix + '#' + prefix
- Discuss memory cost of O(n L^2)
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 do it with two tries and sorted index lists intersected by two pointers?
- How would you cap memory for very long file names?
- What if queries also need the count of matching files?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Constructor takes words (file names, up to 10^4, each up to 10 characters in the classic version). query(prefix, suffix) returns the largest index i such that words[i] starts with prefix and...
High-Level Design
A word satisfies the query iff the string suffix + '#' + word starts with suffix + '#' + prefix for some suffix of the word equal to the query suffix. So for each word at index i and each of its...