Queue Is Empty
Given an array representing a queue, determine whether the queue is empty. Return true if the queue has no elements, and false if it contains at least one element. This operation does not modify the queue.
For example, an empty queue [] returns true. A queue with a single element [5] returns false. A queue with multiple elements [1, 2, 3] also returns false. The result depends only on whether any elements exist, not on their values or quantity.
The isEmpty operation is one of the most frequently used queue operations. Before attempting to dequeue or peek at the front element, you should always check if the queue is empty to avoid errors. This pattern — checking emptiness before access — is a fundamental defensive programming technique used with all collection types including stacks, lists, and sets.
In array-based queue implementations, checking emptiness is as simple as checking whether the array length is zero. In linked-list-based queues, you check whether the front pointer is null. Both approaches are O(1) and do not modify the data structure. Some implementations maintain a separate size counter for O(1) emptiness checks even in linked-list-based designs.
Edge cases include an array that was just created (empty), an array that had all its elements dequeued (also empty), and an array with exactly one element (not empty). The operation is side-effect-free and returns a boolean value.
Example Input & Output
Zero is still an element
Empty again
Has elements
Has one element
Empty queue
Algorithm Flow
Solution Approach
Check whether the array length equals zero and return the boolean result.
The simplest implementation compares the array length to 0 and returns the boolean result. In different languages: arr.length === 0 (JavaScript), len(arr) == 0 (Python), arr.length == 0 (Java), empty($arr) (PHP), arr.is_empty() (Rust). Each of these evaluates to true when the collection is empty and false otherwise.
Time complexity is O(1) because array length is a stored property that does not require iteration. Space complexity is O(1) since no additional memory is allocated.
Best Answers
class Solution {
public boolean solution(int[] nums) {
return nums.length==0;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
