Validate Stack Sequences

Advanced

Validate Stack Sequences

Hey young coders! Today, we are going to dive into a challenge that involves a magical stack to help you realize how items are pushed and popped as if they are on a stack of pancakes!

A stack works like a collection of items that are kept (pushed) or taken (popped) from the top, following the Last-In-First-Out (LIFO) order — imagine stacking your favorite comic books and reading the top one first!

Your mission, if you choose to accept it, is to write a function to check if the pushed and popped sequences of the stack are valid!

Rules:

  1. You are given two sequences of integers:
    • pushed: represents the order in which items are pushed onto a stack.
    • popped: represents the order in which items are popped from the stack!
  2. Your function should return True if the popped sequence can be achieved with the pushed sequence using a stack. Otherwise, return False.

Example:

validate_stack_sequences([1,2,3,4,5], [4,5,3,2,1]) # returns True # Explanation: Push 1,2,3,4 onto the stack, pop 4, then push 5, and pop 5, pop 3, pop 2, pop 1. validate_stack_sequences([1,2,3,4,5], [4,3,5,1,2]) # returns False # Explanation: Pop order 1 and 2 cannot be achieved without violating the LIFO order.

Let the stacking game begin and test your solutions with different sequences! Good luck!

Geek
Inputs

Arg 1:

[1,2,3,4,5]

Arg 2:

[4,5,3,2,1]
Expected Output
true
Actual Output
Validate Stack Sequences [advanced] | Coding for Kids