Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Find Longest Substring Without Repeated Characters
00:00
5 left

Find Longest Substring Without Repeated Characters

MediumPython

Problem

Medscape Search may receive queries containing repeated terms or characters. Given a search query string, return its longest contiguous substring that contains no repeated characters.

Use a sliding-window approach that scans the query once. If multiple longest substrings exist, return the one that appears first. Characters are compared exactly, so uppercase letters, lowercase letters, spaces, and punctuation are distinct.

Formal Specification

Implement longest_unique_segment(query), where query is a string. Return a string containing the longest contiguous portion of query in which every character appears at most once. Return an empty string when query is empty. The input must not be modified.

Example 1:

Input: query = "medscape"
Output: "medscape"

Every character appears once, so the complete query is valid.

Example 2:

Input: query = "abcabcbb"
Output: "abc"

The longest valid segments have length three. The first one, "abc", is returned.

Example 3:

Input: query = "pwwkew"
Output: "wke"

"wke" is the longest contiguous substring with no repeated characters.

Constraints

  • 0 <= len(query) <= 100,000
  • query contains printable ASCII characters
  • The input string must not be modified
  • Ties are resolved by returning the earliest longest segment

Function Signature

def longest_unique_segment(query):
Interviewer

Your question is Find Longest Substring Without Repeated Characters. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.