Magicsheet logo

Valid Word

Easy
95.2%
Updated 6/1/2025

Valid Word

What is this problem about?

The "Valid Word" interview question is a string validation task where you are given a word and must check if it meets several criteria to be considered "valid." Typical constraints include a minimum length (e.g., at least 3 characters), containing only alphanumeric characters, and having at least one vowel and at least one consonant.

Why is this asked in interviews?

Companies like Microsoft and Amazon use the "Valid Word" coding problem in early interview stages to verify basic string manipulation skills. It tests your ability to iterate through a string, handle case sensitivity, and manage multiple boolean flags or conditions efficiently. It is a straightforward test of clean coding practices.

Algorithmic pattern used

The primary pattern is a "Linear String Scan." You iterate through each character of the word exactly once. During this scan, you check if each character is a digit, a vowel, or a consonant. You maintain boolean flags like hasVowel and hasConsonant and ensure no invalid characters (like symbols or spaces) are encountered.

Example explanation

Word: "b3App"

  1. Length check: 5 characters (>= 3). OK.
  2. Character 'b': Consonant. hasConsonant = true.
  3. Character '3': Digit. Alphanumeric check OK.
  4. Character 'A': Vowel. hasVowel = true.
  5. Character 'p': Consonant.
  6. Character 'p': Consonant.
  7. Final check: hasVowel and hasConsonant are both true, and no invalid characters were found.
  8. Result: Valid word.

Common mistakes candidates make

A common mistake is not handling both uppercase and lowercase letters when checking for vowels. Another is failing to verify that the word contains only alphanumeric characters, often forgetting to return false immediately if a special character like @ or ! is found. Candidates also sometimes misclassify 'y' or digits as vowels/consonants.

Interview preparation tip

When solving the "Valid Word" coding problem, define a set or a string containing all vowels ("aeiouAEIOU") for quick lookups. This is cleaner than writing a long if statement with ten different OR conditions and makes your code more readable.

Similar Questions