Coding Interview Question

Coding Interview Question

Stack operations in constant time O(1)

Stack operations in constant time O(1)

Go ahead and step into the kitchen.

What do you see?

Plates, bowls, containers and snacks and so many more delicious delicacies.

Look closely.

You may also see something like this. Image for post

Recognize those Plates on top of each other?

This is precisely where the concept of Stack came from.

Think about the things you can do with such a pile of plates.

You can either put a new plate on top or remove the top plate.

If you want the plate at the bottom, you have to first remove all the plates on top.

Such an arrangement is called Last In First Out — the last item that was placed is the first item to go out.

Programming Jargons of Stack

In the language of Computers,putting an item on top of the stack is called “push” and removing an item is called “pop”.

A stack is an object or more specifically an abstract data structure(ADT) that allows the following operations:

  1. Push: Add an element to the top of a stack

2. Pop: Remove an element from the top of a stack

3. IsEmpty: Check if the stack is empty

4. IsFull: Check if the stack is full

5. Peek: Get the value of the top element without removing it.

Also needless to say, the topmost element is called the top()

We can implement stacks by using concrete data types called Array and/or Linked Lists. They are linear data structures. The difference between them has to do with memory allocation.

Image for post

Now the challenge here is to perform the following operations in O(1) time:

  • Push(x)
  • Pop()
  • top()
  • getMin()

Our main contender here will be the getMin() Operation.

Now, Storing the overall minimum sounds like a good idea. But once we pop an element, how do we find the minimum again in O(1) time?

Let’s look at this example

Image for post

Stack A

Top points to 3

Our minima here is 3.

But if we pop 3, our minima shifts to 5.

If we pop 5, our minima should be 8.

How do we go about performing this in real time?

What we can do is:

We can duplicate this stack and have another stack called Stack B that looks exactly the same except.

Image for post

Stack B

Whenever you insert an element in the new stack, you insert the new minimum from the previous stack A.

Therefore, Initially you will insert 19 as it is the only element, and then compare 8 and 19,

8 is the minimum therefore Stack B should have 8 as the second element.

Now the third element is 23, compare the three elements, 8 is the minimum, hence 8 is inserted in Stack B again.

Similarly, 5 and 3 are inserted.

So, now you can see that the second stack is telling us the current minimum number in the Stack A in O(1) time.

Top of Stack B points to the current minimum element in Stack A.

So, By using this trade-off of Space and time, we actually used more space but we were able to solve the entire problem in O(1) time.

The code implementation using C++ is given below:

Image for post

Image for post

complexity constant stack interview

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Authentication In MEAN Stack - A Quick Guide

Everything you should know about Authenticating in MEAN stack applications with comprehensive explanation and necessary code snippets.

Top 130 Android Interview Questions - Crack Technical Interview Now!

Top Android Interview Questions & Answers from Beginner to Advanced level. Get ready to crack your next android interview with these android interview questions

What Is MEAN STACK and Scope of MEAN STACK Developer ?

Best Mean Stack training skills from top-rated web development experts. CETPA Offers online basic to advanced courses to help you master the full MEAN Stack including MongoDb, ExpressJS, AngularJS and NodeJs.

How to Interview Your Interviewer

How to Interview Your Interviewer - It’s near the end of the interview and it’s time for you to ask your interviewer questions. Figure out if this company is worthy of you

What is the Difference between Full Stack and Mean Stack Developer

Looking for ✔️ Full stack Training in Noida ✔️ Full stack Course in Noida ✔️ Full stack Training Institute in Noida ✔️ Full stack Coaching in Noida ✔️ Full stack Classes in Noida deliver by APTRON Corporate Trainers with Real Time Live Projects.