ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (grouping anagrams together and counting the groups)?

What will be the output of the following pseudocode (grouping anagrams together and counting the groups)?
List words = {"eat", "tea", "tan", "ate", "nat", "bat"}
Map groups = empty map
for (each w in words)
    String key = sortedChars(w)
    append(groups[key], w)
end for
Print size(groups)
  1. A. 6
  2. B. 3
  3. C. 2
  4. D. 4

Correct answer: B. 3

Sorting each word's letters groups them by anagram: 'eat','tea','ate' all sort to 'aet'; 'tan','nat' both sort to 'ant'; 'bat' sorts to 'abt' alone. That's 3 distinct groups.

More Pseudocode and Code Tracing questions

Exams that ask this topic