Hello everyone, hope you are doing good. You are probably a microservices engineer or an engineer building complex applications. So, you might be using the mesh architecture or using complex data structures like Graphs. The data structures like Stacks and Queues might look like primitives or basic to you? So why we want to discuss the Stacks in a world of microservices? and how the Stacks are important in an API world? Let’s find out.

Stacks as a Data Structure
Stacks as a data structure is a basic block for any programming language. With the complex operations and technologies like AI and ML, you might think that Stacks are obsolete. But that’s not the case. We are here to talk about the basics of Stacks first. Well, it might be a little bit of revision for you guys.
Stacks work on a principle of LIFO. If you have a very narrow street with one end closed. There cannot be more than two people standing side by side. Suppose Jacob enters first, followed by Ram, and then Serena. What needs to be done if Jacob has to come out of the street? Serena needs to give way to Ram. Followed by Jacob, in order for Jacob to move out. So, without Serena leaving her current place, there is no way that Jacob can come out on the main road.

This makes the searching in a Stack as O(n), while insertion is retrieval is also O(n) as the worst case. While, it is O(1) for the best case. What if Serena wants to move out of the street? She can just pop out.
Practical usage of Stacks
Since we are interested in the least possible Big O, we would like to use the Stacks when we know that the time complexity is going to be O(1), and it cannot be greater than that. Do you know why? this is because we already have some Data Structures, which are far better than Stacks in terms of searching and retrieval. So, we want to limit the cases to cater to only those requirements where the Stack can fulfill the time complexity of O(1).
This is the very reason for using the Stacks in the Undo operation. Because it will store the previous operation and if needed, can replicate the last settings. It will not follow the order of insertion (unlike in Queues) but will follow the LIFO approach. Remember in the Notepad (not Notepad++), when you do undo or try to do it twice. Do you fail to do so? While you can do it multiple times in Adobe Photoshop or Adobe Lightroom? The answer is simple. The Undo/Redo Stack of the notepad is having only 2 elements. On the other hand, it is much more in terms of the Adobe software.
Can we use Stacks in APIs?
Now comes the most important question. Is it possible to use Stacks in the APIs. Well, Raymond is thinking that he is been using Hashmaps all the time in his APIs, why he will ever use Stacks?
Let’s look closely into Raymond’s APIs. This is happening because he is using stock brokering APIs. And he cannot afford to have the worst-case time complexity in terms of Stacks. So, for his functionalities, he has been using other DSs in order to cater to his current requirements. Raymond is working on a project where he is doing long-term trading and portfolio management of his clients. So, usually, he will know when to pull back a particular stock or share.
Raymond is having a smooth life but during his Sprint planning call. His Scrum master Mr. Raj told him that he wants the capability to do intra-day trading. He is thinking to implement the service class again using the Hashmap. During his coffee break, he discusses this idea of his. Then Mr. Sundar, a senior developer in his team told him that he should use Stacks. The moment Raymond heard Stacks, he started laughing about it, calling his approach primitive. We are dealing with cutting-edge technologies, and the stocks are changing in such a random or complex manner. I think we should be talking about “Stocks” but not “Stacks”.

Mr. Sundar smiled at this response and told him that what he is saying about the technology is right. But there is an old saying in India “Jahaan Kaam aave sui, Kaaha kare talwar”. This means that where you need a needle, you cannot use a sword. Then Sundar continued explaining to him the peculiar case about intra-day trading. The scenario where a stock has been purchased, and the client wants to sell the stock at a particular rate, without buying any extra stock. This means that if he purchases a stock at 10 am. He wants to sell it at 11:30 if the stock value is reaching an x% increment. This can be implemented in Stacks rather than using extra space and memory. As the day ends, the stack memory will have either one element or zero elements. Similarly, the time complexity will always be O(1). In this way, you can achieve the time and space complexity of O(1), without utilizing much of a memory.
Raymond was really surprised to see the application of Stacks in such a complex application. But this is how the Stacks have been saving us since their inception. Hope you use the Stacks in your next projects :-).