Longest Substring Without Repeating Characters
Updated
Problem statement
Given a string s, return the length of the longest consecutive part that contains no repeated character.
If s is empty, return 0.
Interview extension
After you solve the task above, explain how to return one longest valid substring itself by tracking the start index whenever the maximum length improves.
Function
lengthOfLongestSubstring(s: String) → intExamples
Example 1
s = "abcabcbb"return = 3"abc" is one longest substring whose characters are all distinct.
Example 2
s = "bbbbb"return = 1Every valid substring contains only one b, so the maximum length is 1.
Example 3
s = ""return = 0An empty string has no nonempty substring, so the result is 0.
Constraints
0 <= s.length <= 100000scontains printable ASCII characters.- The solution should run in
O(s.length)time.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 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
- 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
- Longest Substring with At Most K Distinct CharactersAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Longest Substring Without Repeating CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Longest Substring Without Repeating CharactersAmazon · 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