{"id":"a245095c-282d-4516-9542-b15839449d21","name":"Queue To Stack Adapter - Push Efficient","description":"1. You are required to complete the code of our QueueToStackAdapter class. \r\n2. As data members you've two queues available - mainQ and helperQ. mainQ is to contain data and helperQ is to assist in operations. (This is cryptic - take hint from video)\r\n3. Here is the list of functions that you are supposed to complete\r\n 3.1. push -> Should accept new data in LIFO manner.\r\n 3.2. pop -> Should remove and return data in LIFO manner. If not available, print \r\n Stack underflow\" and return -1.\r\n 3.3. top -> Should return data in LIFO manner. If not available, print \"Stack \r\n underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the stack.\r\n4. Input and Output is managed for you.\r\n\r\nNote -> push and size should work in constant time. pop and top should work in linear time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Note -&gt; push and size should work in constant time. pop and top should work in linear time.","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\n#include <queue>\nusing namespace std;\n\nclass QueueToStackAdapter{\n public:\n queue<int> mainQ;\n queue<int> helperQ;\n \n int size(){\n // write your code here\n }\n \n void push(int data){\n // write your code here\n }\n\n int top(){\n // write your code here\n }\n\n int pop(){\n // write your code here\n }\n};\n\nint main(){\n QueueToStackAdapter st;\n string str;\n cin>>str;\n \n while(str!=\"quit\"){\n if(str==\"push\"){\n int val;\n cin>>val;\n st.push(val);\n }\n else if(str==\"pop\"){\n int val = st.pop();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"top\"){\n int val = st.top();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"size\"){\n cout<<st.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 QueueToStackAdapter {\r\n Queue<Integer> mainQ;\r\n Queue<Integer> helperQ;\r\n\r\n public QueueToStackAdapter() {\r\n mainQ = new ArrayDeque<>();\r\n helperQ = new ArrayDeque<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void push(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop() {\r\n // write your code here\r\n }\r\n\r\n int top() {\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 QueueToStackAdapter st = new QueueToStackAdapter();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"push\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if (str.startsWith(\"pop\")) {\r\n int val = st.pop();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top\")) {\r\n int val = st.top();\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(st.size());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"import threading, queue\n\nclass stack:\n def __init__(self):\n self.mainQ = queue.Queue()\n self.helperQ = queue.Queue()\n \n def size(self):\n # write your code here\n \n def push(self, data):\n # write your code here\n\n def top(self):\n # write your code here\n \n def pop(self):\n # write your code here\n \ndef main():\n st = stack();\n sr = input().strip();\n srP = sr.split(\" \");\n \n while(srP[0]!=\"quit\"):\n \n if(srP[0].strip()==\"push\"):\n st.push(int(srP[1]));\n \n elif(srP[0].strip()==\"pop\"):\n val = st.pop();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"top\"):\n val = st.top();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"size\"):\n print(st.size());\n \n sr = input().strip();\n srP = sr.split(\" \");\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"push 10\r\npush 20\r\npush 5\r\npush 8\r\npush 2\r\npush 4\r\npush 11\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\nquit","sampleOutput":"11\r\n7\r\n11\r\n4\r\n6\r\n4\r\n2\r\n5\r\n2\r\n8\r\n4\r\n8\r\n5\r\n3\r\n5\r\n20\r\n2\r\n20\r\n10\r\n1\r\n10","questionVideo":"https://www.youtube.com/embed/BLrkF4-3zZ4","hints":[],"associated":[{"id":"1103498a-5c41-4541-9a71-759d11066827","name":"What is the Time Complexity of the pop or top function?(Q- queueToStackAdapterPushEfficient)","slug":"what-is-the-time-complexity-of-the-pop-or-top-function-q-queuetostackadapterpushefficient","type":4},{"id":"18a0d7a9-9da8-447d-a157-8a63f2d9a373","name":"In which function we are using the helper queue?(Q-queueToStackAdapterPushEfficient)","slug":"in-which-function-we-are-using-the-helper-queue-q-queuetostackadapterpushefficient","type":4},{"id":"acb8a715-37ed-4e51-ad02-e58cd3a11cdb","name":"What is the main use of the helper queue?(queueToStackAdapterPushEfficient)","slug":"what-is-the-main-use-of-the-helper-queue-queuetostackadapterpushefficient","type":4},{"id":"d588abef-27a0-41d5-9f46-22ad60a4dfea","name":"In which functions the time complexity is O(1)? (Q- queueToStackAdapterPushEfficient)","slug":"in-which-functions-the-time-complexity-is-o-1-q-queuetostackadapterpushefficient","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":"59efbdad-3a31-495a-b7fb-1b4b45ca499f","name":"Queue To Stack Adapter - Push Efficient","slug":"queue-to-stack-adapter-push-efficient","type":1}],"next":{"id":"6adb8c99-c3ba-4952-b6ea-7536c79a6de3","name":"Queue to Stack Adapter - Push Efficient","type":3,"slug":"queue-to-stack-adapter-push-efficient"},"prev":{"id":"b5f32ead-2068-4128-8215-b9ac64ff44a4","name":"Dynamic Queue","type":3,"slug":"dynamic-queue"}}}

Queue To Stack Adapter - Push Efficient

1. You are required to complete the code of our QueueToStackAdapter class. 2. As data members you've two queues available - mainQ and helperQ. mainQ is to contain data and helperQ is to assist in operations. (This is cryptic - take hint from video) 3. Here is the list of functions that you are supposed to complete 3.1. push -> Should accept new data in LIFO manner. 3.2. pop -> Should remove and return data in LIFO manner. If not available, print Stack underflow" and return -1. 3.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.4. size -> Should return the number of elements available in the stack. 4. Input and Output is managed for you. Note -> push and size should work in constant time. pop and top should work in linear time.

{"id":"a245095c-282d-4516-9542-b15839449d21","name":"Queue To Stack Adapter - Push Efficient","description":"1. You are required to complete the code of our QueueToStackAdapter class. \r\n2. As data members you've two queues available - mainQ and helperQ. mainQ is to contain data and helperQ is to assist in operations. (This is cryptic - take hint from video)\r\n3. Here is the list of functions that you are supposed to complete\r\n 3.1. push -> Should accept new data in LIFO manner.\r\n 3.2. pop -> Should remove and return data in LIFO manner. If not available, print \r\n Stack underflow\" and return -1.\r\n 3.3. top -> Should return data in LIFO manner. If not available, print \"Stack \r\n underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the stack.\r\n4. Input and Output is managed for you.\r\n\r\nNote -> push and size should work in constant time. pop and top should work in linear time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Note -&gt; push and size should work in constant time. pop and top should work in linear time.","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\n#include <queue>\nusing namespace std;\n\nclass QueueToStackAdapter{\n public:\n queue<int> mainQ;\n queue<int> helperQ;\n \n int size(){\n // write your code here\n }\n \n void push(int data){\n // write your code here\n }\n\n int top(){\n // write your code here\n }\n\n int pop(){\n // write your code here\n }\n};\n\nint main(){\n QueueToStackAdapter st;\n string str;\n cin>>str;\n \n while(str!=\"quit\"){\n if(str==\"push\"){\n int val;\n cin>>val;\n st.push(val);\n }\n else if(str==\"pop\"){\n int val = st.pop();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"top\"){\n int val = st.top();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"size\"){\n cout<<st.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 QueueToStackAdapter {\r\n Queue<Integer> mainQ;\r\n Queue<Integer> helperQ;\r\n\r\n public QueueToStackAdapter() {\r\n mainQ = new ArrayDeque<>();\r\n helperQ = new ArrayDeque<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void push(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop() {\r\n // write your code here\r\n }\r\n\r\n int top() {\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 QueueToStackAdapter st = new QueueToStackAdapter();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"push\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if (str.startsWith(\"pop\")) {\r\n int val = st.pop();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top\")) {\r\n int val = st.top();\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(st.size());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"import threading, queue\n\nclass stack:\n def __init__(self):\n self.mainQ = queue.Queue()\n self.helperQ = queue.Queue()\n \n def size(self):\n # write your code here\n \n def push(self, data):\n # write your code here\n\n def top(self):\n # write your code here\n \n def pop(self):\n # write your code here\n \ndef main():\n st = stack();\n sr = input().strip();\n srP = sr.split(\" \");\n \n while(srP[0]!=\"quit\"):\n \n if(srP[0].strip()==\"push\"):\n st.push(int(srP[1]));\n \n elif(srP[0].strip()==\"pop\"):\n val = st.pop();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"top\"):\n val = st.top();\n if(val != -1):\n print(val);\n \n elif(srP[0].strip()==\"size\"):\n print(st.size());\n \n sr = input().strip();\n srP = sr.split(\" \");\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"push 10\r\npush 20\r\npush 5\r\npush 8\r\npush 2\r\npush 4\r\npush 11\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\nquit","sampleOutput":"11\r\n7\r\n11\r\n4\r\n6\r\n4\r\n2\r\n5\r\n2\r\n8\r\n4\r\n8\r\n5\r\n3\r\n5\r\n20\r\n2\r\n20\r\n10\r\n1\r\n10","questionVideo":"https://www.youtube.com/embed/BLrkF4-3zZ4","hints":[],"associated":[{"id":"1103498a-5c41-4541-9a71-759d11066827","name":"What is the Time Complexity of the pop or top function?(Q- queueToStackAdapterPushEfficient)","slug":"what-is-the-time-complexity-of-the-pop-or-top-function-q-queuetostackadapterpushefficient","type":4},{"id":"18a0d7a9-9da8-447d-a157-8a63f2d9a373","name":"In which function we are using the helper queue?(Q-queueToStackAdapterPushEfficient)","slug":"in-which-function-we-are-using-the-helper-queue-q-queuetostackadapterpushefficient","type":4},{"id":"acb8a715-37ed-4e51-ad02-e58cd3a11cdb","name":"What is the main use of the helper queue?(queueToStackAdapterPushEfficient)","slug":"what-is-the-main-use-of-the-helper-queue-queuetostackadapterpushefficient","type":4},{"id":"d588abef-27a0-41d5-9f46-22ad60a4dfea","name":"In which functions the time complexity is O(1)? (Q- queueToStackAdapterPushEfficient)","slug":"in-which-functions-the-time-complexity-is-o-1-q-queuetostackadapterpushefficient","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":"59efbdad-3a31-495a-b7fb-1b4b45ca499f","name":"Queue To Stack Adapter - Push Efficient","slug":"queue-to-stack-adapter-push-efficient","type":1}],"next":{"id":"6adb8c99-c3ba-4952-b6ea-7536c79a6de3","name":"Queue to Stack Adapter - Push Efficient","type":3,"slug":"queue-to-stack-adapter-push-efficient"},"prev":{"id":"b5f32ead-2068-4128-8215-b9ac64ff44a4","name":"Dynamic Queue","type":3,"slug":"dynamic-queue"}}}
plane

Editor


Loading...

Queue To Stack Adapter - Push Efficient

easy

1. You are required to complete the code of our QueueToStackAdapter class. 2. As data members you've two queues available - mainQ and helperQ. mainQ is to contain data and helperQ is to assist in operations. (This is cryptic - take hint from video) 3. Here is the list of functions that you are supposed to complete 3.1. push -> Should accept new data in LIFO manner. 3.2. pop -> Should remove and return data in LIFO manner. If not available, print Stack underflow" and return -1. 3.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.4. size -> Should return the number of elements available in the stack. 4. Input and Output is managed for you. Note -> push and size should work in constant time. pop and top should work in linear time.

Constraints

Note -> push and size should work in constant time. pop and top should work in linear time.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

push 10 push 20 push 5 push 8 push 2 push 4 push 11 top size pop top size pop top size pop top size pop top size pop top size pop top size pop quit

Sample Output

11 7 11 4 6 4 2 5 2 8 4 8 5 3 5 20 2 20 10 1 10

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode