Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new list for each group of anagrams.
DSA Java
if (!map.containsKey(key)) { map.put(key, new [1]<>()); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using HashMap instead of a list for values.
Using HashSet which does not preserve order.
✗ Incorrect
We use ArrayList to store the list of anagrams for each key in the map.
2fill in blank
mediumComplete the code to add the current word to the list of anagrams.
DSA Java
map.get(key).[1](word);Attempts:
3 left
💡 Hint
Common Mistakes
Using put() which is for maps, not lists.
Using append() which is not a Java List method.
✗ Incorrect
The add() method adds an element to the ArrayList.
3fill in blank
hardFix the error in sorting the characters of the word to create the key.
DSA Java
char[] chars = word.toCharArray();
Arrays.[1](chars);Attempts:
3 left
💡 Hint
Common Mistakes
Using Arrays.sorted() which does not exist.
Using order() or arrange() which are not Java methods.
✗ Incorrect
Arrays.sort() sorts the array of characters in place.
4fill in blank
hardFill both blanks to convert the sorted char array back to a string key.
DSA Java
String key = new String([1]); map.putIfAbsent(key, new [2]<>());
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of chars to create the key.
Using HashMap instead of ArrayList for the map value.
✗ Incorrect
We create a string from the sorted chars and initialize a new ArrayList for the map value.
5fill in blank
hardFill all three blanks to return the grouped anagrams as a list of lists.
DSA Java
return new ArrayList<>([1].[2]()); // where [3] is the map variable
Attempts:
3 left
💡 Hint
Common Mistakes
Using keySet() or entrySet() instead of values().
Using a different variable name than the map.
✗ Incorrect
We get the collection of values from the map and convert it to a list to return grouped anagrams.
