Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if two strings have the same length.
DSA C++
bool areSameLength(const string& s1, const string& s2) {
return s1.length() [1] s2.length();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment, not comparison.
Using '!=' checks for inequality, not equality.
✗ Incorrect
We use '==' to check if the lengths of both strings are equal.
2fill in blank
mediumComplete the code to sort a string using the standard library.
DSA C++
void sortString(string& s) {
sort(s.[1](), s.end());
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start()' or 'first()' which are not valid string methods.
Using 'front()' returns a character, not an iterator.
✗ Incorrect
The 'begin()' function returns an iterator to the start of the string.
3fill in blank
hardFix the error in the code to count character frequencies correctly.
DSA C++
bool areAnagrams(const string& s1, const string& s2) {
if (s1.length() != s2.length()) return false;
unordered_map<char, int> freq;
for (char c : s1) freq[c] += 1;
for (char c : s2) {
if (freq[c] == 0) return false;
freq[c] [1] 1;
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+=' increases frequency incorrectly.
Using '=' assigns a value instead of modifying.
✗ Incorrect
We subtract 1 from the frequency for each character in the second string.
4fill in blank
hardFill both blanks to create a map comprehension that counts characters only if they appear more than once.
DSA C++
unordered_map<char, int> freq = [1]; for (char c : s) { freq[c] += 1; } for (auto& p : freq) { if (p.second [2] 1) { cout << p.first << ": " << p.second << endl; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '[]' which is invalid for map initialization.
Using '<=' which checks for less or equal, not more than one.
✗ Incorrect
We initialize an empty map with '{}', and check if frequency is greater than 1.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that filters characters with frequency greater than zero.
DSA C++
unordered_map<char, int> filtered = [1]; for (auto& p : freq) { if (p.second [2] 0) { filtered[p.[3]] = p.second; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '[]' instead of '{}' for map initialization.
Using 'second' instead of 'first' to access the key.
✗ Incorrect
Initialize empty map '{}', check if frequency > 0, and use 'first' to access the key.
