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 TypeScript code that finds the longest common prefix among strings?
DSA Typescript
function longestCommonPrefix(strs: string[]): string {
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
Check the common starting letters of all strings.
✗ Incorrect
The function starts with the first string as the prefix and reduces it until all strings start with it. Here, "fl" is the longest prefix common to all.
❓ Predict Output
intermediateLongest Common Prefix with No Common Prefix
What does the following code print when there is no common prefix?
DSA Typescript
function longestCommonPrefix(strs: string[]): string {
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
If no letters match at the start, the prefix is empty.
✗ Incorrect
None of the strings share a common starting letter, so the longest common prefix is an empty string.
🔧 Debug
advancedIdentify the Error in Longest Common Prefix Implementation
What error will this TypeScript code produce when run?
DSA Typescript
function longestCommonPrefix(strs: string[]): string {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].startsWith(prefix) === false) {
prefix = prefix.slice(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
console.log(longestCommonPrefix(["flower", "flow", "flight"]));Attempts:
2 left
💡 Hint
Check how prefix is shortened inside the loop.
✗ Incorrect
The code incorrectly removes the first character of prefix instead of the last, so it never finds the correct prefix and ends up empty.
🧠 Conceptual
advancedLongest Common Prefix Algorithm Complexity
What is the time complexity of the standard longest common prefix algorithm that compares characters one by one among all strings?
Attempts:
2 left
💡 Hint
Consider worst case comparing each character of each string.
✗ Incorrect
The algorithm compares each character of the shortest string with all other strings, leading to O(N * M) complexity.
🚀 Application
expertLongest Common Prefix with Unicode Characters
Given the following TypeScript code, what is the output when the input strings contain Unicode characters?
DSA Typescript
function longestCommonPrefix(strs: string[]): string {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (!strs[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
console.log(longestCommonPrefix(["café", "caféteria", "cafè"]));Attempts:
2 left
💡 Hint
Unicode characters with accents may differ even if visually similar.
✗ Incorrect
The longest common prefix is "caf" because the accented characters differ, so the prefix stops before them.
