Given an array representing a deque (double-ended queue), return the element at the front of the deque without removing it. The front is the element that has been in the deque the longest and is next to be dequeued from the front.
For example, the front of [10, 20, 30] is 10. The front of [5] is 5. If the deque is empty, return 0. The operation does not modify the deque — it only reads the front element.
Peeking at the front of a deque is essential for inspecting the next element to be processed. In breadth-first search algorithms, dequeuing from the front processes nodes in order of discovery. In sliding window maximum problems, the front of the deque holds the maximum of the current window. In task scheduling systems, the front of the work queue is the next task to be executed by a worker thread.
In array-based deque implementations, the front is at index 0. In linked-list-based deques, the front is the head node's value. More advanced circular deque implementations track the front with a movable pointer that wraps around the underlying array, but for simple array deques, index 0 always represents the front.
Edge cases include an empty deque (return 0 as a sentinel value), a single-element deque (return that element), and a deque with many elements (return the first element). The deque must remain completely unchanged after the peek operation.
Example Input & Output
Single
First is 5
First is 1
Empty
First is 0
Algorithm Flow

Solution Approach
Return the first element of the array if it exists, otherwise return 0.
Check whether the array is non-empty. If it contains at least one element, return arr[0] which represents the front of the deque. If the array is empty, return 0 to indicate that no element is available. This conditional check prevents index-out-of-bounds errors.
Time complexity is O(1) since array index access is constant time. Space complexity is O(1).
Best Answers
class Solution {
public int solution(int[] nums) {
return nums.length>0?nums[0]:-1;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
