Magicsheet logo

Count Common Words With One Occurrence

Easy
100%
Updated 6/1/2025

Count Common Words With One Occurrence

What is this problem about?

The Count Common Words With One Occurrence interview question gives you two arrays of strings, words1 and words2. You need to find the number of strings that appear exactly once in words1 AND exactly once in words2.

Why is this asked in interviews?

Jane Street uses this Array interview pattern to test your basic data structure knowledge. It’s an "Easy" problem that evaluates whether you can use Hash Maps (or Dictionaries) to track frequencies efficiently. It checks for attention to detail—specifically, the "exactly once" constraint in both lists.

Algorithmic pattern used

The problem uses Frequency Counting with Hash Maps.

  1. Create a frequency map for words1.
  2. Create a frequency map for words2.
  3. Iterate through the keys of the first map.
  4. If a word has a count of 1 in the first map AND a count of 1 in the second map, increment the result.

Example explanation

words1 = ["a", "ab", "s"], words2 = ["b", "a", "s", "s"]

  1. Map1: {a: 1, ab: 1, s: 1}.
  2. Map2: {b: 1, a: 1, s: 2}.
  3. Check "a": Count is 1 in Map1, 1 in Map2. (Match!)
  4. Check "ab": Count is 1 in Map1, not in Map2.
  5. Check "s": Count is 1 in Map1, but 2 in Map2. Result: 1.

Common mistakes candidates make

  • Ignoring the "One" constraint: Counting words that appear in both lists but appear multiple times in one of them.
  • Nested Loops: Using O(N*M) search instead of O(N+M) hash map lookups.
  • Case Sensitivity: Not clarifying if "Apple" and "apple" are the same word (usually they are different unless specified).

Interview preparation tip

For frequency problems, always consider the trade-off between using a Hash Map (general) and a fixed-size array (if the alphabet is small). In this case, since words are arbitrary strings, a Hash Map is the only way to go.

Similar Questions