The Number of Valid Words in a Sentence problem gives you a sentence string. A valid token is a word that has: at most one hyphen (not at the start/end), no digits, and at most one punctuation mark (only at the end). Count valid tokens separated by spaces. This easy coding problem tests careful string token validation.
Cisco, Meta, and Oracle ask this to test systematic string validation logic. Each token must be checked against multiple conditions: no digits, at most one hyphen in a valid position, optional single punctuation at end. The string interview pattern is applied with multiple condition checks.
Token validation. Split the sentence on spaces. For each non-empty token: check if any character is a digit (→ invalid). Count hyphens — if more than 1, or if a hyphen is at position 0 or n-1, invalid. Check punctuation — if any punctuation occurs at a non-last position, or if more than one punctuation exists, invalid. Count valid tokens.
"cat dog1 - abc- -def abc".
Multi-condition validation problems require clear separation of each rule check. Write helper functions: has_digit(token), has_valid_hyphen(token), has_valid_punctuation(token). Testing each rule independently is cleaner than nesting multiple conditions. Practice similar string validation problems (valid email format, valid variable name, valid IP address) — they all follow the "check each rule independently" approach.