Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-007

Deque Operations

Problem

Simulate a double-ended queue (deque) supporting operations at both ends.

operations contains "append_left", "append_right", "pop_left", "pop_right", "front", "back", or "size". Appends record None, pops return the removed value or None when empty, front and back return the end values or None, and size returns the current length.

Return the list of recorded results.

Hint

A Python list supports both ends; insert(0, x) and pop(0) work on the left.

Test cases

case 1
in: [["append_right","append_left","append_right","front","back","pop_left","pop_right","size"],[1,2,3,0,0,0,0,0]]
expected: [null,null,null,2,3,2,3,1]
case 2
in: [["pop_left","pop_right","front","back"],[0,0,0,0]]
expected: [null,null,null,null]
case 3
in: [["append_left","append_left","size"],[5,6,0]]
expected: [null,null,2]
case 4
in: [["append_right","pop_right","pop_right"],[9,0,0]]
expected: [null,9,null]
Starter codePython
def deque_operations(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor