Bird
0
0
DSA Cprogramming~20 mins

Longest Common Prefix in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Longest Common Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A"fl"
B"flow"
C"f"
D""
Attempts:
2 left
💡 Hint
Check characters one by one across all strings until a mismatch or string end.
🧠 Conceptual
intermediate
1:30remaining
Longest Common Prefix Edge Case
If the input array of strings is {"dog", "racecar", "car"}, what is the longest common prefix?
A"d"
B""
C"c"
D"r"
Attempts:
2 left
💡 Hint
Check if any character is common at the start of all strings.
🔧 Debug
advanced
2: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;
}
ACompilation error: cannot assign to array element
BNo error, prints "ap"
CSegmentation fault due to modifying string literal
DRuntime error: null pointer dereference
Attempts:
2 left
💡 Hint
String literals are stored in read-only memory and cannot be changed.
Predict Output
advanced
2: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;
}
A""
B"a"
C"abc"
D"abcd"
Attempts:
2 left
💡 Hint
If the first string is empty, the prefix must be empty.
🧠 Conceptual
expert
1: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?
AO(N + M), where N is number of strings and M is total length of all strings
BO(N^2), where N is number of strings
CO(M^2), where M is length of longest string
DO(N * M), where N is number of strings and M is length of shortest string
Attempts:
2 left
💡 Hint
The algorithm checks each character of the shortest string against all strings.