← Questions

Bracket the first matching substring in each word

0
MetaUberCodingLast asked Jan 2026

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 s contain empty words, from repeated or leading spaces?
  • If elements has 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^4 elements.
  • 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 length L, independent of the number of elements; Aho-Corasick brings it to O(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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding