Validate a word by length, allowed characters, vowels and consonants

Quick Overview

Write a function that decides whether a string is a valid word: at least 3 characters, only English letters and digits, and at least one vowel and one consonant. It tests single-pass string scanning, character classification, and the edge case that digits are neither vowels nor consonants.

Validate a word by length, allowed characters, vowels and consonants

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A word is considered valid if it meets all of the following conditions: - It has a minimum length of 3. - It contains only digits (`0`-`9`) and English letters (uppercase and lowercase). - It includes at least one vowel. - It includes at least one consonant. You are given a string `word`. Return `True` if `word` is valid, otherwise return `False`. ### Function Signature ```python def is_valid_word(word: str) -> bool: ``` ### Rules - `'a'`, `'e'`, `'i'`, `'o'`, `'u'` and their uppercase forms are vowels. - A consonant is an English letter that is not a vowel, in either case. `'y'` and `'Y'` are consonants. - Digits are allowed and count toward the length, but they are neither vowels nor consonants. - Any `'@'`, `'#'` or `'$'` makes the word invalid. ### Constraints - `1 <= len(word) <= 20` - `word` consists of English uppercase and lowercase letters, digits, `'@'`, `'#'`, and `'$'`. ### Examples ```text Input: word = "234Adas" Output: True ``` The word has length 7 and every character is a digit or a letter. It contains the vowels `'A'` and `'a'` and the consonants `'d'` and `'s'`. ```text Input: word = "b3" Output: False ``` The word has length 2, which is less than the minimum length of 3. ```text Input: word = "Ae123" Output: False ``` The length is 5 and all characters are allowed, and `'A'` and `'e'` are vowels. The digits do not count as consonants, so the word has no consonant.

Overview: Write a function that decides whether a string is a valid word: at least 3 characters, only English letters and digits, and at least one vowel and one consonant. It tests single-pass string scanning, character classification, and the edge case that digits are neither vowels nor consonants.

Microsoft logo
Microsoft
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A word is considered valid if it meets all of the following conditions:

  • It has a minimum length of 3.
  • It contains only digits ( 0 - 9 ) and English letters (uppercase and lowercase).
  • It includes at least one vowel.
  • It includes at least one consonant.

You are given a string word. Return True if word is valid, otherwise return False.

Function Signature

def is_valid_word(word: str) -> bool:

Rules

  • 'a' , 'e' , 'i' , 'o' , 'u' and their uppercase forms are vowels.
  • A consonant is an English letter that is not a vowel, in either case. 'y' and 'Y' are consonants.
  • Digits are allowed and count toward the length, but they are neither vowels nor consonants.
  • Any '@' , '#' or '$' makes the word invalid.

Constraints

  • 1 <= len(word) <= 20
  • word consists of English uppercase and lowercase letters, digits, '@' , '#' , and '$' .

Examples

Input: word = "234Adas"
Output: True

The word has length 7 and every character is a digit or a letter. It contains the vowels 'A' and 'a' and the consonants 'd' and 's'.

Input: word = "b3"
Output: False

The word has length 2, which is less than the minimum length of 3.

Input: word = "Ae123"
Output: False

The length is 5 and all characters are allowed, and 'A' and 'e' are vowels. The digits do not count as consonants, so the word has no consonant.