Count Valid Space-Delimited Words
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Visa reported this one in October 2026, and the input size is the first thing to read: text can run up to 100000 characters. That rules out anything clever with regex backtracking or repeated string rebuilding. It's a string parsing problem. Split on spaces, skip empties, check each token against a tiny rule set, count the survivors. Nothing exotic, but the rules are easy to fumble when you're rushing. If you blank on the edge cases during the live OA, StealthCoder runs invisibly on your desktop and hands you a working solution as a safety net. Know the shape of it before you sit down.
The problem
Split text on one or more ASCII space characters and ignore empty pieces. Return the number of valid tokens. A valid token contains one or more ASCII letters and may optionally end with exactly one of ,., ?, or !. Digits are never allowed, and punctuation may not appear anywhere except the final character. Function countValidWords(text: String) → int Examples Example 1 text = "hello world! 123 no-way ok?" return = 3 hello, world!, and ok? are valid; the numeric and hyphenated tokens are not. Example 2 text = " A, b. wow!! fine? " return = 3 Repeated spaces are ignored. wow!! is invalid because it has more than one punctuation mark. Constraints 0 <= text.length <= 100000. text contains printable ASCII characters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single pass per token. Split on spaces and drop empty pieces. For each token, look at the last character. If it's one of comma, period, question mark or exclamation, strip it and keep the rest as the body. Then the body must be non-empty and every character must be an ASCII letter. That one rule covers digits, hyphens, and double punctuation like wow!! because the leftover exclamation mark fails the letter check. The classic pitfall is a token that is only punctuation, like a lone period. After stripping, the body is empty, so it's invalid. Also watch an empty input string, which returns 0. Total work is linear in text length, so 100000 characters is trivial. Use isalpha carefully, since you only want ASCII letters. StealthCoder is the hedge if the live OA rattles you and you miss one of these cases.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Valid Space-Delimited Words 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of valid words in a sentence. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Visa's OA.
Visa reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Valid Space-Delimited Words FAQ
How hard is the Visa Count Valid Space-Delimited Words question really?+
Easy to medium. There's no fancy algorithm. The difficulty is handling edge cases cleanly: empty body after stripping punctuation, double punctuation, digits, and hyphens. If you write the rule check carefully, it passes in one linear scan.
What's the trick to getting it right?+
Strip at most one trailing punctuation character, then require the remaining body to be non-empty and all letters. That single rule rejects wow!!, no-way, 123, and a lone period without special-casing each one.
Do I need regex for this?+
No. A regex like ^[A-Za-z]+[,.?!]?$ works, but a manual check is just as short and avoids surprises. With 100000 characters, either approach is fine as long as you don't build strings repeatedly in a loop.
What edge cases should I test before submitting?+
Test an empty string, a string of only spaces, a token that is only punctuation, a token with punctuation in the middle like a,b, and double punctuation like wow!!. Also test leading and trailing spaces, as in Example 2.
How do I prepare for this in 48 hours?+
Write the solution once from scratch in your language, then run both examples by hand. Practice similar string validation tasks that split on whitespace and check character classes. Focus on clean rules over clever tricks, since speed on edge cases decides this one.