Easy solution using 2 queues. Approach and Explanation given below
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nFirst take 2 queues.For pusfFront, using built-in functions of queues, ap
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nFirst take 2 queues.For pusfFront, using built-in functions of queues, appendleft it into the front of the first queue.\n\nFor pushMiddle, first check if the length of the first queue is even, if it is even, with the range, floor of the length of first queue divided by two, pop the elements present in the first queue and append them in the second queue.By doing this, we can now easily append an element in the last of the first queue which is actually the middle of it because we will append all the elements back in the first queue from the second queue.If the length of the first queue is odd, do the same thing but now with the range, floor of the lenght of first queue divided by two minus 1. This is because in the question it is mentioned that if there are two options for pushing the middle element, push it in the leftmost middle.\n\nFor pushBack, simply apppend it in the first queue\n\nFor popFront, check if the length of the first queue is not equal to 0 and popleft the element from the first queue and return it.Otherwise return -1, if it is empty\n\nFor popMiddle, first store the length of the first queue ina variable which help shorten the code. Then, if a is equal to 0, meaning that the queue is empty, return -1. If a is even, store the required answer in a variable and with range, floor of a divided by 2, append the elements of the first queue in the second queue. After doing that, pop the element we have to return since it is of no longer use to us and we have already stored it in a variable.Then append all the elements back in the first stack. If a is odd, we will handle it with same way as we did it for a being even, but we have to be carefull while storing the required answer, since it should be the leftmost middle element.\n\nFor popBack, just check if the queue is not empty and return \nthe roghtmost element by popping it out, otherwise return -1.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass FrontMiddleBackQueue:\n\n    def __init__(self):\n        self.queue = deque()\n        self.queue2 = deque()\n\n\n    def pushFront(self, val: int) -> None:\n        self.queue.appendleft(val)\n\n\n    def pushMiddle(self, val: int) -> None:\n        if len(self.queue)%2==0:\n            for i in range(len(self.queue)//2):\n                self.queue2.append(self.queue.pop())\n            self.queue.append(val)\n            for i in range(len(self.queue2)):\n                self.queue.append(self.queue2.pop())\n        else:\n            for i in range((len(self.queue)//2)+1):\n                self.queue2.append(self.queue.pop())\n            self.queue.append(val)\n            for i in range(len(self.queue2)):\n                self.queue.append(self.queue2.pop())\n\n\n    def pushBack(self, val: int) -> None:\n        self.queue.append(val)\n\n\n\n    def popFront(self) -> int:\n        if len(self.queue)!=0:\n            return self.queue.popleft()\n        else:\n            return -1\n        \n\n    def popMiddle(self) -> int:\n        a = len(self.queue)\n        if a==0:\n            return -1\n        if a%2==0:\n            ans = self.queue[(a//2)-1]\n            for i in range(a//2):\n                self.queue2.append(self.queue.pop())\n            self.queue.pop()\n            for i in range(len(self.queue2)):\n                self.queue.append(self.queue2.pop())\n            return ans\n\n        else:\n            ans = self.queue[(a//2)]\n            for i in range(a//2):\n                self.queue2.append(self.queue.pop())\n            self.queue.pop()\n            for i in range(len(self.queue2)):\n                self.queue.append(self.queue2.pop())\n            return ans\n\n\n    def popBack(self) -> int:\n        if len(self.queue) != 0:\n            return self.queue.pop()\n        else:\n            return -1\n        \n\n\n# Your FrontMiddleBackQueue object will be instantiated and called as such:\n# obj = FrontMiddleBackQueue()\n# obj.pushFront(val)\n# obj.pushMiddle(val)\n# obj.pushBack(val)\n# param_4 = obj.popFront()\n# param_5 = obj.popMiddle()\n# param_6 = obj.popBack()\n```
['Python3']