Challenge - 5 Problems
Prefix Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of prefix count function
What is the output of the following C code that counts how many words start with the prefix "pre"?
DSA C
#include <stdio.h> #include <string.h> int countPrefix(char *words[], int n, char *prefix) { int count = 0; int prefixLen = strlen(prefix); for (int i = 0; i < n; i++) { if (strncmp(words[i], prefix, prefixLen) == 0) { count++; } } return count; } int main() { char *words[] = {"prefix", "preach", "prevent", "post", "prepare", "presentation"}; int n = 6; int result = countPrefix(words, n, "pre"); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Count how many words start exactly with "pre".
✗ Incorrect
The words starting with "pre" are: "prefix", "preach", "prevent", "prepare", and "presentation". That makes 5 words.
🧠 Conceptual
intermediateUnderstanding prefix length in counting words
Why is it important to use the length of the prefix when comparing words to count how many start with that prefix?
Attempts:
2 left
💡 Hint
Think about how prefix matching works in strings.
✗ Incorrect
Using the prefix length ensures only the start of each word is compared to the prefix, so words starting with the prefix are counted correctly.
🔧 Debug
advancedIdentify the bug in prefix counting code
What error will occur when running this code to count words starting with a prefix?
DSA C
#include <stdio.h> #include <string.h> int countPrefix(char *words[], int n, char *prefix) { int count = 0; for (int i = 0; i < n; i++) { if (strncmp(words[i], prefix, strlen(words[i])) == 0) { count++; } } return count; } int main() { char *words[] = {"apple", "app", "application"}; int n = 3; int result = countPrefix(words, n, "app"); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check the third argument of strncmp carefully.
✗ Incorrect
The code uses strlen(words[i]) instead of strlen(prefix), so it compares too many characters, leading to wrong results.
❓ Predict Output
advancedOutput of prefix count with empty prefix
What is the output of this code when counting words starting with an empty prefix string?
DSA C
#include <stdio.h> #include <string.h> int countPrefix(char *words[], int n, char *prefix) { int count = 0; int prefixLen = strlen(prefix); for (int i = 0; i < n; i++) { if (strncmp(words[i], prefix, prefixLen) == 0) { count++; } } return count; } int main() { char *words[] = {"cat", "dog", "", "car"}; int n = 4; int result = countPrefix(words, n, ""); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
An empty prefix matches the start of every string.
✗ Incorrect
Since the prefix is empty, every word starts with it, so all 4 words are counted.
🧠 Conceptual
expertWhy use strncmp instead of strcmp for prefix counting?
Why is strncmp preferred over strcmp when counting how many words start with a given prefix?
Attempts:
2 left
💡 Hint
Think about how prefix matching works with string comparison functions.
✗ Incorrect
strncmp compares only the first n characters, which is exactly what is needed to check if a word starts with a prefix. strcmp compares full strings and would not work for prefixes.
