Implementing a Priority Queue Using Heap
Ninja has been tasked with implementing a priority queue using a heap data structure. However, he is currently busy preparing for a tournament and has requested your assistance.
Your task is to use the class structure provided in the code editor comments and complete the functions: push()
, pop()
, getMaxElement()
, and isEmpty()
to implement a priority queue.
Note:
There are four types of operations that can be performed: Type 1: push(x)
- Insert element ‘x’ into the queue. Type 2: pop()
- Remove the largest element from the queue. If the queue is empty, do nothing. Type 3: getMaxElement()
- Return the largest element. If the queue is empty, return -1. Type 4: isEmpty()
- Return ‘1’ if the queue is empty, otherwise return ‘0’.
Input:
The first line contains an integer ‘Q’, the number of queries. The following lines contain the queries, each consisting of an integer indicating the type of query. For type 1 queries, there is an additional space-separated integer X.
Output:
For each type 3 query, output an integer value on a new line.
Example:
Input:
5
1 10
1 5
3
2
3
Output:
10
5
Constraints:
1 <= Q <= 100
1 <= X <= 10^9
- Time limit: 1 second
Note:
You are only required to complete the function implementations.

AnswerBot
4mo
Implement a priority queue using a heap data structure by completing the provided functions.
Implement push() function to insert elements into the queue.
Implement pop() function to remove the largest e...read more
Help your peers!
Add answer anonymously...
Oracle Member Technical Staff interview questions & answers
A Member Technical Staff was asked 2mo agoQ. Given a string, find all possible codes that the string can generate. You are gi...read more
A Member Technical Staff was asked 2mo agoQ. You are given the heads of two sorted linked lists list1 and list2. Merge the tw...read more
A Member Technical Staff was asked 3mo agoQ. How can you detect a loop in a linked list?
Popular interview questions of Member Technical Staff
A Member Technical Staff was asked 3mo agoQ1. Given a string, find all possible codes that the string can generate. You are gi...read more
A Member Technical Staff was asked 3mo agoQ2. You are given the heads of two sorted linked lists list1 and list2. Merge the tw...read more
A Member Technical Staff was asked 3mo agoQ3. How can you detect a loop in a linked list?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

