Skip to main content

11. Longest Substring Without Repeating Characters

mediumAsked at Postman

Find the length of the longest substring without repeating characters.

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. Substrings must be contiguous.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s consists of printable ASCII

Examples

Example 1

Input
s = "abcabcbb"
Output
3

Example 2

Input
s = "bbbbb"
Output
1

Approaches

1. Brute force

Enumerate every substring and check uniqueness via a Set.

Time
O(n^3)
Space
O(n)
for each i,j: build set; if size==len update best

Tradeoff:

2. Sliding window with last-seen map

Track the last index of each character; advance the left edge to one past the duplicate when found.

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

Tradeoff:

Postman-specific tips

Postman sliding-window questions tie directly to URL deduplication in the request history: same shape, scan once, track last-seen by canonical key.

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 Postman interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →