PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string manipulation and algorithmic problem-solving skills by requiring a Python implementation to determine the length of the longest substring without repeating characters, with an optional follow-up to extract all maximal substrings.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Data Scientist

Implement Python Function for Longest Unique Substring Length

Company: Amazon

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

##### Scenario Real-time text analytics service enforcing uniqueness constraints ##### Question Implement a Python function returning the length of the longest substring without repeating characters. Follow-up: modify it to return all substrings that meet this criterion. ##### Hints Sliding window with hash map for indices; for follow-up, track start positions and collect substrings of max length.

Quick Answer: This question evaluates string manipulation and algorithmic problem-solving skills by requiring a Python implementation to determine the length of the longest substring without repeating characters, with an optional follow-up to extract all maximal substrings.

Longest Substring Without Repeating Characters — Length

You are building a real-time text analytics service that enforces a uniqueness constraint on character windows. Given a string `s`, return the length of the longest substring of `s` that contains no repeating characters. A substring is a contiguous sequence of characters within the string. The answer is the maximum window length you can slide over `s` such that every character inside the window is distinct. Examples: - `"abcabcbb"` -> `3` (the answer is `"abc"`). - `"bbbbb"` -> `1` (the answer is `"b"`). - `"pwwkew"` -> `3` (the answer is `"wke"`; note `"pwke"` is a subsequence, not a substring). - `""` -> `0`. Use a sliding window with a hash map from each character to its most recent index. When you encounter a repeat that lies inside the current window, jump the window start to one past the previous occurrence. Track the maximum window length as you go.

Constraints

  • 0 <= len(s) <= 5 * 10^4
  • s consists of English letters, digits, symbols, and spaces.
  • The empty string has answer 0.
  • Substrings must be contiguous (not subsequences).

Examples

Input: ("abcabcbb",)

Expected Output: 3

Explanation: The longest substring without repeats is "abc", length 3.

Input: ("bbbbb",)

Expected Output: 1

Explanation: Every character is the same; the best window is a single "b".

Input: ("pwwkew",)

Expected Output: 3

Explanation: "wke" (and "kew") have length 3; "pwke" is a subsequence, not a substring.

Input: ("",)

Expected Output: 0

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

Input: ("a",)

Expected Output: 1

Explanation: A single character is trivially unique.

Input: ("dvdf",)

Expected Output: 3

Explanation: The window resets at the second 'd'; "vdf" has length 3.

Input: ("abba",)

Expected Output: 2

Explanation: After the repeated 'b', start jumps to index 2; "ab" at the end has length 2. The map must be guarded by start so the stale 'a' index does not shrink incorrectly.

Input: (" ",)

Expected Output: 1

Explanation: A single space is a valid unique character.

Input: ("tmmzuxt",)

Expected Output: 5

Explanation: "mzuxt" has length 5; the leading repeated 'm' and trailing 't' bound the window.

Hints

  1. Maintain a sliding window [start, i] that always contains distinct characters.
  2. Store the most recent index of each character in a hash map.
  3. When the current character was last seen at index j >= start, move start to j + 1 instead of shrinking one step at a time.
  4. After updating the window for index i, the current length is i - start + 1; keep the running maximum.

Longest Substring Without Repeating Characters — All Maximal Substrings

Follow-up to the length problem. Given a string `s`, return every distinct substring of `s` that has no repeating characters AND whose length equals the maximum such length. Return the substrings as a list sorted in ascending lexicographic order, with duplicates removed. For example, in `"abcabcbb"` the maximum length is 3 and the maximal windows are `"abc"`, `"bca"`, and `"cab"`, so the answer is `["abc", "bca", "cab"]`. For `"pwwkew"` the maximum length is 3 and both `"wke"` and `"kew"` qualify, giving `["kew", "wke"]` after sorting. For the empty string return `[]`. Extend the sliding-window approach: track the maximum length found so far, and collect the window text each time you hit that length. When you find a strictly longer window, discard the old collection and start fresh. Finally de-duplicate (the same substring text can occur at multiple positions) and sort.

Constraints

  • 0 <= len(s) <= 5 * 10^4
  • s consists of English letters, digits, symbols, and spaces.
  • Return distinct substrings only (the same text appearing at different positions counts once).
  • Output is sorted in ascending lexicographic order.
  • For the empty string return an empty list.

Examples

Input: ("abcabcbb",)

Expected Output: ["abc", "bca", "cab"]

Explanation: Max length is 3; the three distinct length-3 windows are abc, bca, cab.

Input: ("bbbbb",)

Expected Output: ["b"]

Explanation: Max length is 1 and the only distinct window is "b".

Input: ("pwwkew",)

Expected Output: ["kew", "wke"]

Explanation: Max length is 3; both "wke" and "kew" qualify, sorted to kew, wke.

Input: ("",)

Expected Output: []

Explanation: Empty string yields no substrings.

Input: ("a",)

Expected Output: ["a"]

Explanation: Single character is the only maximal window.

Input: ("dvdf",)

Expected Output: ["vdf"]

Explanation: Max length is 3 and only "vdf" reaches it; "dvd" repeats 'd'.

Input: ("abcdef",)

Expected Output: ["abcdef"]

Explanation: The whole string is unique, so it is the single maximal substring.

Input: ("aabbaabb",)

Expected Output: ["ab", "ba"]

Explanation: Max length is 2; the distinct length-2 unique windows are "ab" and "ba" (each occurs multiple times but is counted once).

Input: ("tmmzuxt",)

Expected Output: ["mzuxt"]

Explanation: Max length is 5 and only "mzuxt" achieves it.

Hints

  1. Run the same sliding window as the length problem, but also remember the window text whenever its length ties the current maximum.
  2. When you find a strictly longer window, reset your collection to just that new window.
  3. The same substring text can appear at multiple start positions, so de-duplicate with a set before returning.
  4. Sort the final list so the output is deterministic regardless of scan order.
Last updated: Jun 25, 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
  • 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 Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)