Dinner Plate Stacks - Problem

Imagine you're managing a cafeteria where plates are organized in stacks. You have an infinite number of stacks arranged in a row, numbered from 0 (leftmost) onwards. Each stack has the same maximum capacity.

Your task is to implement a DinnerPlates class that efficiently manages these stacks:

  • Push operation: Always add new plates to the leftmost stack that has space
  • Pop operation: Always remove plates from the rightmost non-empty stack
  • Pop at specific stack: Remove a plate from any specific stack by its index

The challenge is to handle these operations efficiently while maintaining the correct ordering and dealing with gaps that form when plates are removed from middle stacks.

Example: With capacity=2, after pushing [1,2,3,4,5] you get: Stack0=[1,2], Stack1=[3,4], Stack2=[5]. If you pop from Stack1, it becomes Stack0=[1,2], Stack1=[3], Stack2=[5], and the next push should fill Stack1 again, not create Stack3.

Input & Output

basic_operations.py โ€” Python
$ Input: DinnerPlates(2) push(1), push(2), push(3), push(4), push(5) pop(), pop(), popAtStack(0), push(20), push(21)
โ€บ Output: [null, null, null, null, null, null, 5, 4, 2, null, null] Final state: Stack0=[1], Stack1=[20,21], Stack2=[]
๐Ÿ’ก Note: With capacity 2: push creates Stack0=[1,2], Stack1=[3,4], Stack2=[5]. Pop removes 5 and 4. PopAtStack(0) removes 2. Push(20) goes to Stack1, Push(21) fills Stack1.
gap_filling.py โ€” Python
$ Input: DinnerPlates(2) push(1), push(2), popAtStack(1), push(2) popAtStack(1), push(99)
โ€บ Output: [null, null, null, -1, null, -1, null] Final state: Stack0=[1,99], Stack1=[]
๐Ÿ’ก Note: Push creates Stack0=[1,2]. PopAtStack(1) on empty returns -1. Push(2) creates Stack1=[2]. PopAtStack(1) removes 2. Push(99) fills Stack0 gap.
single_capacity.py โ€” Python
$ Input: DinnerPlates(1) push(1), push(2), push(3), pop(), pop(), pop()
โ€บ Output: [null, null, null, null, 3, 2, 1] Final state: All stacks empty
๐Ÿ’ก Note: With capacity 1, each push creates new stack: Stack0=[1], Stack1=[2], Stack2=[3]. Pop operations remove from rightmost: 3, then 2, then 1.

Constraints

  • 1 โ‰ค capacity โ‰ค 2 ร— 104
  • 1 โ‰ค val โ‰ค 2 ร— 104
  • 0 โ‰ค index โ‰ค 105
  • At most 2 ร— 105 calls will be made to push, pop, and popAtStack
  • Time limit: All operations should be efficient for large inputs

Visualization

Tap to expand
๐Ÿฝ๏ธ Smart Cafeteria Plate ManagementSetupPushPopGapsStack 021Stack 13availableStack 254Min-Heap136O(log n) accessRightmost: 2O(1) popPerformanceโœ“ Push: O(log n)โœ“ Pop: O(1)โœ“ PopAt: O(1)vs O(n) naive๐Ÿ”‘ Key Insight: Combine min-heap for leftmost access with rightmost pointer for optimal performance
Understanding the Visualization
1
Setup Phase
Initialize empty stack array, min-heap for tracking available positions, and rightmost pointer for quick access
2
Smart Push
Use min-heap to instantly find leftmost available stack, avoiding linear scan through potentially thousands of stacks
3
Efficient Pop
Rightmost pointer enables instant access to the last non-empty stack, no searching required
4
Gap Management
When middle stacks become available, they're immediately added to heap for future use, ensuring optimal space utilization
Key Takeaway
๐ŸŽฏ Key Insight: The optimal solution elegantly combines two data structures: a min-heap provides O(log n) access to the leftmost available stack, while a rightmost pointer enables O(1) pop operations. This hybrid approach transforms a potentially O(n) problem into an efficient logarithmic solution.
Asked in
Amazon 45 Google 32 Meta 28 Microsoft 22
28.5K Views
Medium Frequency
~25 min Avg. Time
967 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