{"id":"cc7e926c-0e01-4356-81dc-f533d0150491","name":"Two Stacks In An Array","description":"1. You are required to complete the code of our TwoStack class. The class should implement LIFO behaviours for two stacks in the same array.\r\n2. Here is the list of functions that you are supposed to complete\r\n 2.1. push1, push2 -> Should accept new data for appropriate stack if there is \r\n space available in the underlying array or print \"Stack overflow\" otherwise.\r\n 2.2. pop1, po2 -> Should remove and return data from appropriate stack if \r\n available or print \"Stack underflow\" otherwise and return -1.\r\n 2.3. top1, top2 -> Should return data from appropriate stack if available or print \r\n \"Stack underflow\" otherwise and return -1.\r\n 2.4. size1, size2 -> Should return the number of elements available in appropriate \r\n stack.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. All fns should be constant in time.\r\n2. Memory should be optimally used i.e. one of the stacks can use more elements in array but overflow should not happen before whole array is used.","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<bits/stdc++.h>\n#include<vector>\n\nusing namespace std;\n\n class TwoStack {\n public:\n vector<int> data;\n int tos1;\n int tos2;\n\n TwoStack(int cap) {\n // write your code here\n }\n\n int size1() {\n // write your code here\n }\n\n int size2() {\n // write your code here\n\n }\n\n void push1(int val) {\n // write your code here\n }\n\n void push2(int val) {\n // write your code here\n\n }\n\n int pop1() {\n // write your code here\n }\n\n int pop2() {\n // write your code here\n }\n\n int top1() {\n // write your code here\n }\n\n int top2() {\n // write your code here\n }\n };\n\n int main() {\nstring str;\n \nint cap;\ncin>>cap;\nTwoStack st(cap);\n \n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'p'&& str[4]=='1') {\n string ss = str.substr(6, 2);\n int n = stoi(ss);\n st.push1(n);\n\n }\n else if (str[0] == 't'&&str[3]=='1') {\n int tp=st.top1();\n if(tp!=-1) cout<<tp<<endl;\n }\n else if (str[0] == 'p'&&str[3]=='1') {\n int tp=st.pop1();\n if(tp!=-1) cout<<tp<<endl;\n }\n else if (str[0] == 'p'&& str[4]=='2') {\n string ss = str.substr(6, 2);\n int n = stoi(ss);\n st.push2(n);\n\n }\n else if (str[0] == 't'&&str[3]=='2') {\n int tp=st.top2();\n if(tp!=-1) cout<<tp<<endl;\n }\n else if (str[0] == 'p'&&str[3]=='2') {\n int tp=st.pop2();\n if(tp!=-1) cout<<tp<<endl;\n }\n \n }\n return 0;\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class TwoStack {\r\n int[] data;\r\n int tos1;\r\n int tos2;\r\n\r\n public TwoStack(int cap) {\r\n // write your code here\r\n }\r\n\r\n int size1() {\r\n // write your code here\r\n }\r\n\r\n int size2() {\r\n // write your code here\r\n }\r\n\r\n void push1(int val) {\r\n // write your code here\r\n }\r\n\r\n void push2(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop1() {\r\n // write your code here\r\n }\r\n\r\n int pop2() {\r\n // write your code here\r\n }\r\n\r\n int top1() {\r\n // write your code here\r\n }\r\n\r\n int top2() {\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 int n = Integer.parseInt(br.readLine());\r\n TwoStack st = new TwoStack(n);\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"push1\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push1(val);\r\n } else if (str.startsWith(\"pop1\")) {\r\n int val = st.pop1();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top1\")) {\r\n int val = st.top1();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size1\")) {\r\n System.out.println(st.size1());\r\n } else if (str.startsWith(\"push2\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push2(val);\r\n } else if (str.startsWith(\"pop2\")) {\r\n int val = st.pop2();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top2\")) {\r\n int val = st.top2();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size2\")) {\r\n System.out.println(st.size2());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class TwoStack :\n def __init__(self, cap) :\n # write your code here\n \n\n def size1(self) :\n # write your code here\n\n def size2(self) :\n # write your code here\n \n\n def push1(self, val) :\n # write your code here\n \n\n def push2(self,val) :\n # write your code here\n \n\n def pop1(self) :\n # write your code here\n \n\n def pop2(self) :\n # write your code here\n\n def top1(self) :\n # write your code here\n def top2(self) :\n # write your code here\n \n\ncap=int(input()) \nl1 = TwoStack(cap)\nwhile 1 > 0 :\n str = input()\n if str[0] == 'q':\n break\n \n elif str[0] == 'p'and str[4]=='1':\n val = int(str[-3] + str[-2])\n l1.push1(val)\n elif str[0] == 'p'and str[4]=='2':\n val = int(str[-3] + str[-2])\n l1.push2(val)\n \n elif str[0] == 't'and str[3]=='1':\n val=l1.top1()\n if val != -1:\n print(val)\n\n \n elif str[0] == 't'and str[3]=='2':\n val=l1.top2()\n if val != -1:\n print(val)\n\n elif str[0] == 'p'and str[3]=='1':\n val=l1.pop1()\n if val != -1:\n print(val)\n\n \n elif str[0] == 'p'and str[3]=='2':\n val=l1.pop2()\n if val != -1:\n print(val)\n\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.top1()\n if val != -1:\n print(val)"}},"points":10,"difficulty":"easy","sampleInput":"5\r\npush1 10\r\npush1 20\r\npush2 30\r\npush2 40\r\npush2 50\r\npush1 60\r\ntop1\r\npop1\r\ntop1\r\npop1\r\ntop2\r\npop2\r\ntop2\r\npop2\r\ntop1\r\npop1\r\ntop2\r\npop2\r\nquit","sampleOutput":"Stack overflow\r\n20\r\n20\r\n10\r\n10\r\n50\r\n50\r\n40\r\n40\r\nStack underflow\r\nStack underflow\r\n30\r\n30","questionVideo":"https://www.youtube.com/embed/HK9ZRogP_98","hints":[],"associated":[{"id":"1bdce537-e262-463e-b3a7-049e5848b323","name":"What is the time complexity of all the functions?(Q- Two Stacks In An Array)","slug":"what-is-the-time-complexity-of-all-the-functions-q-two-stacks-in-an-array","type":4},{"id":"c921462c-92bd-4e85-8fff-5caf58737e70","name":"What is the space complexity of his problem?(Q- Two Stacks In An Array)","slug":"what-is-the-space-complexity-of-his-problem-q-two-stacks-in-an-array","type":4},{"id":"cc76817a-5be8-4b46-a620-e7fced601432","name":"What approach does stack follow?(Q- Two Stacks In An Array)","slug":"what-approach-does-stack-follow-q-two-stacks-in-an-array","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":"89e61aa5-a39b-4307-9ae7-b0d30412b143","name":"Two Stacks In An Array","slug":"two-stacks-in-an-array","type":1}],"next":{"id":"26848229-caae-4b56-95de-fc032318f0e4","name":"Two stacks in an array","type":3,"slug":"two-stacks-in-an-array"},"prev":{"id":"52b7d165-6ad0-494f-bb3f-73b36b2e3710","name":"Stack to Queue - Remove Efficient","type":3,"slug":"stack-to-queue-remove-efficient"}}}

