{"id":"d3b3cc25-f607-4379-ab2f-3385b80c2f0c","name":"Stack To Queue Adapter - Remove Efficient","description":"1. You are required to complete the code of our StackToQueueAdapter class. The class should mimic the behaviour of a Queue and implement FIFO semantic.\r\n2. Here is the list of functions that are written in the class\r\n 2.1. add -> Accepts new data if there is space available in the underlying array or \r\n print \"Queue overflow\" otherwise.\r\n 2.2. remove -> Removes and returns value according to FIFO, if available or print \r\n \"Queue underflow\" otherwise and return -1.\r\n 2.3. peek -> Returns value according to FIFO, if available or print \"Queue \r\n underflow\" otherwise and return -1.\r\n 2.4. size -> Returns the number of elements available in the queue.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Note -&gt; remove, peek and size should work in constant time. add should work in linear time.","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<bits/stdc++.h>\nusing namespace std;\n\nclass StackToQueueAdapter {\npublic:\n stack <int> mainS;\n stack <int> helperS;\n\n int size() {\n // write your code here\n }\n\n void add(int val) {\n// write your code here\n }\n\n int Remove() {\n // write your code here\n }\n\n int peek() {\n // write your code here\n }\n};\n\nint main(){\n string str;\n StackToQueueAdapter qu;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'a') {\n string ss = str.substr(4, 2);\n int n = stoi(ss);\n qu.add(n);\n }\n else if (str[0] == 's') {\n cout << qu.size() << endl;\n }\n else if (str[0] == 'r') {\n int val = qu.Remove();\n if (val != -1) {\n cout << val << endl;\n }\n } else if (str[0] == 'p') {\n int val = qu.peek();\n if (val != -1) {\n cout << val << endl;\n }\n }\n\n }\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class StackToQueueAdapter {\r\n Stack<Integer> mainS;\r\n Stack<Integer> helperS;\r\n\r\n public StackToQueueAdapter() {\r\n mainS = new Stack<>();\r\n helperS = new Stack<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void add(int val) {\r\n // write your code here\r\n }\r\n\r\n int remove() {\r\n // write your code here\r\n }\r\n\r\n int peek() {\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 StackToQueueAdapter qu = new StackToQueueAdapter();\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":"class StackToQueueAdapter :\n def __init__(self) :\n self.mainS = []\n self.helperS = []\n \n\n def size(self) :\n # write your code here\n\n def Add(self, val) :\n # write your code here\n\n def removeFirst(self) :\n # write your code here\n\n def peek(self) :\n # write your code here\n\n\nl1 = StackToQueueAdapter()\nwhile 1 > 0 :\n str = input()\n if str[0] == 'q':\n break;\n \n elif str[0] == 'a':\n val = int(str[-3] + str[-2])\n l1.Add(val)\n \n elif str[0] == 'd':\n l1.display()\n \n elif str[0] == 's':\n \n print(l1.size())\n \n elif str[0] == 'r':\n \n val=l1.removeFirst()\n if val != -1:\n print(val)\n \n elif str[0] == 'p':\n \n val=l1.peek()\n if val != -1:\n print(val)"}},"points":10,"difficulty":"easy","sampleInput":"add 10\r\nadd 20\r\nadd 30\r\nadd 40\r\nadd 50\r\nremove\r\nremove\r\nadd 60\r\nadd 70\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\nquit","sampleOutput":"10\r\n20\r\n30\r\n30\r\n40\r\n40\r\n50\r\n50\r\n60\r\n60\r\n70\r\n70\r\nQueue underflow\r\nQueue underflow","questionVideo":"https://www.youtube.com/embed/tL1G0YX0ZZM","hints":[],"associated":[{"id":"34497021-54ab-4ba5-9c26-d5a2d356516f","name":"How many stacks are required to make 1 queue?(Q- Stack to Queue Adapter- Remove efficient)","slug":"how-many-stacks-are-required-to-make-1-queue-q-stack-to-queue-adapter-remove-efficient","type":4},{"id":"3dcd483f-82b6-4139-be4a-aa98c3ab21b8","name":"Which order does the queue follow ?(Q- Stack to Queue Adapter- Remove efficient)","slug":"which-order-does-the-queue-follow-q-stack-to-queue-adapter-remove-efficient","type":4},{"id":"bc41b640-0566-4509-8b52-b100d713070d","name":"What will be the size of the queue with regard to the main stack?(Q-Stack to Queue Adapter- Remove efficient)","slug":"what-will-be-the-size-of-the-queue-with-regard-to-the-main-stack-q-stack-to-queue-adapter-remove-efficient","type":4},{"id":"dd137d99-9531-4116-b3c6-e3eef1a0381b","name":"What will be the time complexity of adding an element?(Q- Stack to Queue Adapter- Remove efficient)","slug":"what-will-be-the-time-complexity-of-adding-an-element-q-stack-to-queue-adapter-remove-efficient","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":"9847c2b3-e3ad-4b1c-97d1-00206b1be68d","name":"Stacks And Queues For Beginners","slug":"stacks-and-queues-for-beginners","type":0},{"id":"c6d52185-37e5-4c81-9265-708787285124","name":"Stack To Queue Adapter - Remove Efficient","slug":"stack-to-queue-adapter-remove-efficient","type":1}],"next":{"id":"52b7d165-6ad0-494f-bb3f-73b36b2e3710","name":"Stack to Queue - Remove Efficient","type":3,"slug":"stack-to-queue-remove-efficient"},"prev":{"id":"e4c33276-c21a-421a-9931-52d163c57cf9","name":"Stack to Queue Adapter - Add Efficient","type":3,"slug":"stack-to-queue-adapter-add-efficient"}}}

