Challenge - 5 Problems
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sorting-Based Anagram Check
What is the output of the following Java code that checks if two strings are anagrams by sorting their characters?
DSA Java
import java.util.Arrays; public class Anagram { public static boolean isAnagram(String s1, String s2) { if (s1.length() != s2.length()) return false; char[] a1 = s1.toCharArray(); char[] a2 = s2.toCharArray(); Arrays.sort(a1); Arrays.sort(a2); return Arrays.equals(a1, a2); } public static void main(String[] args) { System.out.println(isAnagram("listen", "silent")); System.out.println(isAnagram("triangle", "integral")); System.out.println(isAnagram("apple", "pale")); } }
Attempts:
2 left
💡 Hint
Think about what sorting characters does to the strings before comparing.
✗ Incorrect
Sorting the characters of both strings and then comparing them checks if they have the same characters in the same frequency. If lengths differ, they can't be anagrams.
❓ Predict Output
intermediateOutput of Frequency Count Anagram Check
What is the output of this Java code that checks if two strings are anagrams by counting character frequencies?
DSA Java
public class AnagramFreq { public static boolean isAnagram(String s1, String s2) { if (s1.length() != s2.length()) return false; int[] count = new int[26]; for (int i = 0; i < s1.length(); i++) { count[s1.charAt(i) - 'a']++; count[s2.charAt(i) - 'a']--; } for (int c : count) { if (c != 0) return false; } return true; } public static void main(String[] args) { System.out.println(isAnagram("dusty", "study")); System.out.println(isAnagram("night", "thing")); System.out.println(isAnagram("hello", "bello")); } }
Attempts:
2 left
💡 Hint
Check how the frequency array is updated for both strings simultaneously.
✗ Incorrect
The code increments counts for s1 and decrements for s2 at the same index. If all counts are zero at the end, strings are anagrams.
🔧 Debug
advancedIdentify the Error in Anagram Check Using HashMap
What error will this Java code produce when checking if two strings are anagrams using a HashMap?
DSA Java
import java.util.HashMap; public class AnagramMap { public static boolean isAnagram(String s1, String s2) { if (s1.length() != s2.length()) return false; HashMap<Character, Integer> map = new HashMap<>(); for (char c : s1.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } for (char c : s2.toCharArray()) { map.put(c, map.get(c) - 1); } for (int val : map.values()) { if (val != 0) return false; } return true; } public static void main(String[] args) { System.out.println(isAnagram("abc", "cab")); System.out.println(isAnagram("abc", "cad")); } }
Attempts:
2 left
💡 Hint
Consider what happens when s2 contains a character not in s1.
✗ Incorrect
When s2 has a character not in s1, map.get(c) returns null, and subtracting 1 from null causes NullPointerException.
🧠 Conceptual
advancedTime Complexity of Different Anagram Checks
Which option correctly ranks the time complexity of these anagram check methods from fastest to slowest for large strings of length n?
Attempts:
2 left
💡 Hint
Consider that frequency count uses fixed-size arrays, HashMap uses hashing, and sorting is more expensive.
✗ Incorrect
Frequency count with fixed arrays is fastest (simple array ops), HashMap counting is next (hash overhead), sorting is slowest (O(n log n)).
🚀 Application
expertResult of Anagram Grouping Using HashMap
Given the following Java code that groups a list of strings into anagrams, what is the printed output?
DSA Java
import java.util.*; public class AnagramGroup { 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
Anagrams share the same sorted character key in the map.
✗ Incorrect
Words with same sorted characters are grouped together. "eat", "tea", "ate" share "aet"; "tan", "nat" share "ant"; "bat" alone "abt".
