Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Implement Queue using Stacks
Bookmark
Input
Standard
Alternating
Batch push then pop
Peek and empty checks
Custom
operations
=
[push(1), push(2), push(3), pop(), peek(), push(4), pop()]
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
1
back
front →
1
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
1
2
back
front →
1 2
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
1
2
3
back
front →
1 2 3
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
1
2
3
front →
1 2 3
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
1
2
3
front
front →
1 2 3
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
2
3
front
front →
2 3
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
2
3
front
front →
2 3
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
2
3
4
back
front
front →
2 3 4
← back
push(1)
push(2)
push(3)
pop()
peek()
push(4)
pop()
input
pushes land here
output
pops served here
transfer
returns
2
3
4
back
front
front →
3 4
← back
algo
master
.
io
Step:
Two stacks make a queue: input collects pushes, output serves them in FIFO order
0 / 31
Input
Standard
Alternating
Batch push then pop
Peek and empty checks
Custom
operations
=
[push(1), push(2), push(3), pop(), peek(), push(4), pop()]
0 / 31
algo
master
.
io
Step:
Two stacks make a queue: input collects pushes, output serves them in FIFO order