1,315 views
1 1 vote
Consider a stack is implemented using an array. What is worst case time complexity of push operation?

give explanation

1 Answer

0 0 votes

O(1) in both worst and best case, ONLY if we have a pointer to the location where the next element is to be inserted in the STACK. Thereby, if we have to push a new element into the stack, we execute

a[pointer++] = elementValue.;

To pop and element, we execute,

poppedValue = a[--pointer];

--

Altough unlikely, if we have lost the pointer that was used to keep track of the top element of the stack, then obviously we have to search for the top most element and then push element into the stack. Time Complexity in that case is O(n).

--

Hope I am right. :P

Position:
Show:

Related questions

7 7 votes
1 1 answer
373
373 views
GO Classes asked Jul 3
373 views
A sequence of $n$ elements is implemented in two ways:As a normal array with contiguous memory and no extra empty slot. As a singly linked list with only a $\texttt{head}...
1 1 vote
1 1 answer
4.4k
4.4k views
sripo asked Nov 15, 2018
4,445 views
This question is in CLRS,if we have a max heap it is always in sorted order(descending) order.And by extension if we have min heap the array is sorted in ascending order....
2 2 votes
1 1 answer
4.5k
4.5k views
Na462 asked Aug 22, 2018
4,501 views
Consider array A[1..100,1..100],in which elements are stored in Z representation. An example of 5x5 such array is shown below: Base address of array = 1000,size of each e...
13 13 votes
8 answers 8 answers
9.0k
9.0k views
Na462 asked Aug 22, 2018
8,991 views
Consider a 2 dimensional array A[40...95,40...95] in lower triangular matrix representation. The size of each element of array is 1 Byte.If array is implemented in memory...