Stack To Queue Adapter - Remove Efficient

1. You are required to complete the code of our StackToQueueAdapter class. The class should mimic the behaviour of a Queue and implement FIFO semantic. 2. Here is the list of functions that are written in the class 2.1. add -> Accepts new data if there is space available in the underlying array or print "Queue overflow" otherwise. 2.2. remove -> Removes and returns value according to FIFO, if available or print "Queue underflow" otherwise and return -1. 2.3. peek -> Returns value according to FIFO, if available or print "Queue underflow" otherwise and return -1. 2.4. size -> Returns the number of elements available in the queue. 3. Input and Output is managed for you.

{"id":"d3b3cc25-f607-4379-ab2f-3385b80c2f0c","name":"Stack To Queue Adapter - Remove Efficient","description":"1. You are required to complete the code of our StackToQueueAdapter class. The class should mimic the behaviour of a Queue and implement FIFO semantic.\r\n2. Here is the list of functions that are written in the class\r\n 2.1. add -> Accepts new data if there is space available in the underlying array or \r\n print \"Queue overflow\" otherwise.\r\n 2.2. remove -> Removes and returns value according to FIFO, if available or print \r\n \"Queue underflow\" otherwise and return -1.\r\n 2.3. peek -> Returns value according to FIFO, if available or print \"Queue \r\n underflow\" otherwise and return -1.\r\n 2.4. size -> Returns the number of elements available in the queue.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Note -&gt; remove, peek and size should work in constant time. add should work in linear time.","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<bits/stdc++.h>\nusing namespace std;\n\nclass StackToQueueAdapter {\npublic:\n stack <int> mainS;\n stack <int> helperS;\n\n int size() {\n // write your code here\n }\n\n void add(int val) {\n// write your code here\n }\n\n int Remove() {\n // write your code here\n }\n\n int peek() {\n // write your code here\n }\n};\n\nint main(){\n string str;\n StackToQueueAdapter qu;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'a') {\n string ss = str.substr(4, 2);\n int n = stoi(ss);\n qu.add(n);\n }\n else if (str[0] == 's') {\n cout << qu.size() << endl;\n }\n else if (str[0] == 'r') {\n int val = qu.Remove();\n if (val != -1) {\n cout << val << endl;\n }\n } else if (str[0] == 'p') {\n int val = qu.peek();\n if (val != -1) {\n cout << val << endl;\n }\n }\n\n }\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class StackToQueueAdapter {\r\n Stack<Integer> mainS;\r\n Stack<Integer> helperS;\r\n\r\n public StackToQueueAdapter() {\r\n mainS = new Stack<>();\r\n helperS = new Stack<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void add(int val) {\r\n // write your code here\r\n }\r\n\r\n int remove() {\r\n // write your code here\r\n }\r\n\r\n int peek() {\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 StackToQueueAdapter qu = new StackToQueueAdapter();\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":"class StackToQueueAdapter :\n def __init__(self) :\n self.mainS = []\n self.helperS = []\n \n\n def size(self) :\n # write your code here\n\n def Add(self, val) :\n # write your code here\n\n def removeFirst(self) :\n # write your code here\n\n def peek(self) :\n # write your code here\n\n\nl1 = StackToQueueAdapter()\nwhile 1 > 0 :\n str = input()\n if str[0] == 'q':\n break;\n \n elif str[0] == 'a':\n val = int(str[-3] + str[-2])\n l1.Add(val)\n \n elif str[0] == 'd':\n l1.display()\n \n elif str[0] == 's':\n \n print(l1.size())\n \n elif str[0] == 'r':\n \n val=l1.removeFirst()\n if val != -1:\n print(val)\n \n elif str[0] == 'p':\n \n val=l1.peek()\n if val != -1:\n print(val)"}},"points":10,"difficulty":"easy","sampleInput":"add 10\r\nadd 20\r\nadd 30\r\nadd 40\r\nadd 50\r\nremove\r\nremove\r\nadd 60\r\nadd 70\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\nquit","sampleOutput":"10\r\n20\r\n30\r\n30\r\n40\r\n40\r\n50\r\n50\r\n60\r\n60\r\n70\r\n70\r\nQueue underflow\r\nQueue underflow","questionVideo":"https://www.youtube.com/embed/tL1G0YX0ZZM","hints":[],"associated":[{"id":"34497021-54ab-4ba5-9c26-d5a2d356516f","name":"How many stacks are required to make 1 queue?(Q- Stack to Queue Adapter- Remove efficient)","slug":"how-many-stacks-are-required-to-make-1-queue-q-stack-to-queue-adapter-remove-efficient","type":4},{"id":"3dcd483f-82b6-4139-be4a-aa98c3ab21b8","name":"Which order does the queue follow ?(Q- Stack to Queue Adapter- Remove efficient)","slug":"which-order-does-the-queue-follow-q-stack-to-queue-adapter-remove-efficient","type":4},{"id":"bc41b640-0566-4509-8b52-b100d713070d","name":"What will be the size of the queue with regard to the main stack?(Q-Stack to Queue Adapter- Remove efficient)","slug":"what-will-be-the-size-of-the-queue-with-regard-to-the-main-stack-q-stack-to-queue-adapter-remove-efficient","type":4},{"id":"dd137d99-9531-4116-b3c6-e3eef1a0381b","name":"What will be the time complexity of adding an element?(Q- Stack to Queue Adapter- Remove efficient)","slug":"what-will-be-the-time-complexity-of-adding-an-element-q-stack-to-queue-adapter-remove-efficient","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":"9847c2b3-e3ad-4b1c-97d1-00206b1be68d","name":"Stacks And Queues For Beginners","slug":"stacks-and-queues-for-beginners","type":0},{"id":"c6d52185-37e5-4c81-9265-708787285124","name":"Stack To Queue Adapter - Remove Efficient","slug":"stack-to-queue-adapter-remove-efficient","type":1}],"next":{"id":"52b7d165-6ad0-494f-bb3f-73b36b2e3710","name":"Stack to Queue - Remove Efficient","type":3,"slug":"stack-to-queue-remove-efficient"},"prev":{"id":"e4c33276-c21a-421a-9931-52d163c57cf9","name":"Stack to Queue Adapter - Add Efficient","type":3,"slug":"stack-to-queue-adapter-add-efficient"}}}
plane

Editor


Loading...

Stack To Queue Adapter - Remove Efficient

easy

1. You are required to complete the code of our StackToQueueAdapter class. The class should mimic the behaviour of a Queue and implement FIFO semantic. 2. Here is the list of functions that are written in the class 2.1. add -> Accepts new data if there is space available in the underlying array or print "Queue overflow" otherwise. 2.2. remove -> Removes and returns value according to FIFO, if available or print "Queue underflow" otherwise and return -1. 2.3. peek -> Returns value according to FIFO, if available or print "Queue underflow" otherwise and return -1. 2.4. size -> Returns the number of elements available in the queue. 3. Input and Output is managed for you.

Constraints

Note -> remove, peek and size should work in constant time. add should work in linear time.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

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

Sample Output

10 20 30 30 40 40 50 50 60 60 70 70 Queue underflow Queue underflow

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode