What is a deque (double-ended queue)?
a) A queue where elements can be added or removed from both ends
b) A stack where elements are added at the top
c) A priority queue where elements are sorted
d) A linked list where each element points to the next
Answer:
a) A queue where elements can be added or removed from both ends
Explanation:
A deque (double-ended queue) is a data structure that allows insertion and deletion of elements from both the front and the rear. This makes it more flexible than a regular queue, which only allows operations at one end.
Deques are used in various algorithms that require access to both ends of the sequence, such as in certain sliding window problems or in breadth-first search (BFS) with priority constraints.
Deques can be implemented using arrays or linked lists, and they support O(1) operations for insertion and deletion at both ends.
Reference:
DSA (Data Structures and Algorithms) MCQ Questions and Answers