Adobe Interview Question
MTSsCountry: India
Interview Type: In-Person
class Stack
{
private int c = 0;
private PriorityQueue pq;
public void Push(int x)
{
c++;
pq.Insert(x,c);
}
public int Pop()
{
c--;
return pq.Remove();
}
}
This push pop operation takes O(lg n) time....where n is the number of already existing nodes..
keep a variable c and every time u push it..then increment its priority such that..the element with high priority will be removed first ..so the element which is pushed in the end will have highest value of c so it will be the first element to be popped off.....
- c7c7 October 05, 2012implement priority queue using linked list..and priority will be the time of insertion which we will track with c variable..