Episode 7 — DSA with JavaScript / 7.14 — Stack

7.14 — Quick Revision: Stack

<< Overview


Key concepts

OperationTime
PushO(1)
PopO(1)
PeekO(1)
IsEmptyO(1)

Key patterns

  • Matching: Push open brackets, match on close
  • Monotonic stack: Maintain sorted order for NGE-type problems
  • Two stacks: Implement queue, min stack
  • Expression eval: Operands push, operators pop two

Self-check drill

SC-001

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-002

  • Q: Push/Pop time?
  • A: O(1)

SC-003

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-004

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-005

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-006

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-007

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-008

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-009

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-010

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-011

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-012

  • Q: Push/Pop time?
  • A: O(1)

SC-013

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-014

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-015

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-016

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-017

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-018

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-019

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-020

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-021

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-022

  • Q: Push/Pop time?
  • A: O(1)

SC-023

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-024

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-025

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-026

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-027

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-028

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-029

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-030

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-031

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-032

  • Q: Push/Pop time?
  • A: O(1)

SC-033

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-034

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-035

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-036

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-037

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-038

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-039

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-040

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-041

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-042

  • Q: Push/Pop time?
  • A: O(1)

SC-043

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-044

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-045

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-046

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-047

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-048

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-049

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-050

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-051

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-052

  • Q: Push/Pop time?
  • A: O(1)

SC-053

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-054

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-055

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-056

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-057

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-058

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-059

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-060

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-061

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-062

  • Q: Push/Pop time?
  • A: O(1)

SC-063

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-064

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-065

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-066

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-067

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-068

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-069

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-070

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-071

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-072

  • Q: Push/Pop time?
  • A: O(1)

SC-073

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-074

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-075

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-076

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-077

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-078

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-079

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-080

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-081

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-082

  • Q: Push/Pop time?
  • A: O(1)

SC-083

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-084

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-085

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-086

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-087

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-088

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-089

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-090

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-091

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-092

  • Q: Push/Pop time?
  • A: O(1)

SC-093

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-094

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-095

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-096

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-097

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-098

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-099

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-100

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-101

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-102

  • Q: Push/Pop time?
  • A: O(1)

SC-103

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-104

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-105

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-106

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-107

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-108

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-109

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-110

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-111

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-112

  • Q: Push/Pop time?
  • A: O(1)

SC-113

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-114

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-115

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-116

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-117

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-118

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-119

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-120

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-121

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-122

  • Q: Push/Pop time?
  • A: O(1)

SC-123

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-124

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-125

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-126

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-127

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-128

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-129

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-130

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-131

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-132

  • Q: Push/Pop time?
  • A: O(1)

SC-133

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-134

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-135

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-136

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-137

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-138

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-139

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-140

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-141

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-142

  • Q: Push/Pop time?
  • A: O(1)

SC-143

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-144

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-145

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-146

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-147

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-148

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-149

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-150

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-151

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-152

  • Q: Push/Pop time?
  • A: O(1)

SC-153

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-154

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-155

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-156

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-157

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-158

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-159

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-160

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-161

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-162

  • Q: Push/Pop time?
  • A: O(1)

SC-163

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-164

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-165

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-166

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-167

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-168

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-169

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-170

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-171

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-172

  • Q: Push/Pop time?
  • A: O(1)

SC-173

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-174

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-175

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-176

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-177

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-178

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-179

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-180

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-181

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-182

  • Q: Push/Pop time?
  • A: O(1)

SC-183

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-184

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-185

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-186

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-187

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-188

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-189

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-190

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order

SC-191

  • Q: Stack order?
  • A: LIFO — Last In, First Out

SC-192

  • Q: Push/Pop time?
  • A: O(1)

SC-193

  • Q: Valid parentheses?
  • A: Push open, pop and compare close

SC-194

  • Q: Next greater element?
  • A: Stack from right, pop smaller

SC-195

  • Q: Min stack?
  • A: Auxiliary stack tracking minimums

SC-196

  • Q: Stack vs Queue?
  • A: Stack: LIFO, Queue: FIFO

SC-197

  • Q: Postfix evaluation?
  • A: Push operands, pop two for operators

SC-198

  • Q: Call stack?
  • A: Function calls use stack (LIFO)

SC-199

  • Q: Largest rect histogram?
  • A: Stack of indices, calculate on pop

SC-200

  • Q: Monotonic stack?
  • A: Stack maintaining increasing/decreasing order