0 0 votes Consider a stack is implemented using an array. What is worst case time complexity of push operation? A) O(n) B) O(log n) C) O(n log n) D) O(1) Data Structures stack data-structures + – srestha 6.8k views answer comment Share Follow Print See all 18 Comments 18 18 Comments reply saurabh rai commented Jan 13, 2017 reply Follow flag plzz think atleast once before ask u ll get 50% ans by urself.... https://gateoverflow.in/62056/complexity2 0 0 replyShare vijaycs commented Jan 13, 2017 i edited by vijaycs Jan 13, 2017 reply Follow flag @saurabh, At what index we need to do push operation... In general, in case of stack, we do push or pop operation at the top which can be done in O(1) right ?? but if not necessarily at top index then ans would be O(n) ..right ?? 0 0 replyShare srestha commented Jan 13, 2017 reply Follow flag @saurabh this is not link for this question. I have some doubt in that question. Plz answer there 0 0 replyShare srestha commented Jan 13, 2017 i edited by srestha Jan 13, 2017 reply Follow flag ....................................... 0 0 replyShare IamRishabh commented Jan 13, 2017 reply Follow flag @srestha i guess it must be "O(1)" bcoz will be do push operation we just push on top , we dont push in between of the stack . even if we do some operation on stack element before pushing other element on stack it will also take constant time. so i guess worst case time complexity will "O(1)". correct me if i am wrong at any assumption. 0 0 replyShare saurabh rai commented Jan 13, 2017 reply Follow flag @vijay is it nt O(1) i think there is some typo in ur comment 1 1 replyShare vijaycs commented Jan 13, 2017 reply Follow flag ^yes, I had written O(n) instead of O(1)... thanks :) 0 0 replyShare IamRishabh commented Jan 13, 2017 reply Follow flag @ saurabh sir , then what is the right answer of this? 0 0 replyShare saurabh rai commented Jan 13, 2017 reply Follow flag acc 2 me it is O(1) 0 0 replyShare srestha commented Jan 13, 2017 reply Follow flag " a stack is implemented using an array " u all r talking about stack. how array is using here? 0 0 replyShare saurabh rai commented Jan 13, 2017 reply Follow flag srestha it is so simple to implement stack using array suppose we have a[5] to use size=5 now insert an element in array means push in to stack nd keep track to Top usin an integer ie index of that array. 0 0 replyShare srestha commented Jan 13, 2017 reply Follow flag but insert n th element in array takes O(N) time. But in stack it is always O(1). Moreover is deletion of arrays always takes O(1)? 0 0 replyShare saurabh rai commented Jan 13, 2017 reply Follow flag yes bcoz we have always value of Top 0 0 replyShare srestha commented Jan 13, 2017 reply Follow flag But in array Say n th element deletion how much time will it take? 0 0 replyShare Rahul Jain25 commented Jan 13, 2017 reply Follow flag Stack does not delete nth element fist of all. It only deletes top element. And we will be knowing value of n so deleting will take O(1) , but it might be required(depends on which DS yiu are implementing and space constraints) to shift all other elements, hence resulting in O(n). 0 0 replyShare srestha commented Jan 13, 2017 reply Follow flag like stack how array do deletion in O(1) time? means in array we have to go from 1st element,rt? 0 0 replyShare Rahul Jain25 commented Jan 13, 2017 reply Follow flag I will use a variable top, whenever i will insert i will insert at array[top], increment top and whenver delete i will delte array[top] , bcoz i dont need to remember how many elements are there in stack , this top variable will be doing it. Insert delete both in O(1) 0 0 replyShare Hradesh patel commented Jan 13, 2017 reply Follow flag i think here O(n) is correct 0 0 replyShare Please log in or register to add a comment.