Skip to main content

3. Longest Substring Without Repeating Characters

mediumAsked at Juniper Networks

Find the length of the longest substring with all unique characters using a sliding window. Juniper asks this because sliding-window packet inspection — maintaining a window of unique protocol tokens in a byte stream without rescanning — is a real pattern in deep-packet-inspection engines.

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

Source citations

Public interview reports confirming this problem appears in Juniper Networks loops.

  • Glassdoor (2026-Q1)Frequently cited in Juniper SWE onsite and phone-screen reports as a sliding-window must-know.
  • Blind (2025-12)Appears in almost every Juniper medium-difficulty problem list across multiple engineering teams.

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

Explanation: The answer is 'b', with the length of 1.

Example 3

Input
s = "pwwkew"
Output
3

Explanation: The answer is 'wke', with the length of 3.

Approaches

1. Sliding window with Set

Maintain a Set of characters in the current window [left, right]. Expand right; when a duplicate is found, shrink from the left until the window is valid again.

Time
O(n)
Space
O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
  const set = new Set();
  let left = 0, maxLen = 0;
  for (let right = 0; right < s.length; right++) {
    while (set.has(s[right])) {
      set.delete(s[left]);
      left++;
    }
    set.add(s[right]);
    maxLen = Math.max(maxLen, right - left + 1);
  }
  return maxLen;
}

Tradeoff: O(n) amortized — each character enters and leaves the Set at most once. Simple and correct.

2. Sliding window with Map (optimized jump)

Store character → last-seen index in a Map. When a duplicate is found, jump left directly to lastIndex + 1 instead of shrinking one step at a time.

Time
O(n)
Space
O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
  const map = new Map(); // char -> last index
  let left = 0, maxLen = 0;
  for (let right = 0; right < s.length; right++) {
    if (map.has(s[right]) && map.get(s[right]) >= left) {
      left = map.get(s[right]) + 1; // jump past duplicate
    }
    map.set(s[right], right);
    maxLen = Math.max(maxLen, right - left + 1);
  }
  return maxLen;
}

Tradeoff: Also O(n) but with fewer iterations in practice — left jumps forward rather than crawling. The map.get(ch) >= left guard is critical: it prevents jumping left backwards when the same character appears but outside the current window.

Juniper Networks-specific tips

State the sliding-window pattern name explicitly. Juniper interviewers value knowing algorithm taxonomy. The Map-based jump approach is the upgrade to mention: 'I can avoid crawling the left pointer by storing the last index of each character and jumping directly.' The guard `map.get(s[right]) >= left` is the subtle correctness detail — walk through s = 'abba' to demonstrate it.

Common mistakes

  • Not guarding the left-jump with >= left in the Map approach — jumping backwards shrinks the window incorrectly.
  • Counting the window length as right - left instead of right - left + 1.
  • Not handling the empty string — maxLen initialized to 0 handles it correctly.
  • Using substring() and checking uniqueness with a Set for each position — O(n²) and unnecessary.

Follow-up questions

An interviewer at Juniper Networks may pivot to one of these next:

  • Longest Substring with At Most K Distinct Characters (LC 340) — generalize to k distinct.
  • Minimum Window Substring (LC 76) — find the smallest window containing all characters of a target string.
  • How would you apply this sliding-window pattern to a byte stream arriving in real-time?

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 the Set approach work correctly?

Each character enters the Set once and leaves once. The inner while loop shrinks from left until the duplicate is removed, ensuring the window is always valid before we extend right.

What does the >= left guard do in the Map approach?

It checks that the last occurrence of s[right] is inside the current window. If it is before left, there is no duplicate in the window and left should not move.

What is the space complexity?

O(min(n, |Σ|)) where Σ is the character set. For ASCII that is at most 128 entries — effectively O(1) bounded space.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →