<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: DRASHTI BUDDHADEV</title>
    <description>The latest articles on Forem by DRASHTI BUDDHADEV (@drashti_buddhadev_010a46e).</description>
    <link>https://forem.com/drashti_buddhadev_010a46e</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3587697%2F5ae0bd2a-5683-4186-b514-8d39d4b5a1a1.jpeg</url>
      <title>Forem: DRASHTI BUDDHADEV</title>
      <link>https://forem.com/drashti_buddhadev_010a46e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/drashti_buddhadev_010a46e"/>
    <language>en</language>
    <item>
      <title>Once You See the Pattern, You Can’t Unsee It — The Stack Story</title>
      <dc:creator>DRASHTI BUDDHADEV</dc:creator>
      <pubDate>Wed, 29 Oct 2025 17:16:01 +0000</pubDate>
      <link>https://forem.com/drashti_buddhadev_010a46e/once-you-see-the-pattern-you-cant-unsee-it-the-stack-story-42cg</link>
      <guid>https://forem.com/drashti_buddhadev_010a46e/once-you-see-the-pattern-you-cant-unsee-it-the-stack-story-42cg</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In this article we'll go through some "simple" yet mind-bending stack behaviors that will make you question everything!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkotgi1lppbomwz94epz0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkotgi1lppbomwz94epz0.png" alt=" " width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's start with an easy one:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let stack = [];
stack.push("first");
stack.push("second");
console.log(stack[0]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Obviously, it prints "second" because stacks are LIFO, right?&lt;/p&gt;

&lt;p&gt;Correct Answer: &lt;strong&gt;"first"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;WAIT, WHAT?!&lt;/p&gt;

&lt;p&gt;Why?? JavaScript arrays used as stacks still &lt;strong&gt;maintain their array indexing&lt;/strong&gt;! 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 !&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&amp;gt; Interesting, right? Let’s move on to the 2nd:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let s1 = new Stack();
let s2 = new Stack();
s1.push(1); s1.push(2);
s2.push(1); s2.push(2);

console.log(s1 == s2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;em&gt;&amp;gt; Okay, next one is my favorite!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A monotonic stack is a savior for problems where:&lt;br&gt;
-&lt;strong&gt;Boundaries&lt;/strong&gt; or &lt;strong&gt;ranges&lt;/strong&gt; are to be found for each element of an    array.&lt;br&gt;
-The &lt;strong&gt;previous smallest element&lt;/strong&gt; and the &lt;strong&gt;next smallest element&lt;/strong&gt; are of concern for each element of an array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s called “&lt;strong&gt;monotonic&lt;/strong&gt;” because the elements in the stack always maintain a certain order — either increasing or decreasing.&lt;br&gt;
-A &lt;strong&gt;monotonic increasing stack&lt;/strong&gt; keeps elements in ascending order (used to find the next smaller element).&lt;br&gt;
-A &lt;strong&gt;monotonic decreasing stack&lt;/strong&gt; keeps elements in descending order (used to find the next greater element).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;strong&gt;Quick pattern recap:&lt;/strong&gt;(of problems where stack would be used)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Move through the array (usually from right to left).&lt;/li&gt;
&lt;li&gt;While the stack is not empty and the top element violates your “monotonic” condition, pop it.&lt;/li&gt;
&lt;li&gt;The element on top (after popping) is your next smaller or greater one.&lt;/li&gt;
&lt;li&gt;Push the current element into the stack.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What I’ve learned:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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.**_&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Since I just mentioned the pattern behind the problems — you can’t miss what’s coming next - these classic problems will blow your mind!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; 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!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/next-greater-element-i/description/" rel="noopener noreferrer"&gt;Next Greater Element
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/sum-of-subarray-minimums/description/" rel="noopener noreferrer"&gt;Sum of Subarray Minimums&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/sum-of-subarray-ranges/description/" rel="noopener noreferrer"&gt;Sum of Subarray Ranges&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/trapping-rain-water/description/" rel="noopener noreferrer"&gt;Trapping Rain Water&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/largest-rectangle-in-histogram/description/" rel="noopener noreferrer"&gt;Largest Rectangle in Histogram
&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Which part of stacks changed how you think about problem-solving?&lt;/strong&gt; I’d love to hear your take in the comments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;_Thanks for reading till the end — I hope this helped you connect a few dots and made your DSA journey a little brighter.&lt;br&gt;
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!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>dsa</category>
      <category>softwareengineering</category>
      <category>career</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
