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 Typescript
if (strs.length === 0) return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null or undefined instead of an empty string.
Returning 0 which is not a string.
✗ 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 Typescript
let prefix = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting with an empty string which will always result in empty prefix.
Using the last string or second string which is not standard.
✗ 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 if the current string starts with the prefix.
DSA Typescript
while (!strs[i].[1](prefix)) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using endsWith which checks the suffix, not prefix.
Using includes or indexOf which check anywhere in the string.
✗ Incorrect
We need to check if the string starts with the prefix to find the common prefix.
4fill in blank
hardFill both blanks to shorten the prefix by one character from the end inside the while loop.
DSA Typescript
prefix = prefix[1](0, prefix[2] - 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using substr which has different parameters.
Using substring but mixing parameters incorrectly.
✗ Incorrect
We use slice(0, length - 1) to remove the last character from prefix.
5fill in blank
hardFill all three blanks to complete the for loop that iterates over the strings starting from the second one.
DSA Typescript
for (let i = [1]; i < strs[2]; i[3]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 which is the first string already used as prefix.
Using i-- or other incorrect increments.
✗ Incorrect
The loop starts at 1, runs while i < strs.length, and increments i by 1 each time.
