Challenge - 5 Problems
Hash Mastery: First Non Repeating Character
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of First Non Repeating Character Index
What is the output of the following C++ code that finds the index of the first non-repeating character in the string?
DSA C++
int firstNonRepeatingCharIndex(const std::string& s) { std::unordered_map<char, int> freq; for (char c : s) { freq[c]++; } for (int i = 0; i < s.size(); i++) { if (freq[s[i]] == 1) { return i; } } return -1; } int main() { std::string s = "swiss"; std::cout << firstNonRepeatingCharIndex(s) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the frequency of each character and find the first with frequency 1.
✗ Incorrect
The string "swiss" has characters: s(3 times), w(1 time), i(1 time). The first non-repeating character is 'w' at index 1.
❓ Predict Output
intermediateOutput when no non-repeating character exists
What is the output of the code when the input string has all repeating characters?
DSA C++
int firstNonRepeatingCharIndex(const std::string& s) { std::unordered_map<char, int> freq; for (char c : s) { freq[c]++; } for (int i = 0; i < s.size(); i++) { if (freq[s[i]] == 1) { return i; } } return -1; } int main() { std::string s = "aabbcc"; std::cout << firstNonRepeatingCharIndex(s) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
If no character appears once, the function returns -1.
✗ Incorrect
All characters repeat, so no non-repeating character exists. The function returns -1.
🔧 Debug
advancedIdentify the error in this hash-based first non-repeating character code
What error will this code produce when run?
DSA C++
int firstNonRepeatingCharIndex(const std::string& s) { std::unordered_map<char, int> freq; for (int i = 0; i <= s.size(); i++) { freq[s[i]]++; } for (int i = 0; i < s.size(); i++) { if (freq[s[i]] == 1) { return i; } } return -1; }
Attempts:
2 left
💡 Hint
Check the loop boundary in the first for loop.
✗ Incorrect
The loop runs from i=0 to i<=s.size(), which accesses s[s.size()] causing out_of_range error.
🧠 Conceptual
advancedWhy use a hash map for first non-repeating character?
Why is a hash map (unordered_map) used to find the first non-repeating character in a string?
Attempts:
2 left
💡 Hint
Think about how to count characters quickly.
✗ Incorrect
Hash map stores frequency counts allowing O(1) average lookup to check if a character repeats.
❓ Predict Output
expertOutput of modified code with character removal
What is the output of this code that removes characters from the hash map after counting?
DSA C++
int firstNonRepeatingCharIndex(const std::string& s) { std::unordered_map<char, int> freq; for (char c : s) { if (freq.find(c) == freq.end()) { freq[c] = 1; } else { freq.erase(c); } } for (int i = 0; i < s.size(); i++) { if (freq.find(s[i]) != freq.end()) { return i; } } return -1; } int main() { std::string s = "aabbcdde"; std::cout << firstNonRepeatingCharIndex(s) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Characters repeated are erased, so only unique remain in map.
✗ Incorrect
Characters 'a','b','d' repeat and are erased. Remaining unique are 'c' at index 4 and 'e' at index 7. The first unique is 'c' at index 4.
