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
intermediate2:00remaining
Output of first non-repeating character index
What is the output of this C code that finds the index of the first non-repeating character in the string?
DSA C
#include <stdio.h> #include <string.h> int firstNonRepeatingChar(char *str) { int count[256] = {0}; int i; for (i = 0; str[i]; i++) { count[(unsigned char)str[i]]++; } for (i = 0; str[i]; i++) { if (count[(unsigned char)str[i]] == 1) { return i; } } return -1; } int main() { char str[] = "swiss"; int index = firstNonRepeatingChar(str); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
Check the frequency of each character and find the first with count 1.
✗ Incorrect
The string "swiss" has characters: s(3), w(1), i(1). The first non-repeating character is 'w' at index 1.
❓ Predict Output
intermediate2:00remaining
Output of first non-repeating character index with different string
What is the output of this C code that finds the index of the first non-repeating character in the string?
DSA C
#include <stdio.h> #include <string.h> int firstNonRepeatingChar(char *str) { int count[256] = {0}; int i; for (i = 0; str[i]; i++) { count[(unsigned char)str[i]]++; } for (i = 0; str[i]; i++) { if (count[(unsigned char)str[i]] == 1) { return i; } } return -1; } int main() { char str[] = "aabbcc"; int index = firstNonRepeatingChar(str); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
All characters repeat, so no non-repeating character.
✗ Incorrect
All characters appear twice, so the function returns -1 indicating no non-repeating character.
🔧 Debug
advanced2:00remaining
Identify the error in this code for first non-repeating character
What error will this C code produce when compiled or run?
DSA C
#include <stdio.h> int firstNonRepeatingChar(char *str) { int count[256]; int i; for (i = 0; i < 256; i++) { count[i] = 0; } for (i = 0; str[i]; i++) { count[(unsigned char)str[i]]++; } for (i = 0; str[i]; i++) { if (count[(unsigned char)str[i]] == 1) { return i; } } return -1; } int main() { char str[] = "hello"; int index = firstNonRepeatingChar(str); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
Check how the array is initialized and how characters are used as indices.
✗ Incorrect
The array count is initialized manually with a loop. For "hello", 'h' appears once at index 0, so output is 0. Characters have positive ASCII, no negative index issue. No error.
❓ Predict Output
advanced2:00remaining
Output of first non-repeating character index with mixed case
What is the output of this C code that finds the index of the first non-repeating character in the string with mixed case letters?
DSA C
#include <stdio.h> #include <string.h> int firstNonRepeatingChar(char *str) { int count[256] = {0}; int i; for (i = 0; str[i]; i++) { count[(unsigned char)str[i]]++; } for (i = 0; str[i]; i++) { if (count[(unsigned char)str[i]] == 1) { return i; } } return -1; } int main() { char str[] = "aAbBABac"; int index = firstNonRepeatingChar(str); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
Remember that uppercase and lowercase letters have different ASCII codes.
✗ Incorrect
String "aAbBABac": a(3), A(2), b(1 at index 2), B(2), c(1). First non-repeating is 'b' at index 2.
🧠 Conceptual
expert2:00remaining
Why use a hash array for first non-repeating character?
Why is using a hash array (like int count[256]) efficient for finding the first non-repeating character in a string?
Attempts:
2 left
💡 Hint
Think about how many times the string is scanned and the size of the frequency array.
✗ Incorrect
Using a fixed-size array for ASCII characters allows counting frequencies in one pass (O(n)) and then finding the first unique character in another pass, both linear time. The space is constant (256 ints).
