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 this C++ code that checks if two strings are anagrams by sorting?
DSA C++
bool areAnagrams(std::string s1, std::string s2) { if (s1.length() != s2.length()) return false; std::sort(s1.begin(), s1.end()); std::sort(s2.begin(), s2.end()); return s1 == s2; } int main() { std::string a = "listen"; std::string b = "silent"; if (areAnagrams(a, b)) std::cout << "YES" << std::endl; else std::cout << "NO" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Sorting both strings and comparing them checks if they have the same letters in the same frequency.
✗ Incorrect
The function sorts both strings and compares them. Since "listen" and "silent" have the same letters, the output is YES.
❓ Predict Output
intermediateOutput of Frequency Count Anagram Check
What is the output of this C++ code that checks if two strings are anagrams by counting character frequencies?
DSA C++
bool areAnagrams(std::string s1, std::string s2) { if (s1.length() != s2.length()) return false; int count[256] = {0}; for (int i = 0; i < s1.length(); i++) { count[(unsigned char)s1[i]]++; count[(unsigned char)s2[i]]--; } for (int i = 0; i < 256; i++) { if (count[i] != 0) return false; } return true; } int main() { std::string a = "triangle"; std::string b = "integral"; if (areAnagrams(a, b)) std::cout << "YES" << std::endl; else std::cout << "NO" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Counting characters in both strings and comparing counts helps verify anagrams.
✗ Incorrect
The code counts characters in both strings simultaneously. Since counts balance out, it prints YES.
🔧 Debug
advancedIdentify the Bug in Anagram Check Using Map
What error will this C++ code produce when checking if two strings are anagrams using a map?
DSA C++
#include <iostream> #include <map> #include <string> bool areAnagrams(std::string s1, std::string s2) { std::map<char, int> freq; for (char c : s1) freq[c]++; for (char c : s2) freq[c]--; for (auto p : freq) { if (p.second != 0) return false; } return true; } int main() { std::string a = "apple"; std::string b = "papel"; if (areAnagrams(a, b)) std::cout << "YES" << std::endl; else std::cout << "NO" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the map counts balance out after incrementing and decrementing.
✗ Incorrect
The code correctly counts characters using a map and compares frequencies. Since "apple" and "papel" are anagrams, it prints YES.
❓ Predict Output
advancedOutput of Case-Sensitive Anagram Check
What is the output of this C++ code that checks if two strings are anagrams considering case sensitivity?
DSA C++
bool areAnagrams(std::string s1, std::string s2) { if (s1.length() != s2.length()) return false; int count[256] = {0}; for (int i = 0; i < s1.length(); i++) { count[(unsigned char)s1[i]]++; count[(unsigned char)s2[i]]--; } for (int i = 0; i < 256; i++) { if (count[i] != 0) return false; } return true; } int main() { std::string a = "Listen"; std::string b = "Silent"; if (areAnagrams(a, b)) std::cout << "YES" << std::endl; else std::cout << "NO" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Uppercase and lowercase letters have different ASCII codes, so counts differ.
✗ Incorrect
Because 'L' and 'l' are different characters, the counts do not balance, so the output is NO.
🧠 Conceptual
expertMost Efficient Approach for Large Unicode Strings Anagram Check
Which approach is best to check if two very large Unicode strings are anagrams efficiently in C++?
Attempts:
2 left
💡 Hint
Unicode characters exceed 256, so fixed arrays won't cover all characters.
✗ Incorrect
For large Unicode strings, a hash map counting frequencies of code points is efficient and scalable, unlike fixed arrays or sorting which are costly.
