in DS edited by
3,071 views
9 votes
9 votes

How it is 24? I'm not getting it.

in DS edited by
3.1k views

4 Comments

Defiantly this question is ambiguous because they haven't told how they are actually implementing the max so answer varies how you interpret the question by applying different approaches and interpretation we will get different answer like 12,16,24 etc,
0
0

They have given this solution where they have mentioned 4B in question but given the answer according to 8B!

0
0
So, What is the conclusion  .... I am confused

Someone is saying 16 or 32 or 20 or 8 !!!
0
0

5 Answers

0 votes
0 votes

Ans: 20 bytes

Applying efficient algorithm, 

Main_Stack Max_Stack
5  
8 8
6  
7 7
6 6
5 5

Here, when 7 is pushed, new entry is created in MAX_STACK because it is the new MAX. 

Next element to be pushed is 6. Applying efficient algorithm there is no need to push it in MAX_STACK. 

Similarly, when 8 is pushed, new entry is created in MAX_STACK of new MAX.

Now, coming to POP peration, 8,6,7 are popped from MAIN_STACK and along with it 8 and 7 are also popped from MAX_STACK.

At the end, 5 is pushed on MAIN_STACK and no need to insert it on MAX_STACK.

So, final table is - 

MAIN_STACK MAX_STACK
5  
6 6
5 5

So, total number of elements = 5

Total size = 5*4 bytes = 20 Bytes

PS : Above concept is discussed in Data Structures and Algorithms - Narasimha Karumanchi 

edited by
Answer:

Related questions