← 返回 walmartlabs 的题目列表Longest Even-Length Word
类型:qbank
Given a space-delimited English sentence, return the longest even-length word; on a length tie, return the earliest one. Return "00" when the input is null or empty, or when no even-length word exists.
Requirements
Accept a sentence made of English words separated by spaces.
Return the longest word whose length is even.
If several qualifying words have the same maximum length, return the first one in the sentence.
Return "00" when the sentence is null, empty, or contains no even-length word.
Examples
Input: "Time to write great code"
Output: "Time"
Notes
Scan the words from left to right while tracking the best qualifying word and its length separately from the "00" sentinel. Update the result only when an even-length word is strictly longer; using >= would incorrectly replace the earliest word on a tie.
The scan is linear in the total number of input characters. A streaming tokenizer needs constant auxiliary state apart from the returned word, while a split-based implementation also stores the token list.
Preparation
Implement the scan from a blank file and keep the sentinel separate from the best length so a two-character first match is not skipped.
Build a test table covering null input, an empty sentence, no even-length word, a tie between equal-length words, and a qualifying final word.
Trace the example once with both > and >= update conditions and explain why only the strict comparison preserves the earliest tie winner.