Count Vowels
Arguments
str: string —The input string to analyze.
Returns
number — Solution idea
Scan the string once, normalize case, and count only characters that belong to a vowel set. This keeps the solution O(n), handles empty input gracefully, and avoids tricky regex edge cases. It’s a classic example of membership checks with a Set. Key concepts: strings, iteration, counting.
// Basic examples
countVowels('hello') // => 2
countVowels('rhythm') // => 0
countVowels('AEIOU') // => 5
// Mixed case and spaces
countVowels('A quick brown fox') // => 5Public interview reports
These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.
- Taro: Datadog senior frontend developer interview, 2024
One senior account lists a prompt to count the repeated words in a paragraph. This counting drill is a simpler relative of that prompt, not the reported exercise.
Reported in a public Datadog interview account. Datadog frontend interview questions
Solution
Overview
Scan the string once, normalize case, and count only characters that belong to a vowel set. This keeps the solution O(n), handles empty input gracefully, and avoids tricky regex edge cases. It’s a classic example of membership checks with a Set. Key concepts: strings, iteration, counting.
Approach 1: Iterative counting (explicit and clear)
Steps:
1) Create a Set of vowels (e.g., a/e/i/o/u).
2) Convert the input to lowercase to make matching case-insensitive.
3) Initialize a counter at 0 and loop through each character.
4) If the character is in the vowel Set, increment the counter.
5) Return the counter (0 for empty strings).
Complexity: O(n) time, O(1) space for the fixed vowel set.
export default function countVowels(str) {
const vowels = 'aeiou';
let count = 0;
for (const ch of str.toLowerCase()) {
if (vowels.includes(ch)) count++;
}
return count;
}
export default function countVowels(str: string): number {
const vowels = 'aeiou';
let count = 0;
for (const ch of str.toLowerCase()) {
if (vowels.includes(ch)) count++;
}
return count;
}
Approach 2: Regular expression (concise alternative)
Idea: use a regular expression to directly match all vowels.
We can write: (str.match(/[aeiou]/gi) || []).length.
Read it as: *find all vowels (case-insensitive) and count them*. The /g flag finds all matches, and /i makes it case-insensitive.
Why use it: shorter and declarative, though less explicit than manual iteration. Good as a follow-up solution when you're comfortable with regex.
export default function countVowels(str) {
return (str.match(/[aeiou]/gi) || []).length;
}
export default function countVowels(str: string): number {
return (str.match(/[aeiou]/gi) || []).length;
}
Notes & Pitfalls
Empty strings should return 0.Regex approach may be less intuitive for beginners. Non-letter characters (spaces, punctuation) are simply skipped. yis not treated as a vowel here.
No vowels → returns 0.Mixed case → handled by toLowerCase()or the/iflag.Accented vowels (é, ü) → not counted unless explicitly included in the vowel set or regex.
String traversal and membership checking. Regex matching and counting via array length.
Common mistakes on this challenge
Vowel check is missing case normalization
Uppercase and mixed-case inputs should count the same vowels. Normalize input once ( toLowerCase) before scanning.Count only a,e,i,o,u; ignore consonants, spaces, and punctuation.Confirm with AEIOUandA quick brown foxbefore full rerun.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.