Stage 2 · Lesson 3 of 6
Stacks, queues and deques
A is last in, first out, like a pile of plates: you add and remove at the top only. It is how the call stack 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 , 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
- CoursePaidGrokking Data Structures & Algorithms for Coding Interviews
Design Gurus · Beginner · Subscription
Each structure from first principles, with solutions in Python, Java, C++ and JavaScript
- CourseFreeMIT 6.006 Introduction to Algorithms
MIT OpenCourseWare · Intermediate
Free: lectures 2 and 4 cover dynamic arrays, linked lists, sets and hashing
Practise
Keep handy
Also worth your time
How to pick a structure from the operations a problem needs