Challenge - 5 Problems
Longest Common Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Longest Common Prefix Function
What is the output of the following JavaScript code that finds the longest common prefix among strings?
DSA Javascript
function longestCommonPrefix(strs) {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
console.log(longestCommonPrefix(["flower", "flow", "flight"]));Attempts:
2 left
💡 Hint
Think about the common starting letters in all strings.
✗ Incorrect
The function compares the first string with each subsequent string, reducing the prefix until it matches the start of all strings. Here, "fl" is the longest common prefix.
❓ Predict Output
intermediateLongest Common Prefix with Empty String in Array
What will the following code output when the input array contains an empty string?
DSA Javascript
function longestCommonPrefix(strs) {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
console.log(longestCommonPrefix(["", "b", "c"]));Attempts:
2 left
💡 Hint
An empty string means no common prefix can exist.
✗ Incorrect
Since the first string is empty, the prefix starts empty and remains empty, so the output is an empty string.
❓ Predict Output
advancedOutput of Longest Common Prefix with Single Character Strings
What is the output of this code when all strings are single characters?
DSA Javascript
function longestCommonPrefix(strs) {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
console.log(longestCommonPrefix(["a", "a", "a"]));Attempts:
2 left
💡 Hint
All strings are the same single character.
✗ Incorrect
Since all strings are "a", the longest common prefix is "a".
❓ Predict Output
advancedLongest Common Prefix with No Common Prefix
What will this code output when there is no common prefix among the strings?
DSA Javascript
function longestCommonPrefix(strs) {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
console.log(longestCommonPrefix(["dog", "racecar", "car"]));Attempts:
2 left
💡 Hint
Check if any starting letters match.
✗ Incorrect
No strings share a common starting letter, so the longest common prefix is empty.
🧠 Conceptual
expertTime Complexity of Longest Common Prefix Algorithm
What is the time complexity of the standard longest common prefix algorithm that compares characters of each string one by one until mismatch?
Attempts:
2 left
💡 Hint
Consider how many characters are compared in the worst case.
✗ Incorrect
The algorithm compares each character of the shortest string with all other strings, so complexity is O(N * M).
