Challenge - 5 Problems
First Non Repeating Character Master
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#
using System; using System.Collections.Generic; class Program { static int FirstNonRepeatingChar(string s) { var counts = new Dictionary<char, int>(); foreach (var c in s) { if (counts.ContainsKey(c)) counts[c]++; else counts[c] = 1; } for (int i = 0; i < s.Length; i++) { if (counts[s[i]] == 1) return i; } return -1; } static void Main() { Console.WriteLine(FirstNonRepeatingChar("aabc")); } }
Attempts:
2 left
š” Hint
Check the frequency of each character and find the first with count 1.
ā Incorrect
The string "aabc" has characters: a(2 times), b(1 time), c(1 time). The first non-repeating character is 'b' at index 2.
ā Predict Output
intermediateOutput of First Non Repeating Character Index with all repeating
What is the output of the following C# code when all characters repeat?
DSA C#
using System; using System.Collections.Generic; class Program { static int FirstNonRepeatingChar(string s) { var counts = new Dictionary<char, int>(); foreach (var c in s) { if (counts.ContainsKey(c)) counts[c]++; else counts[c] = 1; } for (int i = 0; i < s.Length; i++) { if (counts[s[i]] == 1) return i; } return -1; } static void Main() { Console.WriteLine(FirstNonRepeatingChar("aabbcc")); } }
Attempts:
2 left
š” Hint
If no character appears once, the function returns -1.
ā Incorrect
All characters in "aabbcc" appear twice, so no non-repeating character exists. The function returns -1.
š§ Debug
advancedIdentify the error in this First Non Repeating Character code
What error does this C# code produce when run?
DSA C#
using System; using System.Collections.Generic; class Program { static int FirstNonRepeatingChar(string s) { var counts = new Dictionary<char, int>(); foreach (var c in s) { counts[c] += 1; } for (int i = 0; i < s.Length; i++) { if (counts[s[i]] == 1) return i; } return -1; } static void Main() { Console.WriteLine(FirstNonRepeatingChar("hello")); } }
Attempts:
2 left
š” Hint
Check how dictionary keys are accessed and updated.
ā Incorrect
The code tries to increment counts[c] without initializing counts[c] first. This causes a KeyNotFoundException because the key does not exist yet.
ā Predict Output
advancedOutput of First Non Repeating Character with Unicode characters
What is the output of this C# code when the input string contains Unicode characters?
DSA C#
using System; using System.Collections.Generic; class Program { static int FirstNonRepeatingChar(string s) { var counts = new Dictionary<char, int>(); foreach (var c in s) { if (counts.ContainsKey(c)) counts[c]++; else counts[c] = 1; } for (int i = 0; i < s.Length; i++) { if (counts[s[i]] == 1) return i; } return -1; } static void Main() { Console.WriteLine(FirstNonRepeatingChar("aƔa")); } }
Attempts:
2 left
š” Hint
Unicode characters are treated as distinct chars.
ā Incorrect
The string "aƔa" has 'a' twice and 'Ɣ' once at index 1. So the first non-repeating character is at index 1.
š§ Conceptual
expertWhy use a hash map for First Non Repeating Character?
Why is using a hash map (dictionary) the best approach to find the first non-repeating character in a string?
Attempts:
2 left
š” Hint
Think about how to count and check characters efficiently.
ā Incorrect
A hash map stores counts of each character in O(n) time. Then a second pass finds the first character with count 1. This is efficient and simple.
