PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's skills in string manipulation, algorithm design, and reasoning about time and space complexity when identifying maximal contiguous substrings with distinct characters.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Find longest substring without repeating characters

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Given a string `s`, return the length of the longest contiguous substring that contains no repeated characters. ### Input - `s`: a string (may contain letters, digits, symbols, and spaces). ### Output - An integer: the maximum length of a substring of `s` with all distinct characters. ### Examples - Input: `"abcabcbb"` → Output: `3` (e.g., `"abc"`) - Input: `"bbbbb"` → Output: `1` (e.g., `"b"`) - Input: `"pwwkew"` → Output: `3` (e.g., `"wke"`) ### Constraints (typical) - `0 <= len(s) <= 2 * 10^5` ### Notes - The substring must be contiguous (not a subsequence).

Quick Answer: This question evaluates a candidate's skills in string manipulation, algorithm design, and reasoning about time and space complexity when identifying maximal contiguous substrings with distinct characters.

## Problem Given a string `s`, return the length of the longest contiguous substring that contains no repeated characters. ### Input - `s`: a string (may contain letters, digits, symbols, and spaces). ### Output - An integer: the maximum length of a substring of `s` with all distinct characters. ### Examples - Input: `"abcabcbb"` → Output: `3` (e.g., `"abc"`) - Input: `"bbbbb"` → Output: `1` (e.g., `"b"`) - Input: `"pwwkew"` → Output: `3` (e.g., `"wke"`) ### Constraints - `0 <= len(s) <= 2 * 10^5` ### Notes - The substring must be contiguous (not a subsequence).

Constraints

  • 0 <= len(s) <= 2 * 10^5
  • s may contain letters, digits, symbols, and spaces.
  • The answer substring must be contiguous, not a subsequence.

Examples

Input: ("abcabcbb",)

Expected Output: 3

Explanation: The longest distinct-char window is "abc" with length 3.

Input: ("bbbbb",)

Expected Output: 1

Explanation: Every character is 'b', so the best window is a single "b".

Input: ("pwwkew",)

Expected Output: 3

Explanation: "wke" has length 3; note "pwke" is a subsequence, not contiguous, so it does not count.

Input: ("",)

Expected Output: 0

Explanation: Empty string has no characters, so the answer is 0.

Input: (" ",)

Expected Output: 1

Explanation: A single space is one distinct character, length 1.

Input: ("dvdf",)

Expected Output: 3

Explanation: When the second 'd' appears, start jumps to index 2; "vdf" gives length 3.

Input: ("abba",)

Expected Output: 2

Explanation: After the second 'a' at index 3, start must stay at 2 (not jump back to 1); window "ab" / "ba" has length 2. This checks the `>= start` guard.

Input: ("tmmzuxt",)

Expected Output: 5

Explanation: "mzuxt" is the longest distinct window with length 5.

Input: ("au",)

Expected Output: 2

Explanation: All characters distinct, full string length 2.

Input: ("aab",)

Expected Output: 2

Explanation: After the duplicate 'a', the best window is "ab" with length 2.

Hints

  1. Use a sliding window with two pointers [start, i]. Expand i one character at a time and shrink the window from the left only when a duplicate enters.
  2. Track the last index at which each character was seen. When you hit a repeat, jump `start` to one past the previous occurrence — but never move `start` backward (guard with `last_seen[ch] >= start`).
  3. At each step the current window length is `i - start + 1`; keep the running maximum.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)