2 deques + rebalancer
I keep the queues in the state [left + 1, right] all the time in order to simplify pop ops and other checks.\n\n\nclass FrontMiddleBackQueue:\n\n    def __init_
I keep the queues in the state `[left + 1, right]` all the time in order to simplify `pop ops` and other checks.\n\n```\nclass FrontMiddleBackQueue:\n\n    def __init__(self):\n        self.left = deque()\n        self.right = deque()\n\n    def pushFront(self, val: int) -> None:\n        self.left.appendleft(val)\n        self.rebalance()\n\n    def pushMiddle(self, val: int) -> None: # push aax aaa pop aax aaa\n        if len(self.left) > len(self.right):\n            self.right.appendleft(self.left.pop())\n        self.left.append(val)\n        self.rebalance()\n\n    def pushBack(self, val: int) -> None:\n        self.right.append(val)\n        self.rebalance()\n\n    def popFront(self) -> int:\n        res = -1\n        if self.left:\n            res = self.left.popleft()\n        self.rebalance()\n        return res\n\n    def popMiddle(self) -> int:\n        res = -1\n        if self.left:\n            res = self.left.pop()\n        self.rebalance()\n        return res\n\n    def popBack(self) -> int:\n        res = -1\n        if self.right:\n            res = self.right.pop()\n        elif self.left:\n            res = self.left.pop()\n        self.rebalance()\n        return res\n\n    def rebalance(self):\n        if len(self.left) < len(self.right):\n            self.left.append(self.right.popleft())\n        elif len(self.left) > len(self.right) + 1:\n            self.right.appendleft(self.left.pop())\n```
['Python', 'Python3']