All problems

Design Circular Queue

Medium · design

A ticket counter has a waiting line with room for at most capacity people. It starts empty. People join at the back and are served from the front, in the order they joined. You are given a list of operations to carry out in order: operations[i] is one of the six words below, and values[i] is the number of the person joining when the operation is "enqueue" (it is 0 and should be ignored for every other operation).

Return a list with one number for every operation, in order:

Examples

Example 1

Input: capacity = 3, operations = ["enqueue", "enqueue", "enqueue", "enqueue", "rear", "isFull", "dequeue", "enqueue", "front", "rear"], values = [1, 2, 3, 4, 0, 0, 0, 4, 0, 0]

Output: [1, 1, 1, 0, 3, 1, 1, 1, 2, 4]

The fourth enqueue fails because the line already holds 3 people. After serving person 1, person 4 can join, so the front is 2 and the back is 4.

Example 2

Input: capacity = 2, operations = ["isEmpty", "front", "enqueue", "dequeue", "dequeue", "isEmpty"], values = [0, 0, 9, 0, 0, 0]

Output: [1, -1, 1, 1, 0, 1]

The line is empty, so front gives -1. The second dequeue fails because person 9 has already left.

Constraints