AniUI Academy
medium+250 XPPractice

A deque you can push and pop from both ends

Some queues are entered from both ends: a carousel you can scroll either way, a chat that prepends older messages while appending new ones, a sliding window that grows at the front and shrinks at the back.

Implement a Deque class (double-ended queue). It is constructed with no arguments and starts empty. pushFront(value) and pushBack(value) add a value at that end and each return the new size. popFront() and popBack() remove and return the value at that end, or return undefined when the deque is empty. peekFront() and peekBack() return the value at that end without removing it, or undefined when empty. size() returns how many values are held. toArray() returns the values as a plain array ordered front to back, and [] when empty.

What it has to do

  • pushFront and pushBack each return the new size.
  • toArray() lists values from front to back.
  • All four pop and peek methods return undefined on an empty deque.
  • Values pushed to the front come out of popFront in reverse push order.
  • The deque stays consistent when drained from both ends.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.