Skip to main content

32. Longest Substring Without Repeating Characters

mediumAsked at Snowflake

Find the length of the longest substring with all distinct characters. Snowflake uses this to test sliding-window mastery — the same technique their executor uses for running-distinct window functions.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2026-Q1)Snowflake execution-engine team uses this to lead into window-function discussion.
  • LeetCode Discuss (2025-12)Recurring at Snowflake new-grad onsites.

Problem

Given a string s, find the length of the longest substring without 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 answer is "abc", with the length of 3.

Example 2

Input
s = "bbbbb"
Output
1

Example 3

Input
s = "pwwkew"
Output
3

Approaches

1. Brute force every substring

For every (i, j), build a set; check uniqueness.

Time
O(n^3)
Space
O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
  let best = 0;
  for (let i = 0; i < s.length; i++) {
    for (let j = i; j < s.length; j++) {
      const set = new Set(s.slice(i, j + 1));
      if (set.size === j - i + 1) best = Math.max(best, j - i + 1);
    }
  }
  return best;
}

Tradeoff: Cubic. Mention only as the worst case.

2. Sliding window with hash map (optimal)

Right pointer extends the window. When a duplicate appears, jump left to one past the previous occurrence.

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

Tradeoff: Linear with each character visited at most twice. The map stores last-seen index to enable the O(1) jump.

Snowflake-specific tips

Snowflake interviewers grade this on whether the left pointer jumps (not increments) on a duplicate — and whether you guard against stale map entries from outside the current window. Bonus signal: pivot to window functions — Snowflake's COUNT(DISTINCT) over a moving window uses exactly this technique to maintain the distinct set incrementally.

Common mistakes

  • Incrementing left one at a time when you could jump (O(n^2) worst case).
  • Forgetting to check lastSeen.get(c) >= left — stale entries shouldn't move left backward.
  • Updating lastSeen before computing left — order matters.

Follow-up questions

An interviewer at Snowflake may pivot to one of these next:

  • Longest Substring with At Most K Distinct Characters (LC 340).
  • Longest Substring with At Most Two Distinct Characters (LC 159).
  • How does Snowflake implement moving-window aggregations?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why the lastSeen.get(c) >= left guard?

Without it, a duplicate from outside the current window would erroneously pull left backward. The guard ensures we only react to duplicates inside the window.

How does this connect to window functions?

ROWS BETWEEN N PRECEDING AND CURRENT ROW with DISTINCT semantics uses incremental insert + remove into a multiset. The longest-distinct-window question is the simplified version: just track the boundary, not all elements.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →