Reported January 2019
Mygatestring

Count Vowels and Consonants

Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Mygate OA. Under 2s to a working solution.
Founder's read

Mygate reported this one in January 2019, and it's a single-pass counting problem dressed up as an OA. Strip the wording and it reduces to this: walk the string once, decide if each character is a letter, then decide if it's a vowel. That's it. If you've got an invite for the next day or two, the risk isn't difficulty, it's sloppy edge cases like uppercase, y, and punctuation. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but you probably won't need it here. Know the filter, know the vowel set, return the pair.

The problem

Given an ASCII string s, return [vowels, consonants], the counts of vowels and consonants among its English letters.
Treat uppercase and lowercase letters alike. The vowels are a, e, i, o, and u. Every other English letter, including y, is a consonant. Ignore digits, punctuation, and whitespace.
An empty string or a string without letters produces [0,0].

Function
countVowelsConsonants(s: String) → int[]

Examples
Example 1
s = "Hello, World!"
return = [3,7]
The vowels are e, o, and o. The other seven letters are consonants; punctuation and the space are ignored.
Example 2
s = "AEIOUaeiou 123!"
return = [10,0]
Both cases of all five vowels count; digits and punctuation do not.

Constraints
0 ≤ s.length ≤ 100,000.
s contains ASCII characters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that there's no trick. Loop through s once, lowercase each character, and check whether it falls between 'a' and 'z'. If it doesn't, skip it. If it does, check membership in the set {a, e, i, o, u}. Vowel goes to one counter, everything else goes to the other. Time is O(n), space is O(1), and n tops out at 100,000, so nothing fancy is needed. The pitfalls are all small. Don't use a general isalpha on non-ASCII input if your language treats it loosely. Don't count y as a vowel. Don't count digits, spaces, or punctuation as consonants, which is the classic mistake of computing consonants as length minus vowels. Return [0,0] for an empty string, which falls out naturally. If you freeze on the input handling, StealthCoder can hand you the clean loop live, but this is a five-minute problem if you stay calm.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Count Vowels and Consonants cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Mygate's OA.

Mygate reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Vowels and Consonants FAQ

How hard is the Mygate Count Vowels and Consonants question really?+

Easy. It's a linear scan with a character check. The only way to lose points is missing an edge case like uppercase letters, treating y as a vowel, or counting punctuation and digits as consonants. Write it carefully and test the two samples by hand.

What's the trick to this problem?+

Filter first, classify second. Only characters from a to z (after lowercasing) count at all. Among those, five are vowels and the rest are consonants. Never derive consonants as total length minus vowels, because that wrongly includes digits, spaces, and punctuation.

Is y a vowel here?+

No. The problem says every English letter other than a, e, i, o, and u is a consonant, and it explicitly names y as a consonant. Hardcode the five-vowel set and let everything else alphabetic fall into the consonant count.

What should I return for an empty string or no letters?+

Return [0,0]. If you initialize both counters at zero and only increment them for alphabetic characters, this works automatically without a special case. Still, run your code mentally against an empty input and a string like "123!" before submitting.

How do I prepare for this in 48 hours?+

Don't over-prep this one. Practice writing a clean single-pass loop with a set lookup, then spend your time on harder string and hash map problems. Test your solution against both examples, including the mixed-case one, and check that you handle the 100,000 length without extra allocations.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Mygate.

OA at Mygate?
Invisible during screen share
Get it