Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Case-Sensitive Word Mismatch Function
00:00
5 left

Case-Sensitive Word Mismatch Function

MediumPython

Problem

A Meta Ads text-processing pipeline compares two whitespace-delimited strings and needs to identify word occurrences that cannot be matched between them. Write a function that returns the unmatched occurrences, preserving their left-to-right order within their original string.

Matching is case sensitive. If a word occurs a times in the first string and b times in the second, return abs(a - b) copies of that word from the string with the larger count. Preserve the order in which returned copies appear in that source string. Punctuation remains part of a word because words are split only on whitespace.

Formal Specification

Implement unmatched_words(s1, s2).

  • Input: Two strings, s1 and s2, containing zero or more whitespace-delimited words.
  • Output: A list of strings containing unmatched word occurrences. Return an empty list when every occurrence can be paired.

Constraints

  • 0 <= len(s1), len(s2) <= 10^5
  • Each string contains at most 2 * 10^4 words
  • Words are separated by arbitrary whitespace
  • Matching is case sensitive
  • Punctuation remains part of a word

Function Signature

def unmatched_words(s1, s2):
Interviewer

Your question is Case-Sensitive Word Mismatch Function. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.