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 for given strings
What is the output of the following Java code that finds the longest common prefix among an array of strings?
DSA Java
public class Solution { public static String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return ""; } } return prefix; } public static void main(String[] args) { String[] input = {"flower", "flow", "flight"}; System.out.println(longestCommonPrefix(input)); } }
Attempts:
2 left
💡 Hint
Check how the prefix is shortened when it doesn't match the start of each string.
✗ Incorrect
The code starts with the first string as prefix and shortens it until it matches the start of all strings. For the input, "flower", "flow", and "flight", the longest common prefix is "fl".
🧠 Conceptual
intermediateLongest Common Prefix with empty string in array
What will be the output of the longest common prefix function if the input array contains an empty string?
Attempts:
2 left
💡 Hint
If any string is empty, what prefix can all strings share?
✗ Incorrect
If any string in the array is empty, the longest common prefix must be empty because no prefix can be longer than an empty string.
🔧 Debug
advancedIdentify the error in longest common prefix code
What error will the following code produce when run with input {"dog", "racecar", "car"}?
DSA Java
public class Solution { public static String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (!strs[i].startsWith(prefix)) { prefix = prefix.substring(1); if (prefix.isEmpty()) return ""; } } return prefix; } public static void main(String[] args) { String[] input = {"dog", "racecar", "car"}; System.out.println(longestCommonPrefix(input)); } }
Attempts:
2 left
💡 Hint
Check how prefix is shortened in the while loop.
✗ Incorrect
The code shortens prefix by removing the first character each time. Since no common prefix exists, prefix eventually becomes empty and returns "" safely.
❓ Predict Output
advancedOutput of longest common prefix with identical strings
What is the output of the following code when all strings in the array are identical?
DSA Java
public class Solution { public static String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return ""; } } return prefix; } public static void main(String[] args) { String[] input = {"test", "test", "test"}; System.out.println(longestCommonPrefix(input)); } }
Attempts:
2 left
💡 Hint
If all strings are the same, what is their longest common prefix?
✗ Incorrect
Since all strings are identical, the longest common prefix is the entire string itself.
🧠 Conceptual
expertTime complexity of longest common prefix algorithm
What is the time complexity of the 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 total across all strings.
✗ Incorrect
The algorithm compares characters until mismatch or end of shortest string. In worst case, it checks all characters once, so time is proportional to total characters S.
