DSA and problem solving

Stage 2 · Lesson 3 of 6

Stacks, queues and deques

Arrays, hashing, linked lists, stacks and queues

A is last in, first out, like a pile of plates: you add and remove at the top only. It is how the works, and how you match brackets, undo actions or work out expressions. A is first in, first out, like a line at a shop: add at the back, remove from the front.

That is exactly what needs later. A (double-ended queue) lets you add and remove at both ends. Every one of these operations should be O(1), and that is where language details catch people out.

Removing from the front of a plain array is O(n), so use a real deque type: deque from Python's collections module, ArrayDeque in Java or deque in C++. In JavaScript, keep an index that marks the front instead of removing items.

A question to check yourself, an AI practice session and 3 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Containers library: vector, unordered_map, deque and stack, with complexity for each call

Also worth your time

Mark this stage as not done?