PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string manipulation, use of associative data structures (hash maps/sets), and algorithmic thinking for detecting and maintaining unique-character substrings.

  • Medium
  • Snowflake
  • Coding & Algorithms
  • Software Engineer

Find longest unique-character substring

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Given a string s, return the length of the longest substring without repeating characters. Explain and implement an O(n) sliding-window solution using a hash map or set. Discuss time and space complexity, and note any changes needed to correctly handle Unicode characters.

Quick Answer: This question evaluates proficiency in string manipulation, use of associative data structures (hash maps/sets), and algorithmic thinking for detecting and maintaining unique-character substrings.

Given a string `s`, return the length of the longest substring of `s` that contains no repeating characters. A substring is a contiguous run of characters within the string. Implement an O(n) sliding-window solution using a hash map (character -> last seen index) or a set. As the right edge of the window expands one character at a time, advance the left edge just past the previous occurrence of any character that would otherwise repeat, and track the maximum window width seen. Note on Unicode: iterating in Python over `str` already yields Unicode code points, so the algorithm is correct as written for the basic multilingual characters most inputs use. For full correctness over user-perceived characters (grapheme clusters such as emoji with combining marks or skin-tone modifiers), you would segment on grapheme boundaries rather than code points before applying the same window logic. Return 0 for an empty string.

Constraints

  • 0 <= len(s) <= 5 * 10^4
  • s may contain letters, digits, symbols, and spaces (any Unicode code points)
  • Return 0 for the empty string

Examples

Input: ("abcabcbb",)

Expected Output: 3

Explanation: The answer is "abc", length 3.

Input: ("bbbbb",)

Expected Output: 1

Explanation: The answer is "b", length 1 — every character repeats.

Input: ("pwwkew",)

Expected Output: 3

Explanation: The answer is "wke", length 3. Note "pwke" is a subsequence, not a substring.

Input: ("",)

Expected Output: 0

Explanation: Empty string has no substring; length 0.

Input: ("a",)

Expected Output: 1

Explanation: Single character, length 1.

Input: ("dvdf",)

Expected Output: 3

Explanation: The answer is "vdf", length 3. The repeated 'd' jumps start to index 2, and the window correctly grows again.

Input: ("abba",)

Expected Output: 2

Explanation: After "ab", the second 'b' moves start to 2; the trailing 'a' must NOT move start backward (its old index 0 is outside the window), so "ab" and "ba" both give length 2.

Input: (" ",)

Expected Output: 1

Explanation: A single space is a valid non-repeating character, length 1.

Input: ("tmmzuxt",)

Expected Output: 5

Explanation: The answer is "mzuxt", length 5. The leading 't' at index 0 must not shrink the window when the final 't' appears, since index 0 is already outside it.

Input: ("abcdefghijklmnopqrstuvwxyz",)

Expected Output: 26

Explanation: All 26 letters are distinct, so the whole string is the answer, length 26.

Hints

  1. Use two pointers forming a window [start, i]; expand i one step at a time and shrink from the left only when you hit a repeat.
  2. Store the last index at which each character was seen so you can jump start directly past the previous occurrence instead of stepping one-by-one.
  3. Guard the jump with `last_seen[ch] >= start` — a stale occurrence that is already outside the window (e.g. the second 'a' in "abba") must not move start backward.
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

  • Implement a JSON Parser - Snowflake (hard)
  • Solve Matrix and Array Distance Problems - Snowflake (medium)
  • Solve Array Distance and Wiki Navigation - Snowflake (medium)
  • Implement Document Predicate APIs - Snowflake (medium)
  • Find Shortest Wiki Click Path - Snowflake (medium)