Skip to content
Work Free practice Coding course Blog Method Results Why me About Enquire Book a call

Coding · Sliding window

Longest run of distinct characters

Medium · Target 15 minutes · Sliding window · Type asked atOptiverTwo SigmaAmazon

Given a string s, return the length of the longest contiguous substring in which no character repeats.

Examples

InputOutputWhy
s = "abcabcbb"3"abc".
s = "bbbbb"1"b".
s = "pwwkew"3"wke". "pwke" is a subsequence, not a substring.

Constraints

  • 0 ≤ |s| ≤ 10⁵
  • s contains printable ASCII characters

Hints

Hint 1

Brute force checks every substring in O(n²) or worse. What work is being repeated?

Hint 2

Keep a window that is always valid — no repeats inside it — and never move the left edge backwards.

Hint 3

When you meet a character you have seen inside the current window, jump the left edge to just past its previous position. Store the last index of each character.

Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.