Medium · Stacks & Queues

Implement queue with max

Given a sequence of enqueue(val), dequeue() and max() operations on an initially empty queue, return in order the value returned by every dequeue() and max() call, using 0 when the queue is empty.

Examples

Example 1

{
  "ops": [
    {"kind": "enq", "val": 3},
    {"kind": "enq", "val": 1},
    {"kind": "enq", "val": 4},
    {"kind": "enq", "val": 1},
    {"kind": "enq", "val": 5},
    {"kind": "max"},
    {"kind": "deq"},
    {"kind": "max"}
  ]
}

Output: [5, 3, 5]

Example 2

{
  "ops": [
    {"kind": "enq", "val": 5},
    {"kind": "enq", "val": 2},
    {"kind": "enq", "val": 6},
    {"kind": "deq"},
    {"kind": "max"},
    {"kind": "deq"},
    {"kind": "max"}
  ]
}

Output: [5, 6, 2, 6]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Stacks & Queues problems