Skip to main content

3. Longest Substring Without Repeating Characters

mediumAsked at Elastic

Find the length of the longest substring with all unique characters. Elastic favors this problem because the sliding-window technique it demands is the same mental model used in Elasticsearch's windowed token analysis during text tokenization and shingle generation.

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

Source citations

Public interview reports confirming this problem appears in Elastic loops.

  • Glassdoor (2026-Q1)Multiple Elastic SWE candidates report sliding-window problems as a consistent mid-difficulty question in both phone and onsite rounds.
  • Blind (2025-12)Elastic thread lists this problem as one of the most commonly reported questions across SWE-II and senior-SWE interviews.

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 substring 'abc' has length 3.

Example 2

Input
s = "bbbbb"
Output
1

Explanation: The only valid substring is 'b'.

Example 3

Input
s = "pwwkew"
Output
3

Explanation: The substring 'wke' has length 3.

Approaches

1. Sliding window with Map (jump pointer)

Maintain a Map from character to its most recent index. A left pointer marks the start of the current window. When a duplicate is found, jump left to max(left, lastSeen[char] + 1) to shrink the window past the previous occurrence.

Time
O(n)
Space
O(min(m, n)) where m = charset size
function lengthOfLongestSubstring(s) {
  const lastSeen = new Map();
  let left = 0;
  let maxLen = 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; // jump window start past duplicate
    }
    lastSeen.set(ch, right);
    maxLen = Math.max(maxLen, right - left + 1);
  }
  return maxLen;
}

Tradeoff: O(n) with a single pass and at most one map operation per character. The jump-pointer variant avoids shrinking one step at a time and is faster in practice.

2. Sliding window with Set

Keep a Set of characters in the current window. When a duplicate enters, advance left while removing characters until the duplicate is removed, then add the new character.

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

Tradeoff: Easier to reason about but the inner while loop means left can advance O(n) total steps across all right iterations — still O(n) overall but with worse constants than the Map approach.

Elastic-specific tips

Elastic interviewers like when you state the window invariant before coding: 'At every point, the window [left, right] contains only unique characters.' Then explain the Map-based jump: 'Instead of shrinking one step at a time, I jump left directly past the previous occurrence of the duplicate — this avoids redundant deletions.' This level of precision about loop invariants is valued in Elastic's engineering culture.

Common mistakes

  • With the Map approach, not checking lastSeen.get(ch) >= left — if the previous occurrence was before the current window, it should not trigger a left jump.
  • Updating lastSeen after computing maxLen — always update lastSeen.set(ch, right) to record the current position.
  • Off-by-one in window size: right - left + 1, not right - left.
  • Not handling the empty string — the loop simply never executes and returns 0 correctly.

Follow-up questions

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

  • Longest Substring with At Most K Distinct Characters (LC 340) — generalize the window to allow k distinct characters.
  • Minimum Window Substring (LC 76) — find the smallest window containing all characters of a pattern.
  • How would you extend this to find the longest substring with at most 2 character swaps allowed?

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 check lastSeen.get(ch) >= left before jumping?

The Map stores all historical occurrences, not just those in the current window. If the stored index is before left, that duplicate is already outside the window and should be ignored.

What counts as 'characters' — ASCII only?

The problem specifies English letters, digits, symbols and spaces — effectively printable ASCII. For Unicode or multi-byte characters, use a Map keyed by code point.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →