FastPrepBounded Circular Queue
Problems/Bounded Circular Queue

Bounded Circular Queue

Datadog logoDatadog● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for queue operations using a circular buffer backed by a fixed-size array. Operation encoding, full and empty behavior, examples, and bounds are specified for practice. The judged core task matches the report at about 75%.

Implement a FIFO queue whose storage is one fixed-size array of length capacity. The queue starts empty. Process operations in order and return one integer result for each operation.

  • [0, x]: enqueue x. Return 1 if it is inserted. If the queue is full, return 0 and leave the queue unchanged.
  • [1, 0]: dequeue the oldest value and return it. Return -1 when empty.
  • [2, 0]: return the front value without removing it. Return -1 when empty.
  • [3, 0]: return the current number of stored values.

Stored values are nonnegative, so -1 is reserved for an empty dequeue or front operation. Do not overwrite a value when full or resize the queue storage. Reuse freed array positions. Aim for constant time per operation after allocating storage. The returned results may use a separate array.

Function

runCircularQueue(capacity: int, operations: int[][]) → int[]

Examples

Example 1

capacity = 2operations = [[0,7],[0,9],[0,11],[2,0],[1,0],[0,11],[1,0],[1,0],[1,0]]return = [1,1,0,7,7,1,9,11,-1]

The first two enqueues succeed, so the queue is [7,9]. Enqueuing 11 fails because the queue is full. Front returns 7 without removing it. After removing 7, enqueuing 11 succeeds. The remaining dequeues return 9, 11, and then -1 for an empty queue.

Example 2

capacity = 1operations = [[2,0],[3,0],[0,0],[3,0],[0,5],[1,0],[2,0]]return = [-1,0,1,1,0,0,-1]

An empty queue has front -1 and size 0. Enqueuing the value 0 succeeds and makes the size 1. The next enqueue fails. Dequeue returns the stored value 0; the queue is then empty again.

Example 3

capacity = 3operations = []return = []

No operations are requested, so the returned array is empty.

Constraints

  • 1 <= capacity <= 1000
  • 0 <= operations.length <= 5000
  • Every operation has exactly two integers.
  • Every operation entry is between 0 and 1000000, inclusive.
  • Each row is [code, argument], where code is 0, 1, 2, or 3. For codes 1, 2, and 3, the argument is 0.

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public int[] runCircularQueue(int capacity, int[][] operations) {
    // Write your code here.
}
Checking account…