Two Stacks In An Array

1. You are required to complete the code of our TwoStack class. The class should implement LIFO behaviours for two stacks in the same array. 2. Here is the list of functions that you are supposed to complete 2.1. push1, push2 -> Should accept new data for appropriate stack if there is space available in the underlying array or print "Stack overflow" otherwise. 2.2. pop1, po2 -> Should remove and return data from appropriate stack if available or print "Stack underflow" otherwise and return -1. 2.3. top1, top2 -> Should return data from appropriate stack if available or print "Stack underflow" otherwise and return -1. 2.4. size1, size2 -> Should return the number of elements available in appropriate stack. 3. Input and Output is managed for you.

{"id":"cc7e926c-0e01-4356-81dc-f533d0150491","name":"Two Stacks In An Array","description":"1. You are required to complete the code of our TwoStack class. The class should implement LIFO behaviours for two stacks in the same array.\r\n2. Here is the list of functions that you are supposed to complete\r\n 2.1. push1, push2 -> Should accept new data for appropriate stack if there is \r\n space available in the underlying array or print \"Stack overflow\" otherwise.\r\n 2.2. pop1, po2 -> Should remove and return data from appropriate stack if \r\n available or print \"Stack underflow\" otherwise and return -1.\r\n 2.3. top1, top2 -> Should return data from appropriate stack if available or print \r\n \"Stack underflow\" otherwise and return -1.\r\n 2.4. size1, size2 -> Should return the number of elements available in appropriate \r\n stack.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. All fns should be constant in time.\r\n2. Memory should be optimally used i.e. one of the stacks can use more elements in array but overflow should not happen before whole array is used.","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<bits/stdc++.h>\n#include<vector>\n\nusing namespace std;\n\n class TwoStack {\n public:\n vector<int> data;\n int tos1;\n int tos2;\n\n TwoStack(int cap) {\n // write your code here\n }\n\n int size1() {\n // write your code here\n }\n\n int size2() {\n // write your code here\n\n }\n\n void push1(int val) {\n // write your code here\n }\n\n void push2(int val) {\n // write your code here\n\n }\n\n int pop1() {\n // write your code here\n }\n\n int pop2() {\n // write your code here\n }\n\n int top1() {\n // write your code here\n }\n\n int top2() {\n // write your code here\n }\n };\n\n int main() {\nstring str;\n \nint cap;\ncin>>cap;\nTwoStack st(cap);\n \n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'p'&& str[4]=='1') {\n string ss = str.substr(6, 2);\n int n = stoi(ss);\n st.push1(n);\n\n }\n else if (str[0] == 't'&&str[3]=='1') {\n int tp=st.top1();\n if(tp!=-1) cout<<tp<<endl;\n }\n else if (str[0] == 'p'&&str[3]=='1') {\n int tp=st.pop1();\n if(tp!=-1) cout<<tp<<endl;\n }\n else if (str[0] == 'p'&& str[4]=='2') {\n string ss = str.substr(6, 2);\n int n = stoi(ss);\n st.push2(n);\n\n }\n else if (str[0] == 't'&&str[3]=='2') {\n int tp=st.top2();\n if(tp!=-1) cout<<tp<<endl;\n }\n else if (str[0] == 'p'&&str[3]=='2') {\n int tp=st.pop2();\n if(tp!=-1) cout<<tp<<endl;\n }\n \n }\n return 0;\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class TwoStack {\r\n int[] data;\r\n int tos1;\r\n int tos2;\r\n\r\n public TwoStack(int cap) {\r\n // write your code here\r\n }\r\n\r\n int size1() {\r\n // write your code here\r\n }\r\n\r\n int size2() {\r\n // write your code here\r\n }\r\n\r\n void push1(int val) {\r\n // write your code here\r\n }\r\n\r\n void push2(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop1() {\r\n // write your code here\r\n }\r\n\r\n int pop2() {\r\n // write your code here\r\n }\r\n\r\n int top1() {\r\n // write your code here\r\n }\r\n\r\n int top2() {\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 int n = Integer.parseInt(br.readLine());\r\n TwoStack st = new TwoStack(n);\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"push1\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push1(val);\r\n } else if (str.startsWith(\"pop1\")) {\r\n int val = st.pop1();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top1\")) {\r\n int val = st.top1();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size1\")) {\r\n System.out.println(st.size1());\r\n } else if (str.startsWith(\"push2\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push2(val);\r\n } else if (str.startsWith(\"pop2\")) {\r\n int val = st.pop2();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top2\")) {\r\n int val = st.top2();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size2\")) {\r\n System.out.println(st.size2());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class TwoStack :\n def __init__(self, cap) :\n # write your code here\n \n\n def size1(self) :\n # write your code here\n\n def size2(self) :\n # write your code here\n \n\n def push1(self, val) :\n # write your code here\n \n\n def push2(self,val) :\n # write your code here\n \n\n def pop1(self) :\n # write your code here\n \n\n def pop2(self) :\n # write your code here\n\n def top1(self) :\n # write your code here\n def top2(self) :\n # write your code here\n \n\ncap=int(input()) \nl1 = TwoStack(cap)\nwhile 1 > 0 :\n str = input()\n if str[0] == 'q':\n break\n \n elif str[0] == 'p'and str[4]=='1':\n val = int(str[-3] + str[-2])\n l1.push1(val)\n elif str[0] == 'p'and str[4]=='2':\n val = int(str[-3] + str[-2])\n l1.push2(val)\n \n elif str[0] == 't'and str[3]=='1':\n val=l1.top1()\n if val != -1:\n print(val)\n\n \n elif str[0] == 't'and str[3]=='2':\n val=l1.top2()\n if val != -1:\n print(val)\n\n elif str[0] == 'p'and str[3]=='1':\n val=l1.pop1()\n if val != -1:\n print(val)\n\n \n elif str[0] == 'p'and str[3]=='2':\n val=l1.pop2()\n if val != -1:\n print(val)\n\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.top1()\n if val != -1:\n print(val)"}},"points":10,"difficulty":"easy","sampleInput":"5\r\npush1 10\r\npush1 20\r\npush2 30\r\npush2 40\r\npush2 50\r\npush1 60\r\ntop1\r\npop1\r\ntop1\r\npop1\r\ntop2\r\npop2\r\ntop2\r\npop2\r\ntop1\r\npop1\r\ntop2\r\npop2\r\nquit","sampleOutput":"Stack overflow\r\n20\r\n20\r\n10\r\n10\r\n50\r\n50\r\n40\r\n40\r\nStack underflow\r\nStack underflow\r\n30\r\n30","questionVideo":"https://www.youtube.com/embed/HK9ZRogP_98","hints":[],"associated":[{"id":"1bdce537-e262-463e-b3a7-049e5848b323","name":"What is the time complexity of all the functions?(Q- Two Stacks In An Array)","slug":"what-is-the-time-complexity-of-all-the-functions-q-two-stacks-in-an-array","type":4},{"id":"c921462c-92bd-4e85-8fff-5caf58737e70","name":"What is the space complexity of his problem?(Q- Two Stacks In An Array)","slug":"what-is-the-space-complexity-of-his-problem-q-two-stacks-in-an-array","type":4},{"id":"cc76817a-5be8-4b46-a620-e7fced601432","name":"What approach does stack follow?(Q- Two Stacks In An Array)","slug":"what-approach-does-stack-follow-q-two-stacks-in-an-array","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":"89e61aa5-a39b-4307-9ae7-b0d30412b143","name":"Two Stacks In An Array","slug":"two-stacks-in-an-array","type":1}],"next":{"id":"26848229-caae-4b56-95de-fc032318f0e4","name":"Two stacks in an array","type":3,"slug":"two-stacks-in-an-array"},"prev":{"id":"52b7d165-6ad0-494f-bb3f-73b36b2e3710","name":"Stack to Queue - Remove Efficient","type":3,"slug":"stack-to-queue-remove-efficient"}}}
plane

Editor


Loading...

Two Stacks In An Array

easy

1. You are required to complete the code of our TwoStack class. The class should implement LIFO behaviours for two stacks in the same array. 2. Here is the list of functions that you are supposed to complete 2.1. push1, push2 -> Should accept new data for appropriate stack if there is space available in the underlying array or print "Stack overflow" otherwise. 2.2. pop1, po2 -> Should remove and return data from appropriate stack if available or print "Stack underflow" otherwise and return -1. 2.3. top1, top2 -> Should return data from appropriate stack if available or print "Stack underflow" otherwise and return -1. 2.4. size1, size2 -> Should return the number of elements available in appropriate stack. 3. Input and Output is managed for you.

Constraints

1. All fns should be constant in time. 2. Memory should be optimally used i.e. one of the stacks can use more elements in array but overflow should not happen before whole array is used.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

5 push1 10 push1 20 push2 30 push2 40 push2 50 push1 60 top1 pop1 top1 pop1 top2 pop2 top2 pop2 top1 pop1 top2 pop2 quit

Sample Output

Stack overflow 20 20 10 10 50 50 40 40 Stack underflow Stack underflow 30 30

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode