# Prefix And Suffix Search: Practice Notes

Prefix And Suffix Search shows up often in coding rounds. Here is how I talk through it when the interviewer is watching.

## Key idea

Insert every word into a Trie under keys of the form suffix + '{' + word (or wrap as suffix#word) so f(prefix, suffix) is a single path. Store the largest weight seen along that path, which is the latest matching word index.

## Steps to say out loud

1. Restate the input and the exact ask, and nail down constraints and overflow.
2. Name the pattern (a heap or priority queue, Union-Find, interval scheduling, bitmask DP, a topological order, or a Trie) and why a nested scan or naive search fails.
3. Code it, then trace a short example plus one empty or already-solved edge case.

## Complexity

O(L^2) preprocess per word, O(P+S) per query.

## Common mistake

Scanning every word for startswith/endswith on each query, which is too slow for the limits.

## Practice tip

Time yourself: two minutes to explain, ten to code, three to test edges. Disclosure: I work on Linkjob AI. I also keep a practice cheat sheet at AI assistant for technical interviews.