Code Logo

Queue Clear

Published at25 Jul 2026
Basic Queue Easy 2 views
Like0

Given an array representing a queue, clear all elements from it and return an empty array. This operation removes every element from the queue, leaving it completely empty.

For example, clearing a queue containing [1, 2, 3] produces []. An empty queue cleared also produces []. The operation produces the same result regardless of what elements are in the queue or how many there are — the output is always an empty array.

In a real queue data structure, clearing means removing all nodes and resetting the front and rear pointers. In array-based simulation, it means returning a new empty array. This operation is commonly used when you want to reset a queue for reuse, such as between processing batches of tasks or after draining a work queue.

The clear operation runs in O(1) time for arrays (just return a new empty array) or O(n) time for linked-list-based queues where each node must be individually deallocated. Space complexity is O(1) since the result is always a fixed-size empty container regardless of the original queue size.

This is one of the most straightforward queue operations and serves as a building block for more complex queue management scenarios, including batch processing, task draining, state reset, and queue pooling.

Example Input & Output

Example 1
Input
[5]
Output
[]
Explanation

Single element

Example 2
Input
[10,20]
Output
[]
Explanation

Two elements

Example 3
Input
[1,2,3]
Output
[]
Explanation

Clear all elements

Example 4
Input
[1,2,3,4,5]
Output
[]
Explanation

Five elements

Example 5
Input
[]
Output
[]
Explanation

Empty stays empty

Algorithm Flow

Recommendation Algorithm Flow for Queue Clear
Recommendation Algorithm Flow for Queue Clear

Solution Approach

Return an empty array to represent the cleared queue.

function solution(arr) {
  return [];
}

The simplest possible implementation returns a new empty array literal []. In languages with mutable data structures, you could also clear the input in place — for example, setting arr.length = 0 in JavaScript, calling arr.clear() in Python, or queue.clear() in Java. However, returning a new empty array is cleaner because it avoids mutating the input and makes the function's behavior predictable.

Time complexity is O(1) since no iteration is needed. Space complexity is O(1) as the empty array occupies constant memory.

Best Answers

java
class Solution {
    public int[] solution(int[] nums) {
        return new int[]{};
    }
}