← Questions

Find words that contain another word from the list

0
MetaCodingLast asked Apr 2026

A coding interview question asked in Meta's technical phone screen, last asked Apr 2026. It tests strings, tries and ai-assisted coding.

Given a list of strings, find a string that contains a different string from the same list as a contiguous substring. A brute-force version that compares every pair is the starting point; write it, then two or three faster approaches, and compare their costs.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • find_container(words: list[str]) -> str — return any one string that contains another, different string from the list; "" when none does. A string doesn't count as containing itself, and a duplicate copy of the same text isn't a different string.

Example

find_container(["programming", "am", "pro"])  ->  "programming"
find_container(["cat", "dog", "bird"])        ->  ""
find_container(["ab", "ab", "xaby"])          ->  "xaby"   # "ab" twice alone is not a match

Constraints

  • 1 <= len(words) <= 10^4; each word has 1 to 1,000 characters; total length at most 2 * 10^5.
  • Case-sensitive; duplicates may appear.

Clarifying questions

  • Return one string, or all strings that contain another?
  • Do duplicate copies of the same text count as containing each other?
  • Is the check case-sensitive?
  • What are the sizes: many short words, or a few long ones?

What a strong answer covers

  • Brute force over all pairs with a substring check, and its cost (O(n^2) checks, each up to O(L^2) naively or O(L) with a good search).
  • Sort by length so only shorter words are checked against longer ones, and deduplicate first.
  • Put every word in a hash set and check every substring of each word against it: O(sum of L^2), good when words are short.
  • Aho-Corasick: build one automaton of all words and scan each word once, reporting any other word found inside; near-linear in the total length plus matches.
  • Tests: duplicates, one word, a word contained in several others, no match.

Follow-ups

  • Return all strings that contain another.
  • For each string, list which other strings it contains.
  • Words arrive one at a time; report a match as soon as one exists.

Where it's asked

  • Meta · Technical phone screen · Last asked Apr 202645 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted

What it tests

strings · tries · ai-assisted coding

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