Challenge - 5 Problems
Longest Common Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Longest Common Prefix Function
What is the output of the following C code that finds the longest common prefix of an array of strings?
DSA C
#include <stdio.h> #include <string.h> char* longestCommonPrefix(char* strs[], int strsSize) { if (strsSize == 0) return ""; for (int i = 0; strs[0][i]; i++) { char c = strs[0][i]; for (int j = 1; j < strsSize; j++) { if (strs[j][i] != c || strs[j][i] == '\0') { strs[0][i] = '\0'; return strs[0]; } } } return strs[0]; } int main() { char str1[] = "flower"; char str2[] = "flow"; char str3[] = "flight"; char* arr[] = {str1, str2, str3}; printf("%s\n", longestCommonPrefix(arr, 3)); return 0; }
Attempts:
2 left
💡 Hint
Check characters one by one across all strings until a mismatch or string end.
✗ Incorrect
The function compares characters of the first string with all others. It stops at the first mismatch or string end. Here, 'fl' is common in all three strings.
🧠 Conceptual
intermediate1:30remaining
Longest Common Prefix Edge Case
If the input array of strings is {"dog", "racecar", "car"}, what is the longest common prefix?
Attempts:
2 left
💡 Hint
Check if any character is common at the start of all strings.
✗ Incorrect
No common starting character exists among all strings, so the longest common prefix is empty.
🔧 Debug
advanced2:00remaining
Identify the Bug in Longest Common Prefix Code
What error will occur when running this code snippet?
DSA C
#include <stdio.h> #include <string.h> char* longestCommonPrefix(char* strs[], int strsSize) { if (strsSize == 0) return ""; for (int i = 0; strs[0][i]; i++) { char c = strs[0][i]; for (int j = 1; j < strsSize; j++) { if (strs[j][i] != c) { strs[0][i] = '\0'; return strs[0]; } } } return strs[0]; } int main() { char* arr[] = {"apple", "ape", "april"}; printf("%s\n", longestCommonPrefix(arr, 3)); return 0; }
Attempts:
2 left
💡 Hint
String literals are stored in read-only memory and cannot be changed.
✗ Incorrect
The code tries to modify a string literal ("apple" etc.) which is undefined behavior and usually causes a segmentation fault.
❓ Predict Output
advanced2:00remaining
Output of Longest Common Prefix with Empty String
What is the output of this code?
DSA C
#include <stdio.h> #include <string.h> char* longestCommonPrefix(char* strs[], int strsSize) { if (strsSize == 0) return ""; for (int i = 0; strs[0][i]; i++) { char c = strs[0][i]; for (int j = 1; j < strsSize; j++) { if (strs[j][i] != c || strs[j][i] == '\0') { strs[0][i] = '\0'; return strs[0]; } } } return strs[0]; } int main() { char str1[] = ""; char str2[] = "abc"; char str3[] = "abcd"; char* arr[] = {str1, str2, str3}; printf("%s\n", longestCommonPrefix(arr, 3)); return 0; }
Attempts:
2 left
💡 Hint
If the first string is empty, the prefix must be empty.
✗ Incorrect
The first string is empty, so the longest common prefix cannot be longer than empty string.
🧠 Conceptual
expert1:30remaining
Longest Common Prefix Algorithm Complexity
What is the time complexity of the standard longest common prefix algorithm that compares characters one by one across all strings?
Attempts:
2 left
💡 Hint
The algorithm checks each character of the shortest string against all strings.
✗ Incorrect
The algorithm compares up to M characters (shortest string length) for each of the N strings, resulting in O(N*M) complexity.
