bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1188. Design Bounded Blocking Queue

1188. Design Bounded Blocking Queue

Leetcode Solutions

Semaphore and Lock-based Solution

  1. Initialize the queue with the given capacity.
  2. Use a semaphore emptySlots initialized to the capacity to represent the number of empty slots available for enqueue operations.
  3. Use another semaphore filledSlots initialized to 0 to represent the number of elements available for dequeue operations.
  4. Use a lock to ensure mutual exclusion when accessing the queue.
  5. For enqueue, acquire emptySlots, acquire the lock, add the element to the queue, release the lock, and then release filledSlots.
  6. For dequeue, acquire filledSlots, acquire the lock, remove the element from the queue, release the lock, and then release emptySlots.
  7. For size, simply return the current size of the queue.
UML Thumbnail

Lock and Condition Variables Solution

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...