Longest Unique Substring Range
Updated
Problem statement
You are given a nonempty string text. Return [start, end], the zero-based inclusive indices of a longest contiguous substring whose characters are all distinct.
If several longest valid substrings exist, return the one with the smallest start index.
Process the string from left to right in O(text.length) time and use O(1) auxiliary space for the fixed printable-ASCII alphabet.
Follow-up
A sliding-window solution works. How should the approach change when the input is too large to keep in memory?
Function
longestUniqueSubstringRange(text: String) → int[]Examples
Example 1
text = "abcabcbb"return = [0,2]The maximum length is 3. The valid ranges [0,2], [1,3], [2,4], and [3,5] all have that length, so the earliest range is returned.
Example 2
text = "bbbbb"return = [0,0]Every valid substring contains one b. The earliest one begins and ends at index 0.
Example 3
text = "pwwkew"return = [2,4]Both wke at [2,4] and kew at [3,5] have maximum length 3. The earlier range is returned.
Example 4
text = "dvdf"return = [1,3]After the second d, the window moves forward and the substring vdf becomes the unique maximum.
Constraints
1 <= text.length <= 10^6.textcontains printable ASCII characters.- The returned range uses zero-based inclusive indices.
- When several ranges have the same maximum length, return the earliest one.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Top K Best-Selling Products in the Last N DaysAmazon · ONSITE INTERVIEW
- Longest Substring Without Repeating CharactersInfosys · ONSITE INTERVIEWInfosys interview questions
- Longest Substring Without Repeating CharactersOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Longest Substring Without Repeating CharactersBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Longest Substring with At Most K Distinct CharactersGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Longest Substring Without Repeating CharactersBank of America · ONSITE INTERVIEWBank of America interview questions
- Sliding-Window Rate LimiterStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions