ā¤ Like
šŸ”– Save
šŸ”— Share
Eric Hu
Eric Hu

DSA Stacks & Queues

@thedevspaceio

šŸ“š Stacks & Queues Cheatsheet

Stacks are LIFO, queues are FIFO. This cheatsheet covers both structures, their operations, and the classic problems they solve.

āœ… Stack operations āœ… Queue operations āœ… Monotonic stack āœ… Deque āœ… Classic problems

#dsa #stack #queue #deque #interview #coding #tips


Stacks and queues are linear structures with restricted access. Stacks are last-in-first-out (LIFO), queues are first-in-first-out (FIFO).


Stack

Last in, first out. Like a stack of plates.

js
const stack = [];
 
stack.push(1); // add to top
stack.push(2);
stack.push(3);
 
stack.at(-1); // 3 — peek at top
stack.pop(); // 3 — remove from top

All operations are O(1).


Queue

First in, first out. Like a line at a store.

js
const queue = [];
 
queue.push(1); // enqueue at back
queue.push(2);
queue.push(3);
 
queue[0]; // 1 -> peek at front
queue.shift(); // 1 -> dequeue from front

āš ļø shift() on an array is O(n). For performance-critical code, use a real queue with a head pointer.

js
class Queue {
  #items = [];
  #head = 0;
 
  enqueue(item) {
    this.#items.push(item);
  }
 
  dequeue() {
    return this.#items[this.#head++];
  }
 
  get size() {
    return this.#items.length - this.#head;
  }
}

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io