Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first occurrence index of target in the list.
DSA Python
def first_occurrence(arr, target): for i in range(len(arr)): if arr[i] == [1]: return i return -1
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[i] with the wrong variable like i or arr.
Returning index without checking the element.
✗ Incorrect
We compare each element arr[i] with the target value to find the first occurrence.
2fill in blank
mediumComplete the code to find the last occurrence index of target in the list.
DSA Python
def last_occurrence(arr, target): last_index = -1 for i in range(len(arr)): if arr[i] == [1]: last_index = i return last_index
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[i] with i or last_index instead of target.
Not updating last_index correctly.
✗ Incorrect
We update last_index whenever we find an element equal to target to get the last occurrence.
3fill in blank
hardFix the error in the code to correctly find the first occurrence of target.
DSA Python
def first_occurrence(arr, target): for i in range(len(arr)): if arr[i] = [1]: return i return -1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' in if condition.
Using '!=' which checks for inequality.
✗ Incorrect
Use '==' to compare values, '=' is for assignment and causes a syntax error here.
4fill in blank
hardFill both blanks to create a dictionary with elements as keys and their first occurrence index as values.
DSA Python
def first_occurrence_dict(arr): result = {} for i, val in enumerate(arr): if val not in [1]: [2][val] = i return result
Attempts:
3 left
💡 Hint
Common Mistakes
Checking in arr instead of result dictionary.
Assigning to wrong variable.
✗ Incorrect
We check if val is not in result dictionary and then assign the index i to result[val].
5fill in blank
hardFill all three blanks to create a dictionary with elements as keys and their last occurrence index as values.
DSA Python
def last_occurrence_dict(arr): result = {} for i, val in enumerate(arr): [1][val] = [2] return [3]
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to wrong variable or key.
Returning wrong variable.
✗ Incorrect
We update result[val] with the current index i to keep the last occurrence, then return result.
