Skip to main content

3. Longest Substring Without Repeating Characters

mediumAsked at GoDaddy

Find the length of the longest substring with all unique characters — GoDaddy applies this sliding-window pattern when validating domain labels and parsing URL path segments where repeated characters signal malformed input.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a string s, find the length of the longest substring that contains no repeating characters.

Constraints

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

Examples

Example 1

Input
s = "abcabcbb"
Output
3

Explanation: The substring "abc" has length 3 with no repeating characters.

Example 2

Input
s = "bbbbb"
Output
1

Example 3

Input
s = "pwwkew"
Output
3

Approaches

1. Brute force

Check every substring for uniqueness using a Set.

Time
O(n^3)
Space
O(min(n, m))
function lengthOfLongestSubstring(s) {
  let max = 0;
  for (let i = 0; i < s.length; i++) {
    const seen = new Set();
    for (let j = i; j < s.length; j++) {
      if (seen.has(s[j])) break;
      seen.add(s[j]);
      max = Math.max(max, j - i + 1);
    }
  }
  return max;
}

Tradeoff:

2. Sliding window with hash map

Maintain a window [left, right]; advance left past any duplicate using the stored index of the last-seen character.

Time
O(n)
Space
O(min(n, m))
function lengthOfLongestSubstring(s) {
  const lastIndex = new Map();
  let max = 0;
  let left = 0;
  for (let right = 0; right < s.length; right++) {
    const ch = s[right];
    if (lastIndex.has(ch) && lastIndex.get(ch) >= left) {
      left = lastIndex.get(ch) + 1;
    }
    lastIndex.set(ch, right);
    max = Math.max(max, right - left + 1);
  }
  return max;
}

Tradeoff:

GoDaddy-specific tips

GoDaddy interviewers reward candidates who immediately name the sliding-window pattern and explain the index-jump optimization — it mirrors how their URL-parsing pipelines shrink the inspection window on duplicate path tokens without rescanning from scratch.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Longest Substring Without Repeating Characters and other GoDaddy interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →