Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dictionary for storing seen numbers.
DSA Python
seen = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list or set instead of a dictionary.
Using parentheses which create a tuple, not a dictionary.
✗ Incorrect
We use an empty dictionary {} to store numbers and their indices for quick lookup.
2fill in blank
mediumComplete the code to iterate over the list with index and value.
DSA Python
for [1], num in enumerate(nums):
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the value variable name instead of index.
Using a variable name that is not descriptive or inconsistent.
✗ Incorrect
The variable i is commonly used as the index in loops.
3fill in blank
hardFix the error in checking if the complement exists in the dictionary.
DSA Python
if [1] in seen:
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if the current number
num is in seen instead of the complement.Using the index
i instead of the complement.✗ Incorrect
We check if the complement target - num is already in the dictionary seen.
4fill in blank
hardFill both blanks to return the indices of the two numbers that add up to target.
DSA Python
return [seen[[1]], [2]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the current number instead of its index.
Swapping the order of indices incorrectly.
✗ Incorrect
Return the index of the complement from seen and the current index i.
5fill in blank
hardFill all three blanks to add the current number and its index to the dictionary.
DSA Python
seen[[1]] = [2] # After loop ends return [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the index as key instead of the number.
Returning an empty list instead of None.
✗ Incorrect
Add the current number as key and index as value to seen. Return None if no pair found.