What is a deque (double-ended queue)?
Answer
A deque (double-ended queue, pronounced "deck") is a linear data structure that supports insertion and deletion from both ends in O(1). It combines the functionality of both a stack and a queue. Operations: addFront/pushFront: O(1); addBack/pushBack: O(1); removeFront/popFront: O(1); removeBack/popBack: O(1); peekFront/peekBack: O(1). Implementation: typically as a doubly linked list or a circular buffer (dynamic array with front/back pointers). Language implementations: Java: ArrayDeque (faster than Stack/LinkedList for stack/queue operations); Python: collections.deque; C++: std::deque. Applications: (1) Sliding window maximum — maintain a monotonic deque of indices (max element always at front); (2) Implementing stack + queue using one structure; (3) Palindrome checking — compare front and back; (4) Undo/redo in text editors; (5) Scheduling algorithms; (6) Deque-based BFS with weights (0-1 BFS). Java ArrayDeque is generally preferred over Stack and LinkedList for LIFO/FIFO operations due to better performance.