{"id":"d45164f4-d4e9-41bb-ae20-2b4a885b3081","name":"Design Circular Deque","description":"Design your implementation of the circular double-ended queue (deque).\r\n\r\nYour implementation should support following operations:\r\n\r\n1. MyCircularDeque(): Initializes the deque object.\r\n2. insertFront(): Adds an item at the front of Deque.\r\n3. insertLast(): Adds an item at the rear of Deque.\r\n4. deleteFront(): Deletes the front item from the Deque and return it's value. If the deque is empty, return null.\r\n5. deleteLast(): Deletes the last item from Deque and return it's value. If the deque is empty, return null.\r\n6. getFront(): Gets the front item from the Deque. If the deque is empty, return null.\r\n7. getRear(): Gets the last item from Deque. If the deque is empty, return null.\r\n8. isEmpty(): Checks whether Deque is empty or not.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nclass MyCircularDeque{\npublic: \n /** Initialize your data structure here.*/\n \n MyCircularDeque(){\n }\n /**\n * Adds an item at the front of Deque.\n */\n void insertFront(int value) {\n }\n\n /**\n * Adds an item at the rear of Deque.\n */\n void insertLast(int value) {\n }\n\n /**\n * Deletes the front item from the Deque and return it's value. If the deque is empty, return null.\n */\n int deleteFront() {\n }\n\n /**\n * Deletes the last item from Deque and return it's value. If the deque is empty, return null.\n */\n int deleteLast() {\n }\n\n /** Gets the front item from the Deque. If the deque is empty, return null. */\n int getFront() {\n }\n\n /** Gets the last item from Deque. If the deque is empty, return null. */\n int getRear() {\n }\n\n /** Checks whether the circular deque is empty or not. */\n bool isEmpty() {\n }\n};\n\nint main(){\n MyCircularDeque obj;\n string s;\n while( cin >> s ){\n if (s==\"insertLast\") {\n int val;\n cin >> val;\n obj.insertLast(val);\n } else if (s==\"insertFront\") {\n int val;\n cin >> val;\n obj.insertFront(val);\n } else if (s==\"deleteFront\") {\n int val = obj.deleteFront();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n \n } else if (s==\"deleteLast\") {\n int val = obj.deleteLast();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n } else if (s==\"getFront\") {\n int val = obj.getFront();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n } else if (s==\"getRear\") {\n int val = obj.getRear();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n } else if (s==\"isEmpty\") {\n cout<<obj.isEmpty()<<endl;;\n }\n }\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MyCircularDeque<T> {\r\n\r\n /** Initialize your data structure here.*/\r\n public MyCircularDeque() {\r\n }\r\n\r\n /**\r\n * Adds an item at the front of Deque.\r\n */\r\n public void insertFront(T value) {\r\n }\r\n\r\n /**\r\n * Adds an item at the rear of Deque.\r\n */\r\n public void insertLast(T value) {\r\n }\r\n\r\n /**\r\n * Deletes the front item from the Deque and return it''s value. If the deque is empty, return null.\r\n */\r\n public T deleteFront() {\r\n }\r\n\r\n /**\r\n * Deletes the last item from Deque and return it''s value. If the deque is empty, return null.\r\n */\r\n public T deleteLast() {\r\n }\r\n\r\n /** Gets the front item from the Deque. If the deque is empty, return null. */\r\n public T getFront() {\r\n }\r\n\r\n /** Gets the last item from Deque. If the deque is empty, return null. */\r\n public T getRear() {\r\n }\r\n\r\n /** Checks whether the circular deque is empty or not. */\r\n public boolean isEmpty() {\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n MyCircularDeque<Integer> obj = new MyCircularDeque<>();\r\n\r\n while (read.ready()) {\r\n String inp[] = read.readLine().split(\" \");\r\n String s = inp[0];\r\n\r\n if (s.equals(\"insertLast\")) {\r\n obj.insertLast(Integer.parseInt(inp[1]));\r\n } else if (s.equals(\"insertFront\")) {\r\n obj.insertFront(Integer.parseInt(inp[1]));\r\n } else if (s.equals(\"deleteFront\")) {\r\n System.out.println(obj.deleteFront());\r\n } else if (s.equals(\"deleteLast\")) {\r\n System.out.println(obj.deleteLast());\r\n } else if (s.equals(\"getFront\")) {\r\n System.out.println(obj.getFront());\r\n } else if (s.equals(\"getRear\")) {\r\n System.out.println(obj.getRear());\r\n } else if (s.equals(\"isEmpty\")) {\r\n System.out.println(obj.isEmpty());\r\n }\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"insertLast 1\r\ninsertLast 2\r\ninsertFront 3\r\ninsertFront 4\r\ngetRear\r\ndeleteLast\r\ngetRear\r\ndeleteLast\r\ninsertFront 4\r\ngetFront \r\n","sampleOutput":"2\r\n2\r\n1\r\n1\r\n4\r\n","questionVideo":"","hints":[],"associated":[],"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":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"06574ec3-c921-4dd4-9597-c7db1ef7ffc0","name":"Design Circular Deque","slug":"design-circular-deque","type":1}],"next":{"id":"c00679cc-2847-4f45-bbd0-53a13e56cdda","name":"Design Circular Deque Medium MCQ","type":0,"slug":"design-circular-deque-medium-mcq"},"prev":{"id":"facec938-0b3d-4ab2-919d-b8b6a3623e01","name":"Design A Stack With Increment Operation","type":3,"slug":"design-a-stack-with-increment-operation"}}}

Design Circular Deque

Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: 1. MyCircularDeque(): Initializes the deque object. 2. insertFront(): Adds an item at the front of Deque. 3. insertLast(): Adds an item at the rear of Deque. 4. deleteFront(): Deletes the front item from the Deque and return it's value. If the deque is empty, return null. 5. deleteLast(): Deletes the last item from Deque and return it's value. If the deque is empty, return null. 6. getFront(): Gets the front item from the Deque. If the deque is empty, return null. 7. getRear(): Gets the last item from Deque. If the deque is empty, return null. 8. isEmpty(): Checks whether Deque is empty or not.

{"id":"d45164f4-d4e9-41bb-ae20-2b4a885b3081","name":"Design Circular Deque","description":"Design your implementation of the circular double-ended queue (deque).\r\n\r\nYour implementation should support following operations:\r\n\r\n1. MyCircularDeque(): Initializes the deque object.\r\n2. insertFront(): Adds an item at the front of Deque.\r\n3. insertLast(): Adds an item at the rear of Deque.\r\n4. deleteFront(): Deletes the front item from the Deque and return it's value. If the deque is empty, return null.\r\n5. deleteLast(): Deletes the last item from Deque and return it's value. If the deque is empty, return null.\r\n6. getFront(): Gets the front item from the Deque. If the deque is empty, return null.\r\n7. getRear(): Gets the last item from Deque. If the deque is empty, return null.\r\n8. isEmpty(): Checks whether Deque is empty or not.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nclass MyCircularDeque{\npublic: \n /** Initialize your data structure here.*/\n \n MyCircularDeque(){\n }\n /**\n * Adds an item at the front of Deque.\n */\n void insertFront(int value) {\n }\n\n /**\n * Adds an item at the rear of Deque.\n */\n void insertLast(int value) {\n }\n\n /**\n * Deletes the front item from the Deque and return it's value. If the deque is empty, return null.\n */\n int deleteFront() {\n }\n\n /**\n * Deletes the last item from Deque and return it's value. If the deque is empty, return null.\n */\n int deleteLast() {\n }\n\n /** Gets the front item from the Deque. If the deque is empty, return null. */\n int getFront() {\n }\n\n /** Gets the last item from Deque. If the deque is empty, return null. */\n int getRear() {\n }\n\n /** Checks whether the circular deque is empty or not. */\n bool isEmpty() {\n }\n};\n\nint main(){\n MyCircularDeque obj;\n string s;\n while( cin >> s ){\n if (s==\"insertLast\") {\n int val;\n cin >> val;\n obj.insertLast(val);\n } else if (s==\"insertFront\") {\n int val;\n cin >> val;\n obj.insertFront(val);\n } else if (s==\"deleteFront\") {\n int val = obj.deleteFront();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n \n } else if (s==\"deleteLast\") {\n int val = obj.deleteLast();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n } else if (s==\"getFront\") {\n int val = obj.getFront();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n } else if (s==\"getRear\") {\n int val = obj.getRear();\n if(val==-1)\n cout<<\"null\"<<endl;\n else\n cout<<val<<endl;\n } else if (s==\"isEmpty\") {\n cout<<obj.isEmpty()<<endl;;\n }\n }\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MyCircularDeque<T> {\r\n\r\n /** Initialize your data structure here.*/\r\n public MyCircularDeque() {\r\n }\r\n\r\n /**\r\n * Adds an item at the front of Deque.\r\n */\r\n public void insertFront(T value) {\r\n }\r\n\r\n /**\r\n * Adds an item at the rear of Deque.\r\n */\r\n public void insertLast(T value) {\r\n }\r\n\r\n /**\r\n * Deletes the front item from the Deque and return it''s value. If the deque is empty, return null.\r\n */\r\n public T deleteFront() {\r\n }\r\n\r\n /**\r\n * Deletes the last item from Deque and return it''s value. If the deque is empty, return null.\r\n */\r\n public T deleteLast() {\r\n }\r\n\r\n /** Gets the front item from the Deque. If the deque is empty, return null. */\r\n public T getFront() {\r\n }\r\n\r\n /** Gets the last item from Deque. If the deque is empty, return null. */\r\n public T getRear() {\r\n }\r\n\r\n /** Checks whether the circular deque is empty or not. */\r\n public boolean isEmpty() {\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n MyCircularDeque<Integer> obj = new MyCircularDeque<>();\r\n\r\n while (read.ready()) {\r\n String inp[] = read.readLine().split(\" \");\r\n String s = inp[0];\r\n\r\n if (s.equals(\"insertLast\")) {\r\n obj.insertLast(Integer.parseInt(inp[1]));\r\n } else if (s.equals(\"insertFront\")) {\r\n obj.insertFront(Integer.parseInt(inp[1]));\r\n } else if (s.equals(\"deleteFront\")) {\r\n System.out.println(obj.deleteFront());\r\n } else if (s.equals(\"deleteLast\")) {\r\n System.out.println(obj.deleteLast());\r\n } else if (s.equals(\"getFront\")) {\r\n System.out.println(obj.getFront());\r\n } else if (s.equals(\"getRear\")) {\r\n System.out.println(obj.getRear());\r\n } else if (s.equals(\"isEmpty\")) {\r\n System.out.println(obj.isEmpty());\r\n }\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"insertLast 1\r\ninsertLast 2\r\ninsertFront 3\r\ninsertFront 4\r\ngetRear\r\ndeleteLast\r\ngetRear\r\ndeleteLast\r\ninsertFront 4\r\ngetFront \r\n","sampleOutput":"2\r\n2\r\n1\r\n1\r\n4\r\n","questionVideo":"","hints":[],"associated":[],"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":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"06574ec3-c921-4dd4-9597-c7db1ef7ffc0","name":"Design Circular Deque","slug":"design-circular-deque","type":1}],"next":{"id":"c00679cc-2847-4f45-bbd0-53a13e56cdda","name":"Design Circular Deque Medium MCQ","type":0,"slug":"design-circular-deque-medium-mcq"},"prev":{"id":"facec938-0b3d-4ab2-919d-b8b6a3623e01","name":"Design A Stack With Increment Operation","type":3,"slug":"design-a-stack-with-increment-operation"}}}
plane

Editor


Loading...

Design Circular Deque

medium

Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: 1. MyCircularDeque(): Initializes the deque object. 2. insertFront(): Adds an item at the front of Deque. 3. insertLast(): Adds an item at the rear of Deque. 4. deleteFront(): Deletes the front item from the Deque and return it's value. If the deque is empty, return null. 5. deleteLast(): Deletes the last item from Deque and return it's value. If the deque is empty, return null. 6. getFront(): Gets the front item from the Deque. If the deque is empty, return null. 7. getRear(): Gets the last item from Deque. If the deque is empty, return null. 8. isEmpty(): Checks whether Deque is empty or not.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

insertLast 1 insertLast 2 insertFront 3 insertFront 4 getRear deleteLast getRear deleteLast insertFront 4 getFront

Sample Output

2 2 1 1 4

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode