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 C# code that finds the longest common prefix among an array of strings?
DSA C#
using System; class Program { 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 == "") return ""; } } return prefix; } static void Main() { string[] input = {"flower", "flow", "flight"}; Console.WriteLine(LongestCommonPrefix(input)); } }
Attempts:
2 left
💡 Hint
Check the common starting letters of all strings.
✗ Incorrect
The longest common prefix among "flower", "flow", and "flight" is "fl" because all three strings start with these two letters.
❓ Predict Output
intermediateLongest Common Prefix with No Common Letters
What will be printed by this C# program that finds the longest common prefix?
DSA C#
using System; class Program { 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 == "") return ""; } } return prefix; } static void Main() { string[] input = {"dog", "racecar", "car"}; Console.WriteLine(LongestCommonPrefix(input)); } }
Attempts:
2 left
💡 Hint
Check if any letters are common at the start of all strings.
✗ Incorrect
There is no common prefix among "dog", "racecar", and "car" so the function returns an empty string.
🧠 Conceptual
advancedUnderstanding the Algorithm's Loop Behavior
In the longest common prefix algorithm, why does the inner while loop reduce the prefix length until it matches the start of the current string?
Attempts:
2 left
💡 Hint
Think about what the prefix must represent after each iteration.
✗ Incorrect
The prefix must be common to all strings processed so far, so it is shortened until it matches the start of the current string.
❓ Predict Output
advancedOutput of Longest Common Prefix with Single String
What is the output of this C# program when the input array has only one string?
DSA C#
using System; class Program { 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 == "") return ""; } } return prefix; } static void Main() { string[] input = {"single"}; Console.WriteLine(LongestCommonPrefix(input)); } }
Attempts:
2 left
💡 Hint
With only one string, the prefix is the string itself.
✗ Incorrect
If there is only one string, the longest common prefix is the string itself.
🧠 Conceptual
expertTime Complexity of Longest Common Prefix Algorithm
What is the time complexity of the longest common prefix algorithm that compares strings by trimming the prefix until it matches all strings?
Attempts:
2 left
💡 Hint
Consider how many characters are compared in total across all strings.
✗ Incorrect
The algorithm compares each character of the prefix with each string, leading to O(N * M) complexity.
