{"id":"cc352a4d-99b3-4e61-aa92-46e375ba5944","name":"Stack To Queue Adapter - Add 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; add and size should work in constant time. remove and peek should work in linear time.","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\n#include <stack>\nusing namespace std;\n\nclass StackToQueueAdapter{\n public:\n stack<int> mainS;\n stack<int> helperS;\n \n int size(){\n // write your code here\n }\n \n void add(int data){\n // write your code here\n }\n\n int peek(){\n // write your code here\n }\n\n int remove(){\n // write your code here\n }\n};\n\nint main(){\n StackToQueueAdapter q;\n string str;\n cin>>str;\n \n while(str!=\"quit\"){\n if(str==\"add\"){\n int val;\n cin>>val;\n q.add(val);\n }\n else if(str==\"remove\"){\n int val = q.remove();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"peek\"){\n int val = q.peek();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"size\"){\n cout<<q.size()<<endl;\n }\n cin>>str;\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 CustomQueue:\n def __init__(self):\n self.arr = [];\n \n def size(self):\n # write your code here\n \n def add(self, data):\n # write your code here\n\n def peek(self):\n # write your code here\n\n def remove(self):\n # write your code here\n \n def display(self):\n # write your code here\n \ndef main():\n q = CustomQueue();\n sr = input().strip();\n srP = sr.split(\" \");\n \n while(srP[0]!=\"quit\"):\n \n if(srP[0].strip()==\"add\"):\n q.add(int(srP[1]));\n \n elif(srP[0].strip()==\"remove\"):\n val = q.remove();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"peek\"):\n val = q.peek();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"size\"):\n print(q.size());\n \n elif(srP[0].strip()==\"display\"):\n q.display();\n \n sr = input().strip();\n srP = sr.split(\" \");\n \nmain()"}},"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/YKjh6ggBy-Q","hints":[],"associated":[{"id":"27019638-640d-4fde-83cb-913f660301d2","name":"What should be the time complexity of adding an element to the adapted queue in this approach?(tack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-adding-an-element-to-the-adapted-queue-in-this-approach-tack-to-queue-adapter-add-efficient","type":4},{"id":"748f1505-5ebf-47d0-8314-62f72f288033","name":"What should be the time complexity of getting the size of the adapted queue in this approach?(Qtack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-getting-the-size-of-the-adapted-queue-in-this-approach-qtack-to-queue-adapter-add-efficient","type":4},{"id":"b015aba1-70bb-4f1d-8604-912a48662ebb","name":"What should be the time complexity of removing an element to the adapted queue in this approach?(tack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-removing-an-element-to-the-adapted-queue-in-this-approach-tack-to-queue-adapter-add-efficient","type":4},{"id":"f72d10c4-0854-40f6-b775-5e6563d5dcd4","name":"What should be the time complexity of peek operation in this approach?(Q- tack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-peek-operation-in-this-approach-q-tack-to-queue-adapter-add-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":"a6a7b0b2-8f1b-4dd2-8b55-62c5a3bb595f","name":"Stack To Queue Adapter - Add Efficient","slug":"stack-to-queue-adapter-add-efficient","type":1}],"next":{"id":"e4c33276-c21a-421a-9931-52d163c57cf9","name":"Stack to Queue Adapter - Add Efficient","type":3,"slug":"stack-to-queue-adapter-add-efficient"},"prev":{"id":"9c2a7177-077d-4a7c-bc56-4e4d69033cd6","name":"Queue to Stack Adapter - Pop Efficient","type":3,"slug":"queue-to-stack-adapter-pop-efficient"}}}

Stack To Queue Adapter - Add 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":"cc352a4d-99b3-4e61-aa92-46e375ba5944","name":"Stack To Queue Adapter - Add 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; add and size should work in constant time. remove and peek should work in linear time.","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\n#include <stack>\nusing namespace std;\n\nclass StackToQueueAdapter{\n public:\n stack<int> mainS;\n stack<int> helperS;\n \n int size(){\n // write your code here\n }\n \n void add(int data){\n // write your code here\n }\n\n int peek(){\n // write your code here\n }\n\n int remove(){\n // write your code here\n }\n};\n\nint main(){\n StackToQueueAdapter q;\n string str;\n cin>>str;\n \n while(str!=\"quit\"){\n if(str==\"add\"){\n int val;\n cin>>val;\n q.add(val);\n }\n else if(str==\"remove\"){\n int val = q.remove();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"peek\"){\n int val = q.peek();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"size\"){\n cout<<q.size()<<endl;\n }\n cin>>str;\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 CustomQueue:\n def __init__(self):\n self.arr = [];\n \n def size(self):\n # write your code here\n \n def add(self, data):\n # write your code here\n\n def peek(self):\n # write your code here\n\n def remove(self):\n # write your code here\n \n def display(self):\n # write your code here\n \ndef main():\n q = CustomQueue();\n sr = input().strip();\n srP = sr.split(\" \");\n \n while(srP[0]!=\"quit\"):\n \n if(srP[0].strip()==\"add\"):\n q.add(int(srP[1]));\n \n elif(srP[0].strip()==\"remove\"):\n val = q.remove();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"peek\"):\n val = q.peek();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"size\"):\n print(q.size());\n \n elif(srP[0].strip()==\"display\"):\n q.display();\n \n sr = input().strip();\n srP = sr.split(\" \");\n \nmain()"}},"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/YKjh6ggBy-Q","hints":[],"associated":[{"id":"27019638-640d-4fde-83cb-913f660301d2","name":"What should be the time complexity of adding an element to the adapted queue in this approach?(tack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-adding-an-element-to-the-adapted-queue-in-this-approach-tack-to-queue-adapter-add-efficient","type":4},{"id":"748f1505-5ebf-47d0-8314-62f72f288033","name":"What should be the time complexity of getting the size of the adapted queue in this approach?(Qtack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-getting-the-size-of-the-adapted-queue-in-this-approach-qtack-to-queue-adapter-add-efficient","type":4},{"id":"b015aba1-70bb-4f1d-8604-912a48662ebb","name":"What should be the time complexity of removing an element to the adapted queue in this approach?(tack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-removing-an-element-to-the-adapted-queue-in-this-approach-tack-to-queue-adapter-add-efficient","type":4},{"id":"f72d10c4-0854-40f6-b775-5e6563d5dcd4","name":"What should be the time complexity of peek operation in this approach?(Q- tack To Queue Adapter - Add Efficient)","slug":"what-should-be-the-time-complexity-of-peek-operation-in-this-approach-q-tack-to-queue-adapter-add-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":"a6a7b0b2-8f1b-4dd2-8b55-62c5a3bb595f","name":"Stack To Queue Adapter - Add Efficient","slug":"stack-to-queue-adapter-add-efficient","type":1}],"next":{"id":"e4c33276-c21a-421a-9931-52d163c57cf9","name":"Stack to Queue Adapter - Add Efficient","type":3,"slug":"stack-to-queue-adapter-add-efficient"},"prev":{"id":"9c2a7177-077d-4a7c-bc56-4e4d69033cd6","name":"Queue to Stack Adapter - Pop Efficient","type":3,"slug":"queue-to-stack-adapter-pop-efficient"}}}
plane

Editor


Loading...

Stack To Queue Adapter - Add 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 -> add and size should work in constant time. remove and peek 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