# The Only Difference

> Two versions of the same line. Surface only what changed.

Canonical URL: <https://datadriven.io/problems/the_only_difference>

Domain: Python · Difficulty: easy · Seniority: L3 · Asked in: Meta

## Problem

A document editor lines up the original and edited version of a sentence so a reviewer can see what changed. Given the two versions as `sentence_a` and `sentence_b`, split each on whitespace and return the words present in exactly one version, with `sentence_a`'s uniques listed before `sentence_b`'s.

## Example 1

Input:

```
sentence_a = "Facebook is an awesome place", sentence_b = "Facebook Is an AWESOME place"
```

Output:

```
["is","awesome","Is","AWESOME"]
```

## Example 2

Input:

```
sentence_a = "the cat the dog", sentence_b = "the cat the fox"
```

Output:

```
["dog","fox"]
```

## Worked solution and explanation

### What this problem really is

This is an order-preserving symmetric difference dressed up as document diffing. Anyone can split the two strings and build a set from each; what separates candidates is seeing that the tempting one-liner, `a_set ^ b_set`, fails on two counts. A set has no order, but the contract wants A's uniques first, then B's, each in appearance order. And the moment you lowercase to 'normalize', you merge 'is' with 'Is' and 'awesome' with 'AWESOME', erasing the exact edits you were hired to surface.

---

### Break down the requirements

#### Step 1: Split into lists, and mirror each into a set

Split each version on whitespace into a word list, and build a set from each list. Keep both: the lists preserve appearance order for the output, the sets give O(1) membership checks. A plain `w in b_words` list scan would also be correct, just O(n*m); the set is the cheap way to keep it linear.

#### Step 2: Filter each list by the opposite set

A word is a mismatch if it sits in one version's list but is absent from the other version. You test membership against the SET, but you walk the original LIST, so casing and order survive untouched. This is the case-sensitive part: 'is' and 'Is' are different keys, so neither cancels the other.

#### Step 3: A's words, then B's words, in order

Emit `sentence_a`'s unique words first (in order of appearance), then `sentence_b`'s unique words (in order of appearance). Concatenation of the two filtered lists gives exactly that.

---

### The solution

**Symmetric difference via set membership**

```python
def mismatched_words(sentence_a: str, sentence_b: str) -> list[str]:
    a_words = sentence_a.split()
    b_words = sentence_b.split()
    a_set = set(a_words)
    b_set = set(b_words)
    result = [w for w in a_words if w not in b_set]
    result += [w for w in b_words if w not in a_set]
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n + m) where n and m are the word counts: building the sets and scanning each word list are linear, with O(1) set membership checks. The naive `w in b_words` list scan degrades to O(n*m).
> 
> Space: O(n + m) for the two word sets and the result list.

> **Interviewers Watch For**
>
> The tell is watching whether the candidate reaches for `a_set ^ b_set` and stops there. That returns an unordered set and drops the A-then-B ordering. The senior move is to keep the sets for lookup but iterate the original lists, so order and exact casing are preserved for free.

> **Common Pitfall**
>
> The comparison is case-sensitive. Lowercasing to 'normalize' merges 'is' with 'Is' and 'awesome' with 'AWESOME', so the intentional edits vanish from the output and the diff silently reports nothing changed.

---

## Common follow-up questions

- What if you needed to know which words were added vs. removed? _(Tests using `new - old` for additions and `old - new` for removals separately.)_
- What if word frequency mattered (a word appearing 3 times vs. 2 times)? _(Tests using Counter objects and comparing counts.)_
- How would you generate a unified diff of the two texts? _(Tests knowledge of the `difflib` module for line-level diffs.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_only_difference)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.