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)- A. 6
- B. 3
- C. 2
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (a do-while-style loop that always runs at least once)? Integer x = 10, c = 0 repeat x…
- What will be the output of the following pseudocode (searching a 2D array, stopping both loops once found)? (Arrays are indexed from 0.)…
- What will be the output of the following pseudocode (finding the missing number from 1..n)? (Arrays are indexed from 0.) Integer arr[5] =…
- What will be the output of the following pseudocode (checking whether an array is sorted in non-decreasing order)? (Arrays are indexed…
- What will be the output of the following pseudocode (summing a range with a step)? Function sumRange(Integer a, Integer b, Integer step)…
- What will be the output of the following pseudocode (tail-recursive factorial with an accumulator)? Function factAcc(Integer n, Integer…