Magicsheet logo

Uncommon Words from Two Sentences

Easy
12.5%
Updated 8/1/2025

Asked by 2 Companies

Uncommon Words from Two Sentences

What is this problem about?

The Uncommon Words from Two Sentences interview question is a practical string manipulation challenge. You are given two sentences, and you need to find all the "uncommon" words. A word is considered uncommon if it appears exactly once in one of the sentences and does not appear at all in the other sentence. Essentially, you are looking for words that have a total frequency of exactly one across both strings combined.

Why is this asked in interviews?

This Uncommon Words from Two Sentences coding problem is a favorite for entry-level roles at companies like Amazon and Google. It tests your ability to handle basic string parsing and your proficiency with frequency-counting data structures. It’s a great way to see if a candidate can simplify a problem—realizing that the two sentences can be merged into one large collection of words makes the logic much cleaner.

Algorithmic pattern used

The most effective Hash Table, Counting, String interview pattern for this problem involves three steps: concatenation, splitting, and counting. First, you combine the two sentences into a single string. Then, you split that string into individual words using spaces as delimiters. Finally, you use a Hash Table (or a dictionary) to count the occurrences of each word. Any word with a count of exactly 1 is returned in the final result.

Example explanation

Suppose sentence 1 is "apple banana apple" and sentence 2 is "banana orange".

  1. Combine them: "apple banana apple banana orange".
  2. Count the words:
    • apple: 2
    • banana: 2
    • orange: 1
  3. The only word with a count of 1 is "orange". So, the output is ["orange"].

Common mistakes candidates make

One common mistake is trying to compare the two sentences separately and overcomplicating the logic with nested loops. Another is failing to handle punctuation (though usually these problems assume clean space-separated input). Candidates also sometimes forget that an "uncommon" word must appear exactly once; if it appears twice in the same sentence, it is no longer uncommon.

Interview preparation tip

When dealing with "unique" or "uncommon" items across multiple sets, always consider if merging the sets first simplifies the counting process. The Hash Table interview pattern is almost always the right tool for frequency-based problems.

Similar Questions