0
0
DSA Pythonprogramming~10 mins

Stock Span Problem Using Stack in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A[]
B()
C{}
Dset()
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.
2fill in blank
medium

Complete 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'
Ainsert
Bpop
Cremove
Dappend
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.
3fill in blank
hard

Fix 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'
Ai
Bprices
Cstack
D-1
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.
4fill in blank
hard

Fill 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'
Astack[-1]
B0
C-1
Dlen(stack)
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].
5fill in blank
hard

Fill 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'
Ai
Bj
Ck
Dm
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing index errors.
Using j or k inconsistently.