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 Java
if (strs.length == 0) return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null instead of an empty string.
Trying to access strs[0] when 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 with the first string in the array.
DSA Java
String prefix = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using strs[1] which may cause index errors.
Starting with an empty string which won't help find common prefix.
✗ 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.
DSA Java
while (strs[i].indexOf(prefix) != [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for -1, which only checks existence anywhere, not at the start.
Using 1 or i which are incorrect indices.
✗ Incorrect
indexOf(prefix) returns 0 if the prefix is at the start of strs[i]. The loop continues while it is not 0.
4fill in blank
hardFill both blanks to shorten the prefix by one character from the end inside the loop.
DSA Java
prefix = prefix[1](0, prefix[2]() - 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using charAt or indexOf instead of substring.
Not subtracting 1 from length.
✗ Incorrect
We use substring(0, length - 1) to remove the last character from prefix.
5fill in blank
hardFill all three blanks to complete the for loop iterating over the strings starting from index 1.
DSA Java
for (int i = [1]; i < [2]; i[3]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 which repeats prefix initialization.
Using i-- or i+=2 which skips elements.
✗ Incorrect
We start from 1 to skip the first string, loop until strs.length, and increment i by 1 each time.
