Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize an empty stack for the stock span problem.
DSA Python
stack = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which creates a dictionary, not a stack.
Using () which creates a tuple, which is immutable.
Using set() which is unordered and not suitable for stack.
✗ Incorrect
We use a list as a stack in Python, so initializing with [] creates an empty stack.
2fill in blank
mediumComplete the code to push the current index i onto the stack.
DSA Python
stack.[1](i) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes an element instead of adding.
Using remove() which removes a specific value.
Using insert() which inserts at a specific position.
✗ Incorrect
To add an element to the top of the stack (list), we use append().
3fill in blank
hardFix the error in the while loop condition to check if stack is not empty.
DSA Python
while stack and prices[stack[-1]] <= prices[[1]]:
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using stack instead of i, which is incorrect index.
Using prices which is the list, not an index.
Using -1 which is used to access last element of stack, not current index.
✗ Incorrect
We compare the price at the top index of the stack with the current price at index i.
4fill in blank
hardFill both blanks to calculate the span for the current day.
DSA Python
span[i] = i - [1] if stack else [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of -1 for empty stack case.
Using len(stack) which is unrelated to index.
Using stack instead of stack[-1].
✗ Incorrect
If stack is not empty, span is difference between current index and top of stack index; else span is i - (-1) which is i + 1.
5fill in blank
hardFill all three blanks to complete the stock span function.
DSA Python
def calculate_span(prices): n = len(prices) span = [0] * n stack = [] for [1] in range(n): while stack and prices[stack[-1]] <= prices[[2]]: stack.pop() span[[3]] = [2] - stack[-1] if stack else [2] + 1 stack.append([2]) return span
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing index errors.
Using j or k inconsistently.
✗ Incorrect
The loop variable and all index references should be consistent as i for correct indexing.