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
Word Matching Between Two Strings
00:00
5 left

Word Matching Between Two Strings

MediumPython

Problem

Meta Logistics compares text fields from two logistics systems and needs to identify word occurrences that do not match across the strings. Write a function that returns unmatched words while preserving their left-to-right order.

A word is a maximal sequence of non-whitespace characters. Matching is case sensitive, so Route and route are different words. Each occurrence in one string can match at most one identical occurrence in the other string. Return unmatched occurrences from the first string in their original order, followed by unmatched occurrences from the second string in their original order.

Formal Specification

  • Input: two strings, first and second.
  • Output: a list of strings containing all unmatched word occurrences.
  • Split both strings using standard whitespace behavior, so multiple spaces and leading or trailing whitespace are allowed.
  • If every word occurrence matches, return an empty list.

Examples

Example 1

Input: first = "Meta Logistics ships parcels", second = "Meta Logistics tracks routes"
Output: ["ships", "parcels", "tracks", "routes"]

Meta and Logistics match. The remaining words occur in only one string.

Example 2

Input: first = "Route route route", second = "route route"
Output: ["Route", "route"]

Route differs from route, and one lowercase route occurrence remains unmatched.

Constraints

  • 0 <= len(first), len(second) <= 10^5
  • Each string contains only whitespace and non-whitespace word characters.
  • The total number of word occurrences is at most 2 * 10^5.

Constraints

  • 0 <= len(first), len(second) <= 10^5
  • The total number of word occurrences is at most 2 * 10^5
  • Words are separated by arbitrary whitespace
  • Matching is case sensitive

Function Signature

def unmatched_words(first, second):
Interviewer

Your question is Word Matching Between Two Strings. 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.