Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSAStacksMin StackMonotonic Stack

Stack Types — Array, Linked, Min & Monotonic Visualized

Compare stack implementations — array-based, linked-list, min stack, and monotonic stack with interactive visualizer, code, and real-world examples.

Jul 4, 20264 min read

Introduction

Stack types share the same LIFO interface but differ in backing storage and specialized behavior. This guide covers array, linked-list, min/max, and monotonic stacks — all building on stack fundamentals.

Quick index

#Section
1Type comparison
2Interactive visualizer
3Full implementation
4Choosing a type
5Real-life examples

1. Type comparison

TypeBackingExtra opsTypical use
Array stackDynamic array—Default — simple, cache-friendly
Linked stackSingly linked list—Unbounded, no resize spikes
Min stackArray + aux min trackergetMin() O(1)Running minimum, stock span
Monotonic stackArray (maintains order)next greater/smallerHistogram, trapping rain water

Back to index


2. Interactive visualizer

Stack Types — Array, Linked, Min & Monotonic

Four stack variants — same LIFO interface, different backing storage and specialized behavior.

top ↓
30
20
10
bottom

Dynamic array with push/pop at end — JavaScript Array as stack.

Array-based stack

class ArrayStack {
constructor() { this.items = [] }
push(v) { this.items.push(v) }O(1) amortized
pop() { return this.items.pop() }
}
const s = new ArrayStack()
s.push(10); s.push(20); s.push(30)→ [10, 20, 30]

Default choice — cache-friendly, simple.

Tip

Monotonic stacks process each element once — total O(n) despite the inner while loop.

Back to index


3. Full implementation

js
// ── Array stack ──
class ArrayStack {
  constructor() {
    this.items = []
  }
  push(v) {
    this.items.push(v)
  }
  pop() {
    return this.items.pop()
  }
  peek() {
    return this.items.at(-1)
  }
}
 
// ── Linked-list stack ──
class LNode {
  constructor(v) {
    this.val = v
    this.next = null
  }
}
class LinkedStack {
  constructor() {
    this.head = null
  }
  push(v) {
    const n = new LNode(v)
    n.next = this.head
    this.head = n
  }
  pop() {
    if (!this.head) return
    const v = this.head.val
    this.head = this.head.next
    return v
  }
}
 
// ── Min stack ──
class MinStack {
  constructor() {
    this.stack = []
    this.mins = []
  }
  push(v) {
    this.stack.push(v)
    const min = this.mins.length ? Math.min(this.mins.at(-1), v) : v
    this.mins.push(min)
  }
  getMin() {
    return this.mins.at(-1)
  }
  pop() {
    this.mins.pop()
    return this.stack.pop()
  }
}
 
// ── Monotonic stack (next greater) ──
function nextGreater(arr) {
  const stack = [],
    result = Array(arr.length).fill(-1)
  for (let i = 0; i < arr.length; i++) {
    while (stack.length && arr[stack.at(-1)] < arr[i]) {
      result[stack.pop()] = arr[i]
    }
    stack.push(i)
  }
  return result
}

Function calls with output

js
// ── Demo calls — output shown on the right ──
const s = new ArrayStack()
s.push(10)
s.push(20) // → stack [10, 20]
s.pop() // → 20
 
const ms = new MinStack()
ms.push(3)
ms.push(1)
ms.push(5)
ms.getMin() // → 1
ms.pop() // → 5
 
nextGreater([2, 5, 3]) // → [5, -1, -1]

Back to index


4. Choosing a type

NeedPick
General purposeArray stack
No array resize, unboundedLinked stack
O(1) minimum at any pointMin stack
Next greater/smaller elementMonotonic stack

Interview Answer

"How does a min stack work?"

Maintain a parallel stack of running minimums. On push, push min(newVal, currentMin). On pop, pop both stacks. getMin reads the top of the min stack — O(1).

Back to index


5. Real-life examples

TypeReal system
Array stackJavaScript call stack, undo history
Min stackStock drawdown tracker, sliding window minimum
Monotonic stackLargest rectangle in histogram, daily temperatures
Linked stackExpression evaluator with unbounded depth

Min stack for stock prices

Track running minimum in O(1) — used in financial dashboards and interview problems.

150
120
130
100
110

Index → value

Live stock prices streaming in.

Real-world implementation

class MinStack {
  constructor() { this.stack = []; this.mins = [] }
  push(v) {
    this.stack.push(v)
    const min = this.mins.length ? Math.min(this.mins.at(-1), v) : v
    this.mins.push(min)
  }
  pop() { this.mins.pop(); return this.stack.pop() }
  getMin() { return this.mins.at(-1) }
}

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const ms = new MinStack()
ms.push(150)→ min: 150

Side output shows return value after each call — step through to trace execution.

Back to index


Summary

Array stack for defaults; min stack for running extrema; monotonic stack for next-greater/smaller patterns — all O(1) push/pop at the top.

Next reads: Stack Applications · DSA Stacks

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime