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- A. 5
- B. 0
- C. 2
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The minimum number of insertions or deletions needed to make the string "abcca" a palindrome is len(s) - LPS(s), where LPS is the length…
- The edit distance (Levenshtein, insert/delete/replace each cost 1) between "kitten" and "sitting" is computed by the standard table. What…
- The string " hello world " is split by the Python-style rule that runs of spaces are treated as one separator and empty pieces are…
- The longest common subsequence (LCS) of "ABCBDAB" and "BDCABA" is computed by the standard table. The procedure below prints its length…
- The number of non-empty substrings of "abcd" (substrings are contiguous pieces, counted by position) is computed by the procedure below…
- The procedure below counts the uppercase letters in the sentence "Hello World From AI". What is printed? count = 0 for each character c if…