Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-190

Double-Ended Priority Queue

Problem

Simulate a double-ended priority queue supporting both minimum and maximum operations.

operations contains "insert" (values[i] is the value), "pop_min", "pop_max", "peek_min", or "peek_max". Pops return the removed extreme or None when empty, and peeks return the current extreme or None.

Return the list of recorded results (None for insert).

Hint

A sorted list gives O(1) access to both extremes.

Test cases

case 1
in: [["insert","insert","insert","peek_min","peek_max","pop_min","pop_max"],[5,1,3,0,0,0,0]]
expected: [null,null,null,1,5,1,5]
case 2
in: [["pop_min","pop_max","peek_min"],[0,0,0]]
expected: [null,null,null]
case 3
in: [["insert","peek_min","peek_max"],[7,0,0]]
expected: [null,7,7]
case 4
in: [["insert","insert","pop_min","pop_min","pop_min"],[2,2,0,0,0]]
expected: [null,null,2,2,null]
Starter codePython
def depq_ops(operations, values):
    # Your code here
    pass

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