Bracket the first matching substring in each word
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's technical phone screen and at Meta, last asked Jan 2026. It tests strings and trie.
Implement wrap_substrings(s: str, elements: list[str]) -> str.
s is a sentence of words separated by single spaces. elements is a list of strings. For each word in s, find the first substring of that word that equals one of the elements and wrap it in square brackets. Leave words with no match unchanged, and keep the spaces between words as they are.
Rules for choosing the match in a word:
- Pick the match that starts at the smallest index in the word.
- If several elements match at that index, pick the longest one.
- Wrap only that one match; later matches in the same word are left alone.
- Matching is case-sensitive. Ignore empty strings in
elements.
Constraints: 0 <= len(s) <= 10^5, 0 <= len(elements) <= 10^4, each element has at most 100 characters.
Example 1
s = "orange banana kiwi"
elements = ["an", "ana", "wi"]
output: "or[an]ge b[ana]na ki[wi]"
In banana, both an and ana start at index 1, so the longer ana is wrapped.
Example 2
s = "abc hello"
elements = ["c", "ab", "abc"]
output: "[abc] hello"
c also matches in abc, but ab and abc start earlier, and abc is the longer of those. hello has no match.
Start with a straightforward solution, then make it fast when there are many elements.
Clarifying questions
- Can
scontain empty words, from repeated or leading spaces? - If
elementshas duplicates, does that change anything? - Can an element contain a space, so it could only match across words? (Matches are within a word.)
- Is the matching case-sensitive for every character, including digits and punctuation?
What a strong answer covers
- Starts with the direct approach: for each word and each start index, try every element. Notes its cost with
10^4elements. - Builds a trie of the elements (or a set per element length, lengths up to 100). For each start index in a word, walks the trie and records the longest element that ends there; the first start with any match wins.
-
O(len(s) * L)for maximum element lengthL, independent of the number of elements; Aho-Corasick brings it toO(len(s) + matches). - Rebuilds the sentence with one join, keeping the original spacing.
- Tests both examples, a word with no match, empty
elements, and an element equal to a whole word.
Follow-ups
- Bracket every non-overlapping match in each word, not just the first.
- Merge overlapping matches into one bracket.
- Match across the whole sentence instead of per word.
Where it's asked
- Uber · Technical phone screen · Last asked Jan 202660 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design
- Meta
What it tests
strings · trie
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

