Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compare Two Jumbled Paragraphs

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Compare Two Jumbled Paragraphs. Start with the requirements on the right.

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.

Problem

HashedIn content systems may receive the same paragraph with sentences reordered and words rearranged within each sentence. Write a function that determines whether two paragraphs are structurally identical under these reorderings.

Two paragraphs are considered equal when:

  1. They contain the same number of sentences.
  2. Each sentence in one paragraph matches a sentence in the other paragraph, regardless of sentence order.
  3. Words within a matching sentence may appear in any order, but their frequencies must be identical.
  4. Matching is case-insensitive.
  5. Sentence boundaries are marked by ., !, or ?. Any other non-alphanumeric character separates words, and empty fragments are ignored.

Return True when the paragraphs are equivalent, otherwise return False.

Formal Specification

Implement compare_paragraphs(paragraph_a, paragraph_b), where both inputs are strings. Return a boolean.

Example 1:

Input: paragraph_a = "Fast delivery. Secure platform!"
       paragraph_b = "platform secure? delivery fast."
Output: True

The two sentences and the two words in each sentence are reordered, so their normalized structures match.

Example 2:

Input: paragraph_a = "HashedIn builds great tools."
       paragraph_b = "Great tools build HashedIn."
Output: False

The word frequencies differ because builds and build are different words.

Constraints

  • 1 <= len(paragraph_a), len(paragraph_b) <= 10^6
  • Inputs contain printable ASCII characters
  • Each paragraph contains at most 10^5 sentences
  • Words are contiguous sequences of letters or digits after normalization
  • Repeated words and repeated sentences must be counted separately

Function Signature

def compare_paragraphs(paragraph_a, paragraph_b):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output