Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Maximal Common Substring
00:00
5 left

Maximal Common Substring

MediumPython

Problem

A TotalEnergies OneTech data pipeline compares operational identifiers from two systems. Given two strings, return their longest common contiguous substring.

Unlike a subsequence, every character in the result must occupy consecutive positions in both input strings. If multiple longest common substrings exist, return the one whose starting position is earliest in s1. If there is no common character, return an empty string.

Formal Specification

Implement longest_common_substring(s1, s2), where both inputs are strings. Return a string containing the longest substring that appears in both s1 and s2. Matching is case-sensitive, and characters include letters, digits, spaces, and punctuation.

Your solution should use dynamic programming and limit auxiliary space to O(min(len(s1), len(s2))).

Constraints

  • 0 <= len(s1), len(s2) <= 5000
  • Inputs contain standard Unicode characters
  • Matching is case-sensitive
  • The result must be contiguous in both strings
  • For equal-length answers, return the one with the earliest start in s1

Function Signature

def longest_common_substring(s1, s2):
Interviewer

Your question is Maximal Common Substring. 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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.