Code Logo

Queue Is Empty

Published at25 Jul 2026
Basic Queue Easy 0 views
Like0

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

Example 1
Input
[0]
Output
false
Explanation

Zero is still an element

Example 2
Input
[]
Output
true
Explanation

Empty again

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

Has elements

Example 4
Input
[1]
Output
false
Explanation

Has one element

Example 5
Input
[]
Output
true
Explanation

Empty queue

Algorithm Flow

Recommendation Algorithm Flow for Queue Is Empty

Solution Approach

Check whether the array length equals zero and return the boolean result.

function solution(arr) {
  return arr.length === 0;
}

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

java
class Solution {
    public boolean solution(int[] nums) {
        return nums.length==0;
    }
}