Easy to Understand - O(1) Java Clean code using 2 Deque
Used two deques to implement this problem.\n\nFirst Queue :- When you call pushFront(), it will be adding to the start of the first queue. So now popFront() is 
Used two deques to implement this problem.\n\n**First Queue** :- When you call **pushFront()**, it will be adding to the start of the first queue. So now **popFront()** is easy just need to return first element from the first queue if exists otherwise return first element from the second queue.\n\n**Second Queue**:- When you call **pushBack()**, it will be adding to the end of the second queue. So now **popBack()** is easy just need to return last element from the second queue if exists otherwise return last element from the first queue.\n\nNow we are left with **pushMiddle()** and **popMiddle()**, on every call to these methods try to maintain equal number of elements on both the deques by shifting elements from first deque to second deque in case middle index falls in the first deque and same applies to the other case as well and then finally insert the value.\n\n```\nclass FrontMiddleBackQueue {\n\n    Deque<Integer> dq1, dq2;\n    public FrontMiddleBackQueue() {        \n        dq1 = new ArrayDeque<Integer>();\n        dq2 = new ArrayDeque<Integer>();\n    }\n    \n    public void pushFront(int val) {\n        dq1.addFirst(val);\n    }    \n    \n    public void pushBack(int val) {\n        dq2.addLast(val);\n    }   \n    \n    public void pushMiddle(int val) {\n        while(dq1.size() + 1 < dq2.size())\n            dq1.addLast(dq2.removeFirst());       \n        while(dq1.size() > dq2.size())\n            dq2.addFirst(dq1.removeLast());        \n        dq1.addLast(val);        \n    }     \n    \n    public int popFront() {\n        if(!dq1.isEmpty())\n            return dq1.removeFirst();\n        if(!dq2.isEmpty())\n            return dq2.removeFirst();\n        return -1;    \n    }  \n    \n    public int popMiddle() {\n        if(dq1.isEmpty() && dq2.isEmpty())\n            return -1;        \n        while(dq1.size() < dq2.size())\n            dq1.addLast(dq2.removeFirst());     \n        while(dq1.size() > dq2.size() + 1)\n            dq2.addFirst(dq1.removeLast());\n        return !dq1.isEmpty() ? dq1.removeLast() : dq2.removeFirst();\n    }    \n    \n    public int popBack() {\n        if(!dq2.isEmpty())\n            return dq2.removeLast();\n        if(!dq1.isEmpty())\n            return dq1.removeLast();\n        return -1;            \n    }    \n}\n```
['Queue', 'Java']