As reported: Oracle Senior Application Engineer (Java) walk-in drive, Gandhinagar, round 2, May 2025 — in a string, find the word with the maximum distance between its first and last occurrence; for
"Hello Param Welcome to Oracle Param is from Ahmedabad Param is Java Developer"the answer is"Param".
Given a sentence s of words separated by single spaces, find the word whose first and last occurrences are farthest apart. Distance is measured in word positions: the index of the last occurrence minus the index of the first.
Example 1
s = "Hello Param Welcome to Oracle Param is from Ahmedabad Param is Java Developer"
Output: "Param"
Param is at positions 1 and 9 (distance 8); is is at 6 and 10 (distance 4).
Example 2
s = "one two three"
Output: ""
Example 3
s = "a b a b"
Output: "a"
Constraints: 1 ≤ number of words ≤ 10^5; words are non-empty and made of English letters; no leading or trailing spaces.
Code runner format: one JSON value per line — s. Implement maxSpanWord(s); the harness prints the word as a JSON string.
Clarifications
- Words are compared case-sensitively (
Javaandjavaare different words). - If several words share the largest distance, return the one whose first occurrence comes first.
- If no word appears twice, return the empty string.