Design Front Middle Back Queue - Problem
Design a Flexible Multi-Position Queue

You need to design a special queue data structure that supports push and pop operations at three different positions: front, middle, and back.

Your FrontMiddleBackQueue class should implement:
pushFront(val) - Add element to the front
pushMiddle(val) - Add element to the middle position
pushBack(val) - Add element to the back
popFront() - Remove and return front element
popMiddle() - Remove and return middle element
popBack() - Remove and return back element

Key Rules:
1. When there are two middle positions, choose the frontmost one
2. Return -1 if trying to pop from an empty queue

Examples:
• Pushing 6 into middle of [1,2,3,4,5][1,2,6,3,4,5]
• Popping middle from [1,2,3,4,5,6] → returns 3, queue becomes [1,2,4,5,6]

Input & Output

example_1.py — Basic Operations
$ Input: ["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "popFront", "popMiddle", "popBack"] [[], [1], [2], [3], [], [], []]
Output: [null, null, null, null, 1, 3, 2]
💡 Note: Initialize empty queue, push 1 to front: [1], push 2 to back: [1,2], push 3 to middle: [1,3,2], pop front returns 1: [3,2], pop middle returns 3: [2], pop back returns 2: []
example_2.py — Middle Position Rules
$ Input: ["FrontMiddleBackQueue", "pushBack", "pushBack", "pushBack", "pushBack", "popMiddle", "popMiddle"] [[], [1], [2], [3], [4], [], []]
Output: [null, null, null, null, null, 2, 3]
💡 Note: Build queue [1,2,3,4]. For even length 4, middle positions are indices 1,2. Choose frontmost (index 1): pop 2, queue becomes [1,3,4]. Now length 3, middle is index 1: pop 3.
example_3.py — Empty Queue Edge Case
$ Input: ["FrontMiddleBackQueue", "popFront", "popMiddle", "popBack", "pushMiddle", "popMiddle"] [[], [], [], [], [1], []]
Output: [null, -1, -1, -1, null, 1]
💡 Note: All pop operations on empty queue return -1. After pushing 1 to middle of empty queue: [1], popping middle returns 1.

Visualization

Tap to expand
Concert Hall Queue ManagementLeft Section (Deque)123MIDDLERight Section (Deque)45Operations PerformanceFront OperationsO(1) - Left DequeMiddle OperationsO(1) - BoundaryBack OperationsO(1) - Right Deque🎯 Balance Invariant: |left| ≤ |right| + 1Middle Boundary
Understanding the Visualization
1
Initial Setup
Set up two balanced sections (left and right halves) with a manager for each
2
Balance Rule
Left section can have at most 1 more person than right section
3
Middle Access
Middle seat is always at the boundary - end of left section when balanced
4
Efficient Operations
Most seating/departures are O(1) with occasional rebalancing between sections
Key Takeaway
🎯 Key Insight: By splitting the queue into two balanced deques with the middle always at their boundary, we achieve O(1) performance for all operations while maintaining the correct middle position semantics.

Time & Space Complexity

Time Complexity
⏱️
O(1)

Most operations are O(1), occasional O(1) rebalancing

n
2n
Linear Growth
Space Complexity
O(n)

Store n elements across two deques

n
2n
Linearithmic Space

Constraints

  • 1 ≤ val ≤ 109
  • At most 1000 calls will be made to pushFront, pushMiddle, pushBack, popFront, popMiddle, and popBack
  • All values are positive integers
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen