Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize an empty hash map for grouping anagrams.
DSA Javascript
const map = new [1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using Array or Set instead of Map for key-value storage.
Using Object but forgetting it doesn't preserve insertion order.
✗ Incorrect
We use a Map to store groups of anagrams with keys as sorted strings.
2fill in blank
mediumComplete the code to sort the characters of the string 'word' to create the key.
DSA Javascript
const key = word.split('').[1]().join('');
Attempts:
3 left
💡 Hint
Common Mistakes
Using reverse() which reverses but does not sort.
Using map() or filter() which do not sort.
✗ Incorrect
Sorting characters alphabetically groups anagrams under the same key.
3fill in blank
hardFix the error in checking if the map has the key and adding the word to the group.
DSA Javascript
if (!map.[1](key)) { map.set(key, []); } map.get(key).push(word);
Attempts:
3 left
💡 Hint
Common Mistakes
Using contains() or includes() which are not Map methods.
Using exists() which is not a JavaScript method.
✗ Incorrect
The Map method to check key existence is has().
4fill in blank
hardFill both blanks to convert the map values to an array of grouped anagrams.
DSA Javascript
const result = Array.[1](map.[2]());
Attempts:
3 left
💡 Hint
Common Mistakes
Using map.keys() or map.entries() which do not give groups directly.
Using Array.entries() which is not a function.
✗ Incorrect
Array.from() converts iterable map.values() to an array of groups.
5fill in blank
hardFill all three blanks to complete the full function that groups anagrams.
DSA Javascript
function groupAnagrams(strs) {
const map = new [1]();
for (const word of strs) {
const key = word.split('').[2]().join('');
if (!map.[3](key)) {
map.set(key, []);
}
map.get(key).push(word);
}
return Array.from(map.values());
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Set instead of Map for grouping.
Using reverse instead of sort for keys.
Using contains or exists instead of has.
✗ Incorrect
Use Map to store groups, sort to create keys, and has to check keys.
