Bird
Raised Fist0
DSA C++programming~20 mins

Anagram Check Techniques in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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;
}
ACompilation Error
BNO
CYES
DRuntime Error
Attempts:
2 left
💡 Hint
Sorting both strings and comparing them checks if they have the same letters in the same frequency.
❓ Predict Output
intermediate
2:00remaining
Output 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;
}
AYES
BNO
CCompilation Error
DRuntime Error
Attempts:
2 left
💡 Hint
Counting characters in both strings and comparing counts helps verify anagrams.
🔧 Debug
advanced
2:00remaining
Identify 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;
}
ARuntime Error
BNO
CCompilation Error
DYES
Attempts:
2 left
💡 Hint
Check if the map counts balance out after incrementing and decrementing.
❓ Predict Output
advanced
2:00remaining
Output 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;
}
ANO
BYES
CCompilation Error
DRuntime Error
Attempts:
2 left
💡 Hint
Uppercase and lowercase letters have different ASCII codes, so counts differ.
🧠 Conceptual
expert
3:00remaining
Most 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++?
ASort both strings and compare them
BUse a hash map to count frequencies of Unicode code points
CUse a fixed-size array of 256 counts for characters
DCompare strings character by character without any preprocessing
Attempts:
2 left
💡 Hint
Unicode characters exceed 256, so fixed arrays won't cover all characters.

Practice

(1/5)
1. What is the main idea behind checking if two strings are anagrams?
easy
A. Both strings have the same characters with the same frequency
B. Both strings have the same length only
C. Both strings start with the same character
D. Both strings contain only alphabets

Solution

  1. Step 1: Understand what an anagram means

    An anagram means two strings contain the same characters with the same frequency, but possibly in different orders.
  2. Step 2: Identify the key condition for anagram check

    The key condition is that both strings must have the same characters appearing the same number of times.
  3. Final Answer:

    Both strings have the same characters with the same frequency -> Option A
  4. Quick Check:

    Anagram = Same characters & frequency [OK]
Hint: Check character counts match for both strings [OK]
Common Mistakes:
  • Only checking string length
  • Ignoring character frequency
  • Assuming order matters
  • Checking only first character
2. Which of the following C++ code snippets correctly sorts a string s to help check an anagram?
easy
A. std::sort(s.begin(), s.end());
B. sort(s, s+length);
C. std::sort(s, s+strlen(s));
D. sort(s.begin(), s.end(), compare);

Solution

  1. Step 1: Identify correct sorting syntax for std::string

    For std::string, sorting is done using iterators: s.begin() and s.end().
  2. Step 2: Check each option for correctness

    std::sort(s.begin(), s.end()); uses std::sort(s.begin(), s.end()); which is correct. sort(s, s+length); and C use pointer arithmetic which is invalid for std::string. sort(s.begin(), s.end(), compare); uses a comparator but is incomplete without defining it.
  3. Final Answer:

    std::sort(s.begin(), s.end()); -> Option A
  4. Quick Check:

    Sort string with iterators = std::sort(s.begin(), s.end()) [OK]
Hint: Use s.begin() and s.end() with std::sort for strings [OK]
Common Mistakes:
  • Using pointer arithmetic on std::string
  • Forgetting std:: namespace
  • Using incomplete comparator
  • Sorting only part of the string
3. What will be the output of the following C++ code snippet?
std::string s1 = "listen";
std::string s2 = "silent";
std::sort(s1.begin(), s1.end());
std::sort(s2.begin(), s2.end());
if (s1 == s2) std::cout << "Anagrams";
else std::cout << "Not Anagrams";
medium
A. Not Anagrams
B. Anagrams
C. Compilation Error
D. Runtime Error

Solution

  1. Step 1: Sort both strings

    After sorting, s1 becomes "eilnst" and s2 becomes "eilnst".
  2. Step 2: Compare sorted strings

    Since both sorted strings are equal, the condition s1 == s2 is true, so it prints "Anagrams".
  3. Final Answer:

    Anagrams -> Option B
  4. Quick Check:

    Sorted strings equal = Anagrams [OK]
Hint: Sort both strings and compare for equality [OK]
Common Mistakes:
  • Not sorting strings before comparison
  • Confusing output with original strings
  • Assuming order matters
  • Ignoring case sensitivity
4. Identify the error in the following C++ code for checking anagrams:
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.compare(s2);
}
medium
A. Length check is incorrect
B. Missing header <algorithm> for sort
C. Strings should be passed by reference
D. Using s1.compare(s2) returns int, not bool

Solution

  1. Step 1: Understand std::string::compare return type

    The compare function returns 0 if strings are equal, else non-zero integer.
  2. Step 2: Check return type of function

    The function returns bool but returns s1.compare(s2) which is int, so it will not return correct boolean value.
  3. Final Answer:

    Using s1.compare(s2) returns int, not bool -> Option D
  4. Quick Check:

    compare() returns int, need == 0 for bool [OK]
Hint: Use (s1.compare(s2) == 0) to get bool result [OK]
Common Mistakes:
  • Returning compare() directly as bool
  • Ignoring header includes
  • Passing strings by value unnecessarily
  • Incorrect length check logic
5. Given two strings, which approach is best to check anagrams efficiently when strings contain only lowercase English letters?
hard
A. Sort both strings and compare
B. Use nested loops to check each character presence
C. Use a frequency array of size 26 to count characters
D. Convert strings to uppercase and compare

Solution

  1. Step 1: Analyze sorting approach

    Sorting takes O(n log n) time, which is good but not optimal for fixed character set.
  2. Step 2: Analyze frequency array approach

    Using a frequency array of size 26 counts characters in O(n) time, which is faster and uses constant extra space.
  3. Step 3: Compare other options

    Nested loops are inefficient O(n^2). Converting case does not check anagram correctness.
  4. Final Answer:

    Use a frequency array of size 26 to count characters -> Option C
  5. Quick Check:

    Frequency array = O(n) efficient anagram check [OK]
Hint: Count chars with array for O(n) anagram check [OK]
Common Mistakes:
  • Using sorting for large strings unnecessarily
  • Using nested loops causing slow performance
  • Ignoring character set constraints
  • Assuming case conversion solves anagram check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard