Challenge - 5 Problems
Anagram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Grouping Anagrams by Sorted Key
What is the output of the following Java code that groups anagrams using a hash map with sorted strings as keys?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { String[] words = {"eat", "tea", "tan", "ate", "nat", "bat"}; Map<String, List<String>> map = new HashMap<>(); for (String word : words) { char[] chars = word.toCharArray(); Arrays.sort(chars); String key = new String(chars); map.computeIfAbsent(key, k -> new ArrayList<>()).add(word); } System.out.println(map.values()); } }
Attempts:
2 left
💡 Hint
Think about how sorting each word groups anagrams under the same key.
✗ Incorrect
The code sorts each word to create a key. Anagrams have the same sorted key, so they group together. The output is a collection of lists where each list contains anagrams.
🧠 Conceptual
intermediateWhy Use Sorted Strings as Keys in Grouping Anagrams?
Why is sorting each string used as the key in a hash map when grouping anagrams?
Attempts:
2 left
💡 Hint
Think about what makes anagrams similar when rearranged.
✗ Incorrect
Anagrams have the same letters in different orders. Sorting letters puts them in the same order, so anagrams share the same sorted string key.
🔧 Debug
advancedIdentify the Error in Grouping Anagrams Code
What error will the following Java code produce when grouping anagrams using a hash map?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { String[] words = {"bat", "tab", "eat"}; Map<String, List<String>> map = new HashMap<>(); for (String word : words) { char[] chars = word.toCharArray(); String key = new String(chars); Arrays.sort(chars); map.putIfAbsent(key, new ArrayList<>()); map.get(key).add(word); } System.out.println(map.values()); } }
Attempts:
2 left
💡 Hint
Check the order of sorting and key creation.
✗ Incorrect
The key is created before sorting the char array, so keys are different for anagrams. This causes incorrect grouping.
❓ Predict Output
advancedOutput of Group Anagrams Using Character Count as Key
What is the output of the following Java code that groups anagrams using character count arrays as keys?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { String[] words = {"abc", "bca", "cab", "xyz", "zyx"}; Map<String, List<String>> map = new HashMap<>(); for (String word : words) { int[] count = new int[26]; for (char c : word.toCharArray()) { count[c - 'a']++; } StringBuilder keyBuilder = new StringBuilder(); for (int i = 0; i < 26; i++) { keyBuilder.append('#').append(count[i]); } String key = keyBuilder.toString(); map.computeIfAbsent(key, k -> new ArrayList<>()).add(word); } System.out.println(map.values()); } }
Attempts:
2 left
💡 Hint
Character count arrays uniquely identify anagrams.
✗ Incorrect
The code counts characters for each word and uses the counts as a key. Anagrams have identical counts, so they group together.
🧠 Conceptual
expertWhy Use Character Count Instead of Sorting for Grouping Anagrams?
What is the main advantage of using character count arrays as keys instead of sorting strings when grouping anagrams?
Attempts:
2 left
💡 Hint
Consider the time complexity of sorting vs counting characters.
✗ Incorrect
Counting characters takes linear time relative to word length, while sorting takes more time. This makes character count keys faster for large inputs.
