ProgrammingHardInfosys · Cognizant

Two words are checked as anagrams by comparing letter counts. The procedure below sums the absolute differences between the letter counts of "hello" and "bello". What is printed?

Two words are checked as anagrams by comparing letter counts. The procedure below sums the absolute differences between the letter counts of "hello" and "bello". What is printed?
for each letter x in the alphabet
    total = total + |count_hello(x) - count_bello(x)|
end for
Print total
  1. A. 5
  2. B. 0
  3. C. 2
  4. D. 1

Correct answer: C. 2

The counts differ only for h (one in hello, none in bello) and b (none in hello, one in bello), so the total difference is 2. Counting the distinct letters of both words gives 5, the signed sum of differences is 0, and skipping letters only in bello gives 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic