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
4d

Implement a priority queue using a heap data structure by completing the provided functions: push(), pop(), getMaxElement(), and isEmpty().

  • Understand the operations: push() to insert element, pop() to...read more

Help your peers!
Add answer anonymously...
Salesforce Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter