Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

stacks-queues

Stacks and queues

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Stacks and queues: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Teams apply Stacks and queues in every serious DSA project—skipping it leaves blind spots in analysis and reviews.

You will apply Stacks and queues in contexts like: Interview loops, performance tuning, and foundational CS courses.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on.

When you can explain the previous lesson's ideas in your own words.

A stack is LIFO (last in, first out); a queue is FIFO. C++ provides std::stack and std::queue adapters over deque/vector—used in DFS, BFS, parsing, and undo buffers.

Operations

  • Stack: push, pop, top — O(1)
  • Queue: push, pop, front — O(1)

Applications

  • Stack — matching parentheses, DFS iterative, monotonic stack
  • Queue — BFS shortest path layers, task scheduling

Stack and queue demo

#include 
#include 
#include 

int main() {
    std::stack st;
    st.push(1); st.push(2); st.push(3);
    std::cout << "stack top=" << st.top() << "\n";
    st.pop();
    std::queue q;
    q.push(10); q.push(20);
    std::cout << "queue front=" << q.front() << "\n";
    return 0;
}

Important interview questions and answers

  1. Q: LIFO vs FIFO example?
    A: Undo stack is LIFO; printer queue is FIFO.
  2. Q: Can queue use vector alone?
    A: Pop from front of vector is O(n); deque gives O(1) ends.

Self-check

  1. Name two problems that use a stack.
  2. What is complexity of queue push and pop?

Tip: Parentheses matching is the classic stack warm-up—code it once from memory.

Interview prep

LIFO vs FIFO?

Stack last-in-first-out; queue first-in-first-out.

BFS structure?

Queue for layer-order traversal.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • LIFO vs FIFO?
  • BFS queue?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump