Magicsheet logo

Count Vowel Strings in Ranges

Medium
30%
Updated 6/1/2025

Count Vowel Strings in Ranges

What is this problem about?

The Count Vowel Strings in Ranges interview question gives you an array of strings and several range queries. A string is considered a "vowel string" if it both starts and ends with a vowel (a, e, i, o, u). For each query [left, right], you must count how many vowel strings exist in the array between those indices (inclusive).

Why is this asked in interviews?

Microsoft and PayPal ask the Count Vowel Strings in Ranges coding problem to test a candidate's ability to handle multiple queries efficiently. A brute-force approach (looping through the range for every query) results in O(n * q) time complexity, which is unacceptable for large datasets. It assesses your knowledge of the Prefix Sum technique, which is a fundamental tool for O(1) range query processing.

Algorithmic pattern used

The primary interview pattern here is the Prefix Sum.

  1. Preprocessing: Create a boolean array (or integer array) where isVowel[i] is 1 if the ithi^{th} string starts and ends with a vowel, and 0 otherwise.
  2. Cumulative Sum: Build a prefix sum array P where P[i] is the total count of vowel strings from index 0 to i-1.
  3. Query Handling: For any query [L, R], the answer is simply P[R+1] - P[L].

Example explanation

Words: ["aba", "bcb", "ece", "aa", "e"], Query: [0, 2]

  1. Check vowel property:
    • "aba": starts 'a', ends 'a'. Yes (1).
    • "bcb": starts 'b', ends 'b'. No (0).
    • "ece": starts 'e', ends 'e'. Yes (1).
    • "aa": Yes (1).
    • "e": Yes (1).
  2. Binary Array: [1, 0, 1, 1, 1]
  3. Prefix Sum: [0, 1, 1, 2, 3, 4]
  4. Query [0, 2]: P[3] - P[0] = 2 - 0 = 2. The strings are "aba" and "ece".

Common mistakes candidates make

  • Redundant Calculation: Iterating through the array for every query.
  • Off-by-one errors: Incorrectly indexing the prefix sum array, leading to missing the first or last element of a range.
  • Definition of Vowels: Forgetting to check both the start and the end of the string.

Interview preparation tip

Whenever you encounter a problem involving "Sum of property in range [L, R]" with many queries, think of Prefix Sums immediately. It reduces time complexity from O(N) per query to O(1) after O(N) preprocessing.

Similar Questions