Find max word letter span
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string processing, character mapping, and algorithmic analysis by requiring computation of per-word letter spans while accounting for case normalization, punctuation handling, ties, duplicates, and edge-case word forms.
Constraints
- 0 <= len(text) <= 10^5
- Words are separated by whitespace.
- Only ASCII letters a-z / A-Z count toward a word's first/last letter; all other characters are ignored when computing the span.
- Comparison is case-insensitive ('A' and 'a' map to the same position 0).
- If no word contains an alphabetic character, return [0, []].
Examples
Input: ("cab pat Float",)
Expected Output: [14, ["Float"]]
Explanation: Spans: cab=1, pat=4, Float=14 ('f'=5 to 't'=19). Maximum is 14, achieved only by Float.
Input: ("wood would",)
Expected Output: [19, ["wood", "would"]]
Explanation: Both go 'w'(22) to 'd'(3), span abs(22-3)=19. They tie, so both surface forms are returned in order.
Input: ("can't cannot. pat",)
Expected Output: [17, ["can't", "cannot."]]
Explanation: Ignoring the apostrophe and period, both words run 'c'(2) to 't'(19), span 17. pat's span is only 4. Tie -> both original forms returned.
Input: ("I pop",)
Expected Output: [0, ["I", "pop"]]
Explanation: 'I' has the same first and last letter (span 0); pop goes 'p' to 'p' (span 0). They tie at the maximum span of 0.
Input: ("",)
Expected Output: [0, []]
Explanation: Empty text has no words; return span 0 and an empty list.
Input: ("123 !!! ...",)
Expected Output: [0, []]
Explanation: No token contains an alphabetic character, so nothing participates; return [0, []].
Input: ("PAT TAP cab",)
Expected Output: [4, ["PAT", "TAP"]]
Explanation: Because the span uses absolute value, PAT ('p'->'t') and TAP ('t'->'p') both have span 4 regardless of letter order; cab is 1. Both tie at the max.
Hints
- Split the text on whitespace, then for each token find its first and last alphabetic character, ignoring everything else.
- Map a letter to a number with ord(ch.lower()) - ord('a'); the span is abs(first - last), so order and case never matter.
- Keep a running maximum: start a fresh result list when you see a strictly larger span, and append to it on a tie. Skip tokens that contain no letters so all-punctuation/numeric tokens never affect the answer.