Skip to main content
LeetCode 946, Medium. Topics: Array, Stack, Simulation. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

<p>Given two integer arrays <code>pushed</code> and <code>popped</code> each with <strong>distinct</strong> values, return <code>true</code> if this could have been the result of a sequence of push and pop operations on an initially empty stack, or <code>false</code> otherwise.</p>

Examples

Constraints

  • 1 <= pushed.length <= 1000
  • 0 <= pushed[i] <= 1000
  • All the elements of pushed are unique.
  • popped.length == pushed.length
  • popped is a permutation of pushed.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026