[Python3] 2 deques
\n\nclass FrontMiddleBackQueue:\n\n    def __init__(self):\n        self.d0 = deque()\n        self.d1 = deque()\n\n    def pushFront(self, val: int) -> None:\n
\n```\nclass FrontMiddleBackQueue:\n\n    def __init__(self):\n        self.d0 = deque()\n        self.d1 = deque()\n\n    def pushFront(self, val: int) -> None:\n        self.d0.appendleft(val)\n        if len(self.d0) > len(self.d1): \n            self.d1.appendleft(self.d0.pop())\n\n    def pushMiddle(self, val: int) -> None:\n        if len(self.d0) == len(self.d1): self.d1.appendleft(val)\n        else: self.d0.append(val)\n\n    def pushBack(self, val: int) -> None:\n        self.d1.append(val)\n        if len(self.d1) > 1 + len(self.d0): \n            self.d0.append(self.d1.popleft())\n\n    def popFront(self) -> int:\n        if self.d0: \n            ans = self.d0.popleft()\n            if len(self.d0) + 1 < len(self.d1): \n                self.d0.append(self.d1.popleft())\n            return ans \n        elif self.d1: return self.d1.popleft()\n        else: return -1\n\n    def popMiddle(self) -> int:\n        if len(self.d0) == len(self.d1) > 0: return self.d0.pop()\n        elif self.d1: return self.d1.popleft()\n        return -1\n\n    def popBack(self) -> int:\n        if self.d1:\n            ans = self.d1.pop()\n            if len(self.d0) > len(self.d1): \n                self.d1.appendleft(self.d0.pop())\n            return ans \n        elif self.d0: return self.d0.pop()\n        else: return -1\n```\n\nAdding a more compact implementation\n```\nclass FrontMiddleBackQueue:\n\n    def __init__(self):\n        self.d0 = deque()\n        self.d1 = deque()\n        \n    def _balance(self):\n        if len(self.d0) > len(self.d1): self.d1.appendleft(self.d0.pop())\n        elif len(self.d0) + 1 < len(self.d1): self.d0.append(self.d1.popleft())\n\n    def pushFront(self, val: int) -> None:\n        self.d0.appendleft(val)\n        self._balance()\n\n    def pushMiddle(self, val: int) -> None:\n        self.d0.append(val)\n        self._balance()\n\n    def pushBack(self, val: int) -> None:\n        self.d1.append(val)\n        self._balance()\n        \n    def popFront(self) -> int:\n        if self.d0: \n            ans = self.d0.popleft()\n            self._balance()\n            return ans \n        elif self.d1: return self.d1.pop()\n        else: return -1\n\n    def popMiddle(self) -> int:\n        if self.d0 and len(self.d0) == len(self.d1): return self.d0.pop()\n        elif self.d1: return self.d1.popleft()\n        return -1\n\n    def popBack(self) -> int:\n        ans = (self.d1 or [-1]).pop()\n        self._balance()\n        return ans \n```
['Python3']