Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE

Problem: Dictionary: a list of words is ["cat", "dog", "bat", "dig"]. Words are grouped by changing exactly one letter (Hamming distance 1). How many connected groups are there?

  1. A. 2
  2. B. 3
  3. C. 1
  4. D. 4

Correct answer: A. 2

Union-find joins words at Hamming distance one. Here 'cat'–'bat' and 'dog'–'dig' are linked, and the other pairs are farther apart, giving two groups.

More Coding Interview Problems questions

Exams that ask this topic