Quick Overview

Implement `longest_even_word(sentence)`. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

Find the Longest Even-Length Word

Company: Walmart Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

# Find the Longest Even-Length Word Implement `longest_even_word(sentence)`. Words are separated by one or more spaces. Return the first word among those having the greatest positive even length. Return `"00"` when `sentence` is empty, contains only spaces, or contains no even-length word. The input is always a non-null string in every language. Examples: - `"Time to write great code"` returns `"Time"`. - `"a cat sun"` returns `"00"`. Constraints: at most `200000` characters; words contain English letters. Process the sentence in one pass over its words. ```hint Preserve first-on-tie behavior Update the answer only when a strictly longer eligible word appears. ```

Quick Answer: Implement `longest_even_word(sentence)`. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

Words in a non-null sentence are separated by one or more spaces. Return the first word among those with the greatest positive even length. Return `00` when the sentence is empty, contains only spaces, or contains no even-length word. Words contain only English letters.

Constraints

  • 0 <= len(sentence) <= 200000; sentence is non-null.
  • Words contain English letters and are separated by one or more spaces.
  • Only positive even word lengths qualify; equal maximum lengths return the first word, and no qualifying word returns 00.

Examples

Input: ('',)

Expected Output: '00'

Explanation: An empty sentence returns the sentinel.

Input: (' ',)

Expected Output: '00'

Explanation: A sentence containing only spaces returns the sentinel.

Hints

  1. Test empty and spaces-only sentences, one word, and a sentence with no even-length word.
  2. Include several equal-length eligible words and verify the first one is returned.
  3. Use leading, trailing, and repeated spaces together with a later strictly longer eligible word.

Loading coding console...