KatInterview

First Longest Word with Even Character Count

StringsAuto-graded · 22 test cases
EasyAuto-gradedStringsstring-manipulationlinear-scanedge-cases

Last updated

Tests string parsing, filtering by character parity, tracking maxima with tie-breaking, and handling whitespace edge cases.

You're given a string text made up of words separated by spaces. Find the word with the greatest number of characters where that count is an even number.

When multiple words share the maximum even length, return whichever appears earliest in the input. If the input contains no words with an even character count, return the string "00".

A word is any contiguous sequence of non-space characters.

Input

  • text: a string containing letters, digits, and spaces

Output

  • A string: either the qualifying word, or "00" if none exists

Constraints

  • 1 <= len(text) <= 10^5
  • The input may have leading spaces, trailing spaces, or multiple consecutive spaces
  • Words are composed of alphanumeric characters

Examples

Example 1

Input: first_longest_even_word('Time to write great code')

Output: 'Time'

Example 2

Input: first_longest_even_word('It is a pleasant day today')

Output: 'pleasant'

Example 3

Input: first_longest_even_word('You can do it')

Output: 'do'

Example 4

Input: first_longest_even_word('The red fox jumps')

Output: '00'

Hints

Hint 1

Split the input into tokens and filter for those with even lengths while tracking the longest one encountered so far.

Hint 2

Use a sentinel value to handle the case where no even-length word exists, avoiding special-case logic at the end.

Hint 3

When two words have the same even length, the first one wins—so only replace your current best when a new word is strictly longer, not equal.