Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return an empty string if the input array is empty.
DSA C#
if (strs.Length == 0) return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null instead of an empty string.
Returning the first element without checking if array is empty.
✗ Incorrect
If the array is empty, the longest common prefix is an empty string "".
2fill in blank
mediumComplete the code to initialize the prefix variable with the first string in the array.
DSA C#
string prefix = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using strs[1] which may not exist if array has only one element.
Initializing prefix as empty string which is less efficient.
✗ Incorrect
We start with the first string as the initial prefix to compare with others.
3fill in blank
hardFix the error in the while loop condition to check prefix matching correctly.
DSA C#
while (strs[i].IndexOf(prefix) != [1]) { prefix = prefix.Substring(0, prefix.Length - 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for -1 which means prefix not found anywhere, but we want prefix at start.
Using prefix.Length which is invalid for IndexOf comparison.
✗ Incorrect
IndexOf returns 0 if prefix is at the start of the string. We want to loop while prefix is NOT at start.
4fill in blank
hardFill both blanks to complete the for loop iterating over the strings starting from second element.
DSA C#
for (int i = [1]; i < [2]; i++) { /* loop body */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 which repeats prefix initialization string.
Using strs.Length - 1 which misses the last string.
✗ Incorrect
We start from index 1 to compare with prefix and loop until the end of the array.
5fill in blank
hardFill the blank to return the final longest common prefix after the loop.
DSA C#
return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning strs[0] which may not be the common prefix after adjustments.
Returning null or empty string incorrectly.
✗ Incorrect
After adjusting prefix in the loop, we return it as the longest common prefix.
