Code Logo

Deque Peek Front

Published at25 Jul 2026
Deque Easy 0 views
Like0

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

Example 1
Input
[10]
Output
10
Explanation

Single

Example 2
Input
[5,6,7]
Output
5
Explanation

First is 5

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

First is 1

Example 4
Input
[]
Output
-1
Explanation

Empty

Example 5
Input
[0,1]
Output
0
Explanation

First is 0

Algorithm Flow

Recommendation Algorithm Flow for Deque Peek Front
Recommendation Algorithm Flow for Deque Peek Front

Solution Approach

Return the first element of the array if it exists, otherwise return 0.

function solution(arr) {
  return arr.length > 0 ? arr[0] : 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

java
class Solution {
    public int solution(int[] nums) {
        return nums.length>0?nums[0]:-1;
    }
}