{"id":"b7c1eef4-22a0-401e-96ff-205834e45e20","name":"Dynamic Queue","description":"1. You are required to complete the code of our CustomQueue 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\n 2.5. display -> Prints the elements of queue in FIFO manner (space-separated) \r\n ending with a line-break.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\nusing namespace std;\n\nclass DynamicQueue{\n public:\n int *arr;\n int sizeQ;\n int front;\n int arrSize;\n \n DynamicQueue(int cap){\n sizeQ = 0;\n front = 0;\n arr = new int[cap];\n arrSize = cap;\n }\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 void display() {\n // write your code here\n }\n};\n\nint main(){\n int n;\n cin>>n;\n DynamicQueue q(n);\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 else if(str==\"display\"){\n q.display();\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 CustomQueue {\r\n int[] data;\r\n int front;\r\n int size;\r\n\r\n public CustomQueue(int cap) {\r\n data = new int[cap];\r\n front = 0;\r\n size = 0;\r\n }\r\n\r\n int size() {\r\n // write ur code here\r\n return size;\r\n }\r\n\r\n void display() {\r\n // write ur code here\r\n for(int i = 0; i < size; i++){\r\n int idx = (front + i) % data.length;\r\n System.out.print(data[idx] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n // change this code\r\n void add(int val) {\r\n // write ur code here\r\n if(size == data.length){\r\n System.out.println(\"Queue overflow\");\r\n } else {\r\n int idx = (front + size) % data.length;\r\n data[idx] = val;\r\n size++;\r\n }\r\n }\r\n\r\n int remove() {\r\n // write ur code here\r\n if(size == 0){\r\n System.out.println(\"Queue underflow\");\r\n return -1;\r\n } else {\r\n int val = data[front];\r\n\r\n front = (front + 1) % data.length;\r\n size--;\r\n\r\n return val;\r\n }\r\n }\r\n\r\n int peek() {\r\n // write ur code here\r\n if(size == 0){\r\n System.out.println(\"Queue underflow\");\r\n return -1;\r\n } else {\r\n int val = data[front];\r\n return val;\r\n }\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 CustomQueue qu = new CustomQueue(n);\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 } else if(str.startsWith(\"display\")){\r\n qu.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class DynamicQueue:\n def __init__(self, cap):\n self.sizeQ = 0;\n self.front = 0;\n self.arrLen = cap;\n self.arr = [0] * self.arrLen;\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 n = int(input());\n q = DynamicQueue(n);\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":"5\r\nadd 10\r\ndisplay\r\nadd 20\r\ndisplay\r\nadd 30\r\ndisplay\r\nadd 40\r\ndisplay\r\nadd 50\r\ndisplay\r\nadd 60\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\nquit","sampleOutput":"10 \r\n10 20 \r\n10 20 30 \r\n10 20 30 40 \r\n10 20 30 40 50 \r\n10 20 30 40 50 60 \r\n10\r\n10\r\n20 30 40 50 60 \r\n20\r\n20\r\n30 40 50 60 \r\n30\r\n30\r\n40 50 60 \r\n40\r\n40\r\n50 60 \r\n50\r\n50\r\n60 \r\n60\r\n60","questionVideo":"https://www.youtube.com/embed/gluUcS0Rue8","hints":[],"associated":[{"id":"2422da0f-e956-4dbc-9294-5973258ac978","name":"The space complexity for insertion of an element in a dynamic queue (Q-Dynamic Queue)","slug":"the-space-complexity-for-insertion-of-an-element-in-a-dynamic-queue-q-dynamic-queue","type":4},{"id":"8bc35a8d-0f09-4cd8-801d-1ca1e13c50bb","name":"Which of the following principle does Queue use?(Q- Dynamic Queue)","slug":"which-of-the-following-principle-does-queue-use-q-dynamic-queue","type":4},{"id":"a72f72b5-ee70-4ce4-84b1-ab48a57fadc6","name":"The time complexity for insertion of an element in a dynamic queue at the end by increasing the size will be(Q- Dynamic Queue)","slug":"the-time-complexity-for-insertion-of-an-element-in-a-dynamic-queue-at-the-end-by-increasing-the-size-will-be-q-dynamic-queue","type":4},{"id":"e95ae1c3-3ae0-4dc6-9835-f2121f8225cf","name":"The time complexity for insertion of an element in a dynamic queue at the end without increasing the size will be (Q- Dynamic Queue)","slug":"the-time-complexity-for-insertion-of-an-element-in-a-dynamic-queue-at-the-end-without-increasing-the-size-will-be-q-dynamic-queue","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":"5299a556-123f-43d6-8f74-c6426e9903d4","name":"Dynamic Queue","slug":"dynamic-queue","type":1}],"next":{"id":"b5f32ead-2068-4128-8215-b9ac64ff44a4","name":"Dynamic Queue","type":3,"slug":"dynamic-queue"},"prev":{"id":"fa76c7e5-a165-4e8d-b34a-1c7b1ad316b4","name":"Normal Queue","type":3,"slug":"normal-queue"}}}

Dynamic Queue

1. You are required to complete the code of our CustomQueue 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. 2.5. display -> Prints the elements of queue in FIFO manner (space-separated) ending with a line-break. 3. Input and Output is managed for you.

