FastPrepLongest Substring Without Repeating Characters
Problems/Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

Microsoft logoMicrosoft● MediumINTERNFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: Two Taro (Glassdoor copy) reviews of Microsoft software engineering internship interviews both give this as the question in their first technical round: find the longest piece of a string in which no character appears twice.

Neither report gives the characters used, the limits or what to do with an empty string. Here the string holds letters, digits and spaces, upper and lower case count as different characters, the length is 0 to 5 * 10^4, and you return the length of the longest such substring (0 for an empty string).

The judged task matches the reports at about 95%.

Given a string s, return the length of the longest substring in which no character appears more than once.

A substring is a run of characters that are next to each other in s. Upper-case and lower-case letters are different characters, and a space is a character too. The empty string has answer 0.

Function

lengthOfLongestSubstring(s: String) → int

Examples

Example 1

s = "abcabcbb"return = 3

The longest stretches with no repeated character have length 3, for example abc. Any 4 characters in a row contain a repeat.

Example 2

s = "pwwkew"return = 3

wke has 3 different characters. pwke is not a substring: its letters are not next to each other in s. The answer is 3.

Example 3

s = ""return = 0

The empty string has only the empty substring, so the answer is 0.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, and spaces.

More Microsoft problems

  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int lengthOfLongestSubstring(String s) {
        // Write your code here.
    }
}
Checking account…