DEV Community

DRASHTI BUDDHADEV
DRASHTI BUDDHADEV

Posted on

Once You See the Pattern, You Can’t Unsee It — The Stack Story

There are few things that initially did not make sense to me- What first seemed confusing and senseless later turned out to be brilliantly logical.

In this article we'll go through some "simple" yet mind-bending stack behaviors that will make you question everything!

Let's start with an easy one:

let stack = [];
stack.push("first");
stack.push("second");
console.log(stack[0]);
Enter fullscreen mode Exit fullscreen mode

Obviously, it prints "second" because stacks are LIFO, right?

Correct Answer: "first"

WAIT, WHAT?!

Why?? JavaScript arrays used as stacks still maintain their array indexing! Index 0 is always the first element you pushed, not the "top" of your logical stack. The stack behavior only applies to push() and pop() methods, not array access !

> Interesting, right? Let’s move on to the 2nd:

let s1 = new Stack();
let s2 = new Stack();
s1.push(1); s1.push(2);
s2.push(1); s2.push(2);

console.log(s1 == s2);
Enter fullscreen mode Exit fullscreen mode

Guessed the answer?
Correct Answer is: false
Why? Stacks are objects → compared by reference, not content.
Even identical content → different memory → false.
Want equality? Implement equals() manually.

> Okay, next one is my favorite!

If you are someone preparing for interviews or are someone like me who has started making DSA a routine, here's what you must know:

  1. A monotonic stack is a savior for problems where:
    -Boundaries* or ranges are to be found for each element of an array.
    -The previous smallest element and the next smallest element are of concern for each element of an array.

  2. It’s called “monotonic” because the elements in the stack always maintain a certain order — either increasing or decreasing.
    -A monotonic increasing stack keeps elements in ascending order (used to find the next smaller element).
    -A monotonic decreasing stack keeps elements in descending order (used to find the next greater element).

Why it’s powerful:
It turns problems that seem like they require nested loops (O(n²)) into linear O(n) solutions. You traverse the array just once, pushing and popping elements intelligently.

Quick pattern recap:(of problems where stack would be used)

  1. Move through the array (usually from right to left).
  2. While the stack is not empty and the top element violates your “monotonic” condition, pop it.
  3. The element on top (after popping) is your next smaller or greater one.
  4. Push the current element into the stack.

_**> What I’ve learned:

The monotonic stack isn’t just a trick — it’s a mindset. Once you see the pattern behind these problems, you start recognizing it everywhere.**_

Since I just mentioned the pattern behind the problems — you can’t miss what’s coming next - these classic problems will blow your mind!

Note: Just because you understand the pattern doesn’t mean you’ll code it perfectly on the first go — it took me multiple submissions (and a few “why isn’t this working!?” moments!). That’s how you really learn. Keep going, it clicks eventually!

  1. Next Greater Element
  2. Sum of Subarray Minimums
  3. Sum of Subarray Ranges
  4. Trapping Rain Water
  5. Largest Rectangle in Histogram

Which part of stacks changed how you think about problem-solving? I’d love to hear your take in the comments.

_Thanks for reading till the end — I hope this helped you connect a few dots and made your DSA journey a little brighter.
And if there’s any insight or trick about stacks that I might’ve missed, drop it in the comments too — I’d love to learn from you as well!

Top comments (0)