{"id":"aa195dab-3822-46b8-941c-bb1a81be633f","name":"Write Priority Queue Using Heap","description":"1. You are required to complete the code of our Priority Queue class using the heap data structure. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video.\r\n2. Here is the list of functions that you are supposed to complete:\r\n 2.1. add -> Should accept new data.\r\n 2.2. remove -> Should remove and return smallest value, if available or print \r\n \"Underflow\" otherwise and return -1.\r\n 2.3. peek -> Should return smallest value, if available or print \"Underflow\" \r\n otherwise and return -1.\r\n 2.4. size -> Should return the number of elements available.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n vector<int> data;\n\n int _size() {\n //write your code here\n return 0;\n }\n\n \n\n \n void add(int val) {\n // write your code here\n }\n\n \n\n\n int _remove() {\n //write your code here\n return 0;\n }\n\n \n\n int peek() {\n //write your code here\n\n return 0;\n }\n\n \n\n\n\nint main(){\n \n\n while(1){\n string str;\n getline(cin,str);\n if(str[0]=='a'){\n string num=str.substr(4,str.length());\n int val=stoi(num);\n add(val);\n }else if(str[0]=='r'){\n int val=_remove();\n if(val!=-1){\n cout<<val<<endl;\n }\n }else if(str[0]=='p'){\n int val=peek();\n if(val!=-1){\n cout<<val<<endl;\n }\n }else break;\n }\n\n\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class PriorityQueue {\r\n ArrayList<Integer> data;\r\n\r\n public PriorityQueue() {\r\n data = new ArrayList<>();\r\n }\r\n\r\n public void add(int val) {\r\n // write your code here\r\n }\r\n\r\n public int remove() {\r\n // write your code here\r\n }\r\n\r\n public int peek() {\r\n // write your code here\r\n }\r\n\r\n public int size() {\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n PriorityQueue qu = new PriorityQueue();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"add\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n qu.add(val);\r\n } else if (str.startsWith(\"remove\")) {\r\n int val = qu.remove();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"peek\")) {\r\n int val = qu.peek();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(qu.size());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"data=[]\n\n\n\ndef size():\n #write your code here\n\n\ndef add(val):\n #write your code here\n \n\n\ndef _remove():\n #write your code here\n \n \ndef peek():\n #write your code here\n\n\n\nwhile(1):\n s=input()\n if s[0]=='a':\n num=s[4:]\n val=int(num)\n add(val)\n \n elif s[0]=='r':\n val=_remove()\n if val!=-1:\n print(val,end=\"\\n\")\n \n elif s[0]=='p':\n val=peek()\n if val!=-1:\n print(val,end=\"\\n\")\n \n else:\n break"}},"points":10,"difficulty":"easy","sampleInput":"add 10\r\nadd 20\r\nadd 30\r\nadd 40\r\npeek\r\nadd 50\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nquit","sampleOutput":"10\r\n10\r\n10\r\n20\r\n20\r\n30\r\n30\r\n40\r\n40\r\n50","questionVideo":"https://www.youtube.com/embed/qroD9lvgGQ4","hints":[],"associated":[{"id":"03e74396-0654-427b-89cc-a036aff95062","name":"What is the time complexity of the add function? (Write PriorityQueue using Heap)","slug":"what-is-the-time-complexity-of-the-add-function-write-priorityqueue-using-heap","type":4},{"id":"ad093559-34a6-4b36-a552-ae582b2c3fa1","name":"What is the time complexity of the peek function? (Write PriorityQueue using Heap)","slug":"what-is-the-time-complexity-of-the-peek-function-write-priorityqueue-using-heap","type":4},{"id":"bf9ee41a-e5db-445b-a937-95137caf39a8","name":"With what data structure can a priority queue be implemented? (Write PriorityQueue using Heap)","slug":"with-what-data-structure-can-a-priority-queue-be-implemented-write-priorityqueue-using-heap","type":4},{"id":"cac495fc-2571-45bd-a6f6-bde22c5e2df0","name":"In a min-heap, smallest element is always at the which node? (Write PriorityQueue using Heap)","slug":"in-a-min-heap-smallest-element-is-always-at-the-which-node-write-priorityqueue-using-heap","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"96e8ea06-e1c1-4fd9-a112-be168da1a3cf","name":"Write Priority Queue Using Heap","slug":"write-priority-queue-using-heap","type":1}],"next":{"id":"42424e1a-0d5d-442a-ac4b-2d45e22950b1","name":"Write Priority Queue Using Heap","type":3,"slug":"write-priority-queue-using-heap"},"prev":{"id":"c9f81383-af18-49bf-b9d8-fed22f7eabea","name":"Merge K sorted lists","type":3,"slug":"merge-k-sorted-lists"}}}

Write Priority Queue Using Heap

1. You are required to complete the code of our Priority Queue class using the heap data structure. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video. 2. Here is the list of functions that you are supposed to complete: 2.1. add -> Should accept new data. 2.2. remove -> Should remove and return smallest value, if available or print "Underflow" otherwise and return -1. 2.3. peek -> Should return smallest value, if available or print "Underflow" otherwise and return -1. 2.4. size -> Should return the number of elements available. 3. Input and Output is managed for you.

{"id":"aa195dab-3822-46b8-941c-bb1a81be633f","name":"Write Priority Queue Using Heap","description":"1. You are required to complete the code of our Priority Queue class using the heap data structure. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video.\r\n2. Here is the list of functions that you are supposed to complete:\r\n 2.1. add -> Should accept new data.\r\n 2.2. remove -> Should remove and return smallest value, if available or print \r\n \"Underflow\" otherwise and return -1.\r\n 2.3. peek -> Should return smallest value, if available or print \"Underflow\" \r\n otherwise and return -1.\r\n 2.4. size -> Should return the number of elements available.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n vector<int> data;\n\n int _size() {\n //write your code here\n return 0;\n }\n\n \n\n \n void add(int val) {\n // write your code here\n }\n\n \n\n\n int _remove() {\n //write your code here\n return 0;\n }\n\n \n\n int peek() {\n //write your code here\n\n return 0;\n }\n\n \n\n\n\nint main(){\n \n\n while(1){\n string str;\n getline(cin,str);\n if(str[0]=='a'){\n string num=str.substr(4,str.length());\n int val=stoi(num);\n add(val);\n }else if(str[0]=='r'){\n int val=_remove();\n if(val!=-1){\n cout<<val<<endl;\n }\n }else if(str[0]=='p'){\n int val=peek();\n if(val!=-1){\n cout<<val<<endl;\n }\n }else break;\n }\n\n\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class PriorityQueue {\r\n ArrayList<Integer> data;\r\n\r\n public PriorityQueue() {\r\n data = new ArrayList<>();\r\n }\r\n\r\n public void add(int val) {\r\n // write your code here\r\n }\r\n\r\n public int remove() {\r\n // write your code here\r\n }\r\n\r\n public int peek() {\r\n // write your code here\r\n }\r\n\r\n public int size() {\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n PriorityQueue qu = new PriorityQueue();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"add\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n qu.add(val);\r\n } else if (str.startsWith(\"remove\")) {\r\n int val = qu.remove();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"peek\")) {\r\n int val = qu.peek();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(qu.size());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"data=[]\n\n\n\ndef size():\n #write your code here\n\n\ndef add(val):\n #write your code here\n \n\n\ndef _remove():\n #write your code here\n \n \ndef peek():\n #write your code here\n\n\n\nwhile(1):\n s=input()\n if s[0]=='a':\n num=s[4:]\n val=int(num)\n add(val)\n \n elif s[0]=='r':\n val=_remove()\n if val!=-1:\n print(val,end=\"\\n\")\n \n elif s[0]=='p':\n val=peek()\n if val!=-1:\n print(val,end=\"\\n\")\n \n else:\n break"}},"points":10,"difficulty":"easy","sampleInput":"add 10\r\nadd 20\r\nadd 30\r\nadd 40\r\npeek\r\nadd 50\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nquit","sampleOutput":"10\r\n10\r\n10\r\n20\r\n20\r\n30\r\n30\r\n40\r\n40\r\n50","questionVideo":"https://www.youtube.com/embed/qroD9lvgGQ4","hints":[],"associated":[{"id":"03e74396-0654-427b-89cc-a036aff95062","name":"What is the time complexity of the add function? (Write PriorityQueue using Heap)","slug":"what-is-the-time-complexity-of-the-add-function-write-priorityqueue-using-heap","type":4},{"id":"ad093559-34a6-4b36-a552-ae582b2c3fa1","name":"What is the time complexity of the peek function? (Write PriorityQueue using Heap)","slug":"what-is-the-time-complexity-of-the-peek-function-write-priorityqueue-using-heap","type":4},{"id":"bf9ee41a-e5db-445b-a937-95137caf39a8","name":"With what data structure can a priority queue be implemented? (Write PriorityQueue using Heap)","slug":"with-what-data-structure-can-a-priority-queue-be-implemented-write-priorityqueue-using-heap","type":4},{"id":"cac495fc-2571-45bd-a6f6-bde22c5e2df0","name":"In a min-heap, smallest element is always at the which node? (Write PriorityQueue using Heap)","slug":"in-a-min-heap-smallest-element-is-always-at-the-which-node-write-priorityqueue-using-heap","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"96e8ea06-e1c1-4fd9-a112-be168da1a3cf","name":"Write Priority Queue Using Heap","slug":"write-priority-queue-using-heap","type":1}],"next":{"id":"42424e1a-0d5d-442a-ac4b-2d45e22950b1","name":"Write Priority Queue Using Heap","type":3,"slug":"write-priority-queue-using-heap"},"prev":{"id":"c9f81383-af18-49bf-b9d8-fed22f7eabea","name":"Merge K sorted lists","type":3,"slug":"merge-k-sorted-lists"}}}
plane

Editor


Loading...

Write Priority Queue Using Heap

easy

1. You are required to complete the code of our Priority Queue class using the heap data structure. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video. 2. Here is the list of functions that you are supposed to complete: 2.1. add -> Should accept new data. 2.2. remove -> Should remove and return smallest value, if available or print "Underflow" otherwise and return -1. 2.3. peek -> Should return smallest value, if available or print "Underflow" otherwise and return -1. 2.4. size -> Should return the number of elements available. 3. Input and Output is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

add 10 add 20 add 30 add 40 peek add 50 peek remove peek remove peek remove peek remove peek quit

Sample Output

10 10 10 20 20 30 30 40 40 50

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode