GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
143 views

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

asked in DS by Loyal (4.1k points)  
retagged by | 143 views
I too think 12 is correct
Still ambigious answer, no valid reason for weather it's 12 or 24??

3 Answers

+3 votes

I think the answer is 24. 

If we see, there will be only three elements after all the operations which may prompt us to think the size of the stack is =12 bytes (3*4 bytes) .

But STACK is a static data structure  and hence needs to allocate space for all the numbers even if they are not present in the stack simultaneouly. 

Here , numbers are  - 5 5 6 6 7 8  (Consider them as block of memory rather than nummbers )

So, size of the  datastructure will be = 6* 4 Bytes= 24 Bytes 

PUSH and POP operations doesn't allocate or free memory . They are simply the  Increment/decrement of Stack Pointer.

answered by (335 points)  
if this is the case then we have atmost 4 elements.

push(5) push(6) push(7) pop push(6) push(8) pop pop push(5)

then 4*4=16

isn't it?

 

and other thing if we use linked list instead of array then what will be size??

There will be six elements after the above set of operations. So size of stack is 24. Max reports the maximum element in stack and will not remove it. And Stack need not be a static data structure. If Stack is implemented using a linked list then it can grow dynamically. PUSH operation will create a node, allocate memory to it and then add it to the stack. If so each node will also contain a pointer which will consume some memory. But no such condition is mentioned in the question and hence can be ignored.

I will upvote only if you remove that "I think" written in your answer. 

0 votes

Ans b) 24

There will be six elements after the above set of operations. So size of stack is 24. Max reports the maximum element in stack and will not remove it. And Stack need not be a static data structure. If Stack is implemented using a linked list then it can grow dynamically. PUSH operation will create a node, allocate memory to it and then add it to the stack. If so each node will also contain a pointer which will consume some memory. But no such condition is mentioned in the question and hence can be ignored.

answered by Loyal (3.6k points)  
0 votes

Answer Varies.

Stack is linear data structure.

If its static implementation of stack, iel using linear array then answer is 24 bytes. when total used space is asked.

If its dynamic implementation of stack, ie. using linked list then answer is 12 bytes.

Also if current allocated space asked in static implementation then its 12 bytes.

answered by Junior (507 points)  


Top Users Jul 2017
  1. Bikram

    4062 Points

  2. manu00x

    2464 Points

  3. Debashish Deka

    1850 Points

  4. joshi_nitish

    1658 Points

  5. Arjun

    1294 Points

  6. Hemant Parihar

    1184 Points

  7. Arnab Bhadra

    1112 Points

  8. Shubhanshu

    1054 Points

  9. Ahwan

    900 Points

  10. rahul sharma 5

    706 Points


24,022 questions
30,966 answers
70,346 comments
29,342 users