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
•
•
•
•
•
•
Key Rules:
1. When there are two middle positions, choose the frontmost one
2. Return
Examples:
• Pushing 6 into middle of
• Popping middle from
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 elementKey Rules:
1. When there are two middle positions, choose the frontmost one
2. Return
-1 if trying to pop from an empty queueExamples:
• 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
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
✓ Linear Growth
Space Complexity
O(n)
Store n elements across two deques
⚡ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code