Magicsheet logo

Find Words Containing Character

Easy
100%
Updated 6/1/2025

Find Words Containing Character

1. What is this problem about?

The Find Words Containing Character interview question is a fundamental string filtering task. You are given an array of strings (words) and a single character (x). Your task is to return a list of the indices of all words in the array that contain the character x.

2. Why is this asked in interviews?

Companies like Meta and Amazon use this as a very basic screening question. It tests a candidate's familiarity with Array and String iteration. It evaluations if you can correctly traverse a data structure and use built-in string methods (like contains or find) to perform a search. It’s an essential "level zero" coding task.

3. Algorithmic pattern used

This problem follows the Linear Scan pattern.

  1. Iterate: Use a loop to go through the words array from index 0 to n1n-1.
  2. String Search: For each word, check if character x is present.
  3. Collect: If found, add the current index to the result list.
  4. Early Exit: Within a word, once you find character x, you can stop searching that word and move to the next index.

4. Example explanation

words = ["apple", "banana", "cherry"], x = 'a'

  • index 0 ("apple"): contains 'a'. Add 0.
  • index 1 ("banana"): contains 'a'. Add 1.
  • index 2 ("cherry"): does not contain 'a'. Result: [0, 1].

5. Common mistakes candidates make

  • Duplicate Indices: Adding the same index multiple times if the character appears more than once in a single word.
  • Returning Words: Returning the actual strings instead of their indices.
  • Case Sensitivity: Not clarifying if 'A' should match 'a'.

6. Interview preparation tip

Even for simple problems, focus on Code Readability. Use descriptive variable names and clear loop structures. Interviewers use these easy questions to see your "natural" coding style and attention to detail.

Similar Questions