FastPrepLongest Word Containing Another Input
Problems/Longest Word Containing Another Input

Longest Word Containing Another Input

Meta logoMeta● HardFULLTIMEPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reports describe finding a listed word containing other listed words and improving a supplied brute-force solver. They omit whether one or every other word must match and how multiple matches are returned. This practice version uses at least one other input, longest-first selection and explicit duplicate, no-match and size rules; the judged core matches the reports at about 75%.

Given an array of nonempty lowercase English strings words, find an input string that contains at least one other input string as a contiguous substring.

An input at index i qualifies when some index j != i satisfies that words[j] occurs in words[i]. Equal strings at different indices count as a match; a string does not match itself at the same index.

Return the longest qualifying string. If several qualifying strings have the same length, return the one appearing first in the input array. Return the empty string "" when none qualifies. Matching is literal and case-sensitive.

Optimization discussion

A direct baseline checks every ordered pair and compares characters at every possible substring start. Explain its time and space cost, then describe two faster approaches and implement one. Length-based pruning and multi-pattern search are useful directions.

Only the returned string is graded; the complexity discussion is interview practice.

Function

findContainingWord(words: String[]) → String

Examples

Example 1

words = ["programming","am","pro"]return = "programming"

programming contains both am and pro. It is the longest qualifying input.

Example 2

words = ["category","cat"]return = "category"

cat is a prefix substring of category. The containing word is returned.

Example 3

words = ["dog","cat","bird"]return = ""

No input contains another listed input, so return the empty string.

Constraints

  • 1 <= words.length <= 200.
  • 1 <= words[i].length <= 2000.
  • The total length of all input strings is at most 20000.
  • Every string contains only letters a through z; duplicate strings are allowed.

More Meta problems

  • Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
  • Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
  • Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
All Meta interview questionsSee Meta hiring insights
Saved
public String findContainingWord(String[] words) {
    // Write your code here.
}
Checking account…