Magicsheet logo

Maximum Number of Words Found in Sentences

Easy
12.5%
Updated 8/1/2025

Maximum Number of Words Found in Sentences

What is this problem about?

The Maximum Number of Words Found in Sentences coding problem gives you an array of strings, where each string is a single sentence. You need to find the maximum number of words that appear in any single sentence within the array.

Why is this asked in interviews?

Microsoft, Amazon, Google, and Bloomberg use this as an easy problem to test fundamental string manipulation and array iteration skills. It's a quick way to gauge a candidate's understanding of basic data processing and attention to detail.

Algorithmic pattern used

Iteration and String Splitting: The most straightforward approach is to iterate through each sentence in the input array. For each sentence, split it into words (typically by spaces) and count the number of resulting words. Keep track of the maximum count found across all sentences.

  1. Initialize max_words = 0.
  2. For each sentence in the input array: a. Split the sentence into a list of words using a space delimiter. b. Get the count of words in the list. c. Update max_words = max(max_words, count).
  3. Return max_words.

Most programming languages provide a built-in function to split a string by a delimiter, which simplifies implementation.

Example explanation

sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]

  1. Sentence 1: "alice and bob love leetcode"

    • Split: ["alice", "and", "bob", "love", "leetcode"]
    • Count: 5 words.
    • max_words = 5.
  2. Sentence 2: "i think so too"

    • Split: ["i", "think", "so", "too"]
    • Count: 4 words.
    • max_words = max(5, 4) = 5.
  3. Sentence 3: "this is great thanks very much"

    • Split: ["this", "is", "great", "thanks", "very", "much"]
    • Count: 6 words.
    • max_words = max(5, 6) = 6.

Result: 6.

Common mistakes candidates make

  • Incorrectly handling multiple spaces: If sentences can have multiple spaces between words, simply splitting by " " might result in empty strings in the word list, which should not be counted. Using a split() function without arguments (in Python) or a regex split(" +") (in Java/JavaScript) often handles this gracefully.
  • Off-by-one errors: Ensuring the count is accurate.
  • Edge cases: Empty sentences or an empty array of sentences.

Interview preparation tip

For the Array String interview pattern, this problem emphasizes clear, concise code for basic data processing. Familiarize yourself with common string utility functions in your chosen language.

Similar Questions