Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push the current element onto the stack.
DSA Python
stack.[1](arr[i]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of append.
Using push which is not a Python list method.
✗ Incorrect
In Python, to add an element to the end of a list (used as a stack), we use append().
2fill in blank
mediumComplete the code to pop the top element from the stack.
DSA Python
top = stack.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove which removes by value, not by position.
Using delete which is not a list method.
✗ Incorrect
pop() removes and returns the last element from the list, which is the top of the stack.
3fill in blank
hardFix the error in the while loop condition to check if stack is not empty.
DSA Python
while stack and stack[-1] [1] arr[i]:
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than or equal operators which reverse the logic.
Using less than or equal which includes equal values incorrectly.
✗ Incorrect
We want to pop elements smaller than the current element, so the condition is stack[-1] < arr[i].
4fill in blank
hardFill both blanks to complete the dictionary comprehension for the next greater element.
DSA Python
result = {num: [1] for num in arr if num [2] -1} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which would only include the last element.
Using arr[-1] which is the last element of the array, not the next greater element.
✗ Incorrect
We map each number to the top of the stack (next greater element), excluding the last element which has no next greater element.
5fill in blank
hardFill all three blanks to complete the function that finds the next greater element for each item in the list.
DSA Python
def next_greater_element(arr): stack = [] result = {} for i in range([1]): while stack and stack[-1] < arr[i]: result[stack.pop()] = arr[i] stack.[2](arr[i]) for num in stack: result[num] = [3] return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect loop range.
Using push instead of append.
Assigning -1 instead of None for no next greater element.
✗ Incorrect
We loop through the entire array length, append elements to the stack, and assign None to elements with no next greater element.