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.

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

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
trie
suffix#prefix trick
indexing
space-time trade-off
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported