Find the Longest Even-Length Word
Walmart LabsSoftware Engineer · Technical ScreenLast updated
Tests string tokenization, linear scanning, tie-breaking logic, and handling of edge cases such as absent matches or irregular whitespace.
Problem Statement
You are given a string sentence containing words separated by spaces. Your task is to find the longest word in the sentence that has an even number of characters.
- If there are multiple even-length words with the same maximum length, return the word that appears first in the sentence.
- If no even-length word exists in the sentence, return
"00".
A word is defined as a maximal sequence of non-space characters.
Function Signature
def longest_even_word(sentence: str) -> str:
pass
Input & Output
- Input:
sentence(a string of lengthncontaining English letters, digits, and spaces). - Output: A string representing the longest even-length word found, or
"00"if none exist.
Constraints
1 <= len(sentence) <= 10^5sentencemay contain leading, trailing, or consecutive spaces.- Words consist of alphanumeric characters.
Examples
Example 1
Input: sentence = "Time to write great code"
Output: "Time"
Explanation:
- "Time": length 4 (even)
- "to": length 2 (even)
- "write": length 5 (odd)
- "great": length 5 (odd)
- "code": length 4 (even)
Both "Time" and "code" have the maximum even length of 4. We return "Time" because it appears first.
Example 2
Input: sentence = "It is a pleasant day today"
Output: "pleasant"
Explanation:
- "It": length 2 (even)
- "is": length 2 (even)
- "a": length 1 (odd)
- "pleasant": length 8 (even)
- "day": length 3 (odd)
- "today": length 5 (odd)
The longest even-length word is "pleasant" with length 8.
Example 3
Input: sentence = "You can do it"
Output: "do"
Explanation:
"do" (length 2) and "it" (length 2) are even. "do" appears first.
Example 4
Input: sentence = "The red fox jumps"
Output: "00"
Explanation:
Lengths are 3, 3, 3, 5. None are even, so return "00".
Edge Cases to Consider
- Sentences with multiple spaces between words or leading/trailing whitespace.
- Sentences where every word has an odd length.
- Sentences consisting only of whitespace characters.
- Sentences with only a single word.
Hints
Hint 1
Tokenize the sentence by splitting on whitespace to handle irregular spacing cleanly.
Hint 2
Track both the longest even word found so far and its length; update them only when you encounter a word with an even length strictly greater than the current maximum.
Hint 3
Initialize the result to "00" so you can return it directly if no even-length word is found.
Follow-up questions
- How would you optimize this to run in O(1) auxiliary space without creating an intermediate list of words?
- How should your function handle attached punctuation marks (such as commas or periods) without counting them toward word length?
- If the input were a stream of characters too large to fit in memory, how would you adapt your parsing logic?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.