Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return an empty string if the input list is empty.
DSA Python
def longest_common_prefix(strs): if not strs: return [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of an empty string.
Returning 0 or the list itself.
✗ Incorrect
If the list is empty, the longest common prefix is an empty string ''.
2fill in blank
mediumComplete the code to initialize the prefix as the first string in the list.
DSA Python
def longest_common_prefix(strs): if not strs: return '' prefix = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strs[1] which may not exist or is not the first string.
Starting with an empty string which won't help find the prefix.
✗ Incorrect
The prefix starts as the first string, then we compare it with others.
3fill in blank
hardFix the error in the loop that compares prefix with each string.
DSA Python
def longest_common_prefix(strs): if not strs: return '' prefix = strs[0] for s in strs[[1]:]: while not s.startswith(prefix): prefix = prefix[:-1] if not prefix: return '' return prefix
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 causes redundant comparison.
Using negative or out-of-range indices.
✗ Incorrect
We start comparing from the second string, index 1, because prefix is strs[0].
4fill in blank
hardFill both blanks to correctly reduce the prefix until it matches the start of the string.
DSA Python
def longest_common_prefix(strs): if not strs: return '' prefix = strs[0] for s in strs[1:]: while not s.startswith(prefix): prefix = prefix[1]1 if not prefix: return [2] return prefix
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect slicing syntax like prefix[+1].
Returning None instead of empty string.
✗ Incorrect
prefix[:-1] removes the last character; return '' if prefix is empty.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each word to its length if length is greater than 3.
DSA Python
words = ['apple', 'bat', 'car', 'door'] lengths = { [1] : [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using len(word) as key instead of value.
✗ Incorrect
The comprehension uses word as key and len(word) as value, iterating over words with variable word.