{"id":"bf0714c0-3178-44a9-9230-fcdef4051984","name":"Queue To Stack Adapter - Pop 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 -> pop, top and size should work in constant time. push should work in linear time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Note -&gt; pop, top and size should work in constant time. push 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/4uHZxkUw-F4","hints":[],"associated":[{"id":"27ba8533-9e71-43a4-bc3e-205ac958f258","name":"Time Compexiety of push and pop in Queue:(Queue To Stack Adapter- Pop efficient)","slug":"time-compexiety-of-push-and-pop-in-queue-queue-to-stack-adapter-pop-efficient","type":4},{"id":"65c8bfd0-7cc6-4593-8948-2d92f15c0510","name":"Which of the following is of LIFO type?(Queue To Stack Adapter- Pop efficient)","slug":"which-of-the-following-is-of-lifo-type-queue-to-stack-adapter-pop-efficient","type":4},{"id":"d5d351f6-21ef-47b8-9f3a-c5434ea82c7b","name":"Time Compexiety of push and pop in Stack:(Queue To Stack Adapter- Pop efficient)","slug":"time-compexiety-of-push-and-pop-in-stack-queue-to-stack-adapter-pop-efficient","type":4},{"id":"e5ff8a93-c34a-4f7b-b407-4ce156b22046","name":"Which of the following is of FIFO type?(Queue To Stack Adapter- Pop efficient)","slug":"which-of-the-following-is-of-fifo-type-queue-to-stack-adapter-pop-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":"5d8266e1-96e7-402d-b115-6130f3ceb1f9","name":"Queue To Stack Adapter - Pop Efficient","slug":"queue-to-stack-adapter-pop-efficient","type":1}],"next":{"id":"9c2a7177-077d-4a7c-bc56-4e4d69033cd6","name":"Queue to Stack Adapter - Pop Efficient","type":3,"slug":"queue-to-stack-adapter-pop-efficient"},"prev":{"id":"6adb8c99-c3ba-4952-b6ea-7536c79a6de3","name":"Queue to Stack Adapter - Push Efficient","type":3,"slug":"queue-to-stack-adapter-push-efficient"}}}

Queue To Stack Adapter - Pop 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 -> pop, top and size should work in constant time. push should work in linear time.

{"id":"bf0714c0-3178-44a9-9230-fcdef4051984","name":"Queue To Stack Adapter - Pop 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 -> pop, top and size should work in constant time. push should work in linear time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Note -&gt; pop, top and size should work in constant time. push 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/4uHZxkUw-F4","hints":[],"associated":[{"id":"27ba8533-9e71-43a4-bc3e-205ac958f258","name":"Time Compexiety of push and pop in Queue:(Queue To Stack Adapter- Pop efficient)","slug":"time-compexiety-of-push-and-pop-in-queue-queue-to-stack-adapter-pop-efficient","type":4},{"id":"65c8bfd0-7cc6-4593-8948-2d92f15c0510","name":"Which of the following is of LIFO type?(Queue To Stack Adapter- Pop efficient)","slug":"which-of-the-following-is-of-lifo-type-queue-to-stack-adapter-pop-efficient","type":4},{"id":"d5d351f6-21ef-47b8-9f3a-c5434ea82c7b","name":"Time Compexiety of push and pop in Stack:(Queue To Stack Adapter- Pop efficient)","slug":"time-compexiety-of-push-and-pop-in-stack-queue-to-stack-adapter-pop-efficient","type":4},{"id":"e5ff8a93-c34a-4f7b-b407-4ce156b22046","name":"Which of the following is of FIFO type?(Queue To Stack Adapter- Pop efficient)","slug":"which-of-the-following-is-of-fifo-type-queue-to-stack-adapter-pop-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":"5d8266e1-96e7-402d-b115-6130f3ceb1f9","name":"Queue To Stack Adapter - Pop Efficient","slug":"queue-to-stack-adapter-pop-efficient","type":1}],"next":{"id":"9c2a7177-077d-4a7c-bc56-4e4d69033cd6","name":"Queue to Stack Adapter - Pop Efficient","type":3,"slug":"queue-to-stack-adapter-pop-efficient"},"prev":{"id":"6adb8c99-c3ba-4952-b6ea-7536c79a6de3","name":"Queue to Stack Adapter - Push Efficient","type":3,"slug":"queue-to-stack-adapter-push-efficient"}}}
plane

Editor


Loading...

Queue To Stack Adapter - Pop 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 -> pop, top and size should work in constant time. push should work in linear time.

Constraints

Note -> pop, top and size should work in constant time. push 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