Incorrect expected output

Describe your issue

My code for the Stack Sort problem is failing. I clicked on “Debug My Code” to view failing test cases. It gave me a test case for which the expected output is given as such -
“NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO”
(Notice the scroll bar in the “Expected Output” box in the provided screenshot)
I have checked and double-checked, the answer should be “YES”. Due to this my written code for this problem is failing. I also ran the test case with the correct code submitted by other users in the “Submissions” tab and the “Solution” tab, and the answer I got was the same for the code that I wrote (“YES”).

Screenshot

Additional info

Problem url - Stack Sort Practice Problem in Stacks and Queues

My code -

from collections import deque

t = int(input())
for _ in range(t):
    n = int(input())
    a = deque(list(map(int, input().split())))
    stack = []
    b = deque()
    is_correct_sort = "YES"
    
    for i in range(n):
        while stack and stack[-1] < a[0]:
            b.append(stack.pop())
        stack.append(a.popleft())
            
    while stack:
        num = stack.pop()
        if len(b) and num < b[-1]:
            is_correct_sort = "NO"
            break
        b.append(num)
    
    print(is_correct_sort)

Test case in concern -

1
3
3 1 2

This is fixed.

1 Like