Magicsheet logo

String Matching in an Array

Easy
25%
Updated 8/1/2025

String Matching in an Array

What is this problem about?

The String Matching in an Array problem is an introductory string manipulation task. Given an array of strings, the goal is to find all strings that are a substring of at least one other string in the same array. A substring is a contiguous sequence of characters within a string. The output should be a list of these matching strings, and the order of the result usually doesn't matter. For example, in the array ["mass", "as", "hero", "superhero"], the strings "as" and "hero" are substrings of "mass" and "superhero" respectively.

Why is this asked in interviews?

Companies like Microsoft and Meta often include this in their interview process for entry-level positions or as a warm-up question. It evaluates a candidate's familiarity with string search methods and nested loops. While the brute-force approach is often acceptable for smaller constraints, it allows the interviewer to discuss more advanced string-matching algorithms like KMP (Knuth-Morris-Pratt) or Aho-Corasick for larger datasets. It's a test of basic logic and the ability to use built-in language functions effectively.

Algorithmic pattern used

The most common approach for this interview question is a Brute Force search using Nested Loops. You compare every string in the array with every other string to check if one is contained within the other. For an array of size N, this results in an O(N^2 * M^2) complexity where M is the average length of the strings, assuming string searching takes O(M^2) or O(M) depending on the language implementation. Alternatively, sorting the strings by length first can slightly optimize the search since a longer string cannot be a substring of a shorter one.

Example explanation (use your own example)

Imagine an array of words: ["blue", "blueprint", "red", "reddish", "print"].

  1. We check "blue": It is found inside "blueprint". So, "blue" is a match.
  2. We check "blueprint": No other string contains "blueprint".
  3. We check "red": It is found inside "reddish". So, "red" is a match.
  4. We check "reddish": No other string contains "reddish".
  5. We check "print": It is found inside "blueprint". So, "print" is a match. The final result set would be ["blue", "red", "print"].

Common mistakes candidates make

One mistake is including the same string multiple times in the result if it is a substring of more than one other string. Most problems expect unique results. Another common pitfall is comparing a string with itself, which would incorrectly mark every string as a match. Candidates also sometimes forget to handle case sensitivity, although most coding challenges specify that all characters are lowercase. Using inefficient string concatenation in a loop can also be a minor performance concern.

Interview preparation tip

To prepare for this type of string matching interview pattern, ensure you are comfortable with your language's built-in "contains" or "indexOf" methods. For optimization, consider how sorting the array by the length of strings can reduce the number of comparisons. If you want to impress the interviewer, be ready to discuss how a Trie (prefix tree) could be used to solve this more efficiently if the number of strings was very large.

Similar Questions