Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Compare Two Texts and Separate Matches
00:00
5 left

Compare Two Texts and Separate Matches

MediumPython

Problem

Write a function that takes two lists of strings (or text files), compares them, and separates matching strings into one list and differing strings into another list.

Asked in the Phone Screen stage. Implement the list version using the signature below. Treat the inputs as multisets: duplicate occurrences matter. Return [matching, differing], preserving the order of occurrences from the first list, followed by unmatched occurrences from the second list. Each occurrence can be matched only once.

Input: Two lists of strings.

Output: A two-element list containing the matching strings and differing strings.

Example: list1 = ["error", "info", "error"], list2 = ["error", "warn"] returns [["error"], ["info", "error", "warn"]].

Constraints

  • 0 <= len(list1), len(list2) <= 10^5
  • Each element is a string
  • Strings may be duplicated
  • Matching is case-sensitive
  • Output order must follow the specified occurrence order

Function Signature

def compare_string_lists(list1, list2):
Interviewer

Your question is Compare Two Texts and Separate Matches. 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.