{"id":"b7c1eef4-22a0-401e-96ff-205834e45e20","name":"Dynamic Queue","description":"1. You are required to complete the code of our CustomQueue 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\n 2.5. display -> Prints the elements of queue in FIFO manner (space-separated) \r\n ending with a line-break.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\nusing namespace std;\n\nclass DynamicQueue{\n public:\n int *arr;\n int sizeQ;\n int front;\n int arrSize;\n \n DynamicQueue(int cap){\n sizeQ = 0;\n front = 0;\n arr = new int[cap];\n arrSize = cap;\n }\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 void display() {\n // write your code here\n }\n};\n\nint main(){\n int n;\n cin>>n;\n DynamicQueue q(n);\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 else if(str==\"display\"){\n q.display();\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 CustomQueue {\r\n int[] data;\r\n int front;\r\n int size;\r\n\r\n public CustomQueue(int cap) {\r\n data = new int[cap];\r\n front = 0;\r\n size = 0;\r\n }\r\n\r\n int size() {\r\n // write ur code here\r\n return size;\r\n }\r\n\r\n void display() {\r\n // write ur code here\r\n for(int i = 0; i < size; i++){\r\n int idx = (front + i) % data.length;\r\n System.out.print(data[idx] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n // change this code\r\n void add(int val) {\r\n // write ur code here\r\n if(size == data.length){\r\n System.out.println(\"Queue overflow\");\r\n } else {\r\n int idx = (front + size) % data.length;\r\n data[idx] = val;\r\n size++;\r\n }\r\n }\r\n\r\n int remove() {\r\n // write ur code here\r\n if(size == 0){\r\n System.out.println(\"Queue underflow\");\r\n return -1;\r\n } else {\r\n int val = data[front];\r\n\r\n front = (front + 1) % data.length;\r\n size--;\r\n\r\n return val;\r\n }\r\n }\r\n\r\n int peek() {\r\n // write ur code here\r\n if(size == 0){\r\n System.out.println(\"Queue underflow\");\r\n return -1;\r\n } else {\r\n int val = data[front];\r\n return val;\r\n }\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 CustomQueue qu = new CustomQueue(n);\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 } else if(str.startsWith(\"display\")){\r\n qu.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class DynamicQueue:\n def __init__(self, cap):\n self.sizeQ = 0;\n self.front = 0;\n self.arrLen = cap;\n self.arr = [0] * self.arrLen;\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 n = int(input());\n q = DynamicQueue(n);\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":"5\r\nadd 10\r\ndisplay\r\nadd 20\r\ndisplay\r\nadd 30\r\ndisplay\r\nadd 40\r\ndisplay\r\nadd 50\r\ndisplay\r\nadd 60\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\ndisplay\r\npeek\r\nremove\r\nquit","sampleOutput":"10 \r\n10 20 \r\n10 20 30 \r\n10 20 30 40 \r\n10 20 30 40 50 \r\n10 20 30 40 50 60 \r\n10\r\n10\r\n20 30 40 50 60 \r\n20\r\n20\r\n30 40 50 60 \r\n30\r\n30\r\n40 50 60 \r\n40\r\n40\r\n50 60 \r\n50\r\n50\r\n60 \r\n60\r\n60","questionVideo":"https://www.youtube.com/embed/gluUcS0Rue8","hints":[],"associated":[{"id":"2422da0f-e956-4dbc-9294-5973258ac978","name":"The space complexity for insertion of an element in a dynamic queue (Q-Dynamic Queue)","slug":"the-space-complexity-for-insertion-of-an-element-in-a-dynamic-queue-q-dynamic-queue","type":4},{"id":"8bc35a8d-0f09-4cd8-801d-1ca1e13c50bb","name":"Which of the following principle does Queue use?(Q- Dynamic Queue)","slug":"which-of-the-following-principle-does-queue-use-q-dynamic-queue","type":4},{"id":"a72f72b5-ee70-4ce4-84b1-ab48a57fadc6","name":"The time complexity for insertion of an element in a dynamic queue at the end by increasing the size will be(Q- Dynamic Queue)","slug":"the-time-complexity-for-insertion-of-an-element-in-a-dynamic-queue-at-the-end-by-increasing-the-size-will-be-q-dynamic-queue","type":4},{"id":"e95ae1c3-3ae0-4dc6-9835-f2121f8225cf","name":"The time complexity for insertion of an element in a dynamic queue at the end without increasing the size will be (Q- Dynamic Queue)","slug":"the-time-complexity-for-insertion-of-an-element-in-a-dynamic-queue-at-the-end-without-increasing-the-size-will-be-q-dynamic-queue","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":"5299a556-123f-43d6-8f74-c6426e9903d4","name":"Dynamic Queue","slug":"dynamic-queue","type":1}],"next":{"id":"b5f32ead-2068-4128-8215-b9ac64ff44a4","name":"Dynamic Queue","type":3,"slug":"dynamic-queue"},"prev":{"id":"fa76c7e5-a165-4e8d-b34a-1c7b1ad316b4","name":"Normal Queue","type":3,"slug":"normal-queue"}}}
plane

Editor


Loading...

Dynamic Queue

easy

1. You are required to complete the code of our CustomQueue 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. 2.5. display -> Prints the elements of queue in FIFO manner (space-separated) ending with a line-break. 3. Input and Output is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

5 add 10 display add 20 display add 30 display add 40 display add 50 display add 60 display peek remove display peek remove display peek remove display peek remove display peek remove display peek remove quit

Sample Output

10 10 20 10 20 30 10 20 30 40 10 20 30 40 50 10 20 30 40 50 60 10 10 20 30 40 50 60 20 20 30 40 50 60 30 30 40 50 60 40 40 50 60 50 50 60 60 60

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode