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
Single element
Two elements
Clear all elements
Five elements
Empty stays empty
Algorithm Flow

Solution Approach
Return an empty array to represent the cleared queue.
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
class Solution {
public int[] solution(int[] nums) {
return new int[]{};
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
