Skip to main content

3. Longest Substring Without Repeating Characters

mediumAsked at Pinterest

Pinterest's go-to sliding-window question: given a string, find the longest substring with no repeating characters. The interviewer wants the variable-window two-pointer pattern with a hash map of character-to-most-recent-index.

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

Source citations

Public interview reports confirming this problem appears in Pinterest loops.

  • Glassdoor (2026-Q1)Pinterest SWE phone-screen reports cite Longest Substring Without Repeating as the sliding-window round.
  • LeetCode Pinterest tag (2026-Q1)On the Pinterest company-tagged problem list.

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

Explanation: The answer is 'wke', with the length of 3. Note that 'pwke' is a subsequence, not a substring.

Approaches

1. Brute force: check every substring

Try every (i, j) pair, verify the substring has no duplicates by walking it, track max length.

Time
O(n^3)
Space
O(min(n, alphabet))
function lengthOfLongestSubstringBrute(s) {
  let best = 0;
  for (let i = 0; i < s.length; i++) {
    for (let j = i; j < s.length; j++) {
      const set = new Set();
      let ok = true;
      for (let k = i; k <= j; k++) {
        if (set.has(s[k])) { ok = false; break; }
        set.add(s[k]);
      }
      if (ok) best = Math.max(best, j - i + 1);
    }
  }
  return best;
}

Tradeoff: Cubic; times out at the 50k constraint. Anchor with this, then move to sliding window.

2. Sliding window with last-seen index map (optimal)

Maintain two pointers (left, right). Map character -> most-recent index. When we see a duplicate inside the current window, jump left past it.

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

Tradeoff: O(n) single pass. The 'jump left past the duplicate' move is the key optimization vs the naive Set-shrink-from-front sliding window. Each character is visited at most twice (once by right, once by the implicit left jump).

Pinterest-specific tips

Pinterest interviewers want you to volunteer the 'jump left past the duplicate' pattern, NOT the slower 'shrink left one at a time while there's a duplicate' version. Both are O(n) amortized but the jumping version is constant-time per right-step and matches what most senior engineers would write. Narrate the invariant: 'the window always contains distinct characters.'

Common mistakes

  • Forgetting to check lastSeen.get(ch) >= left — without this, an old occurrence outside the window incorrectly resets left.
  • Shrinking left one-at-a-time while there's a duplicate — works but is the less-elegant version.
  • Using a Set instead of a Map — you lose the last-seen-index needed for the jump.
  • Returning the substring instead of the length.

Follow-up questions

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

  • Return the substring itself, not just the length.
  • Generalize to 'at most K distinct characters' (LeetCode 340).
  • Longest substring with at most two distinct characters (LeetCode 159).
  • Stream: maintain the current-window distinct-character count online.

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 does Pinterest ask this if it's a 5-year-old problem?

Sliding window is foundational for problems involving 'longest / shortest contiguous subarray meeting a constraint.' Mastering this unlocks dozens of follow-ups Pinterest pivots to.

Is the Set-based version acceptable?

Yes for correctness. But if asked 'can you do it more elegantly?' the map-based version is the expected upgrade. Lead with the map version to avoid the follow-up.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →