Stable Priority Queue

Implement StablePriorityQueue with an empty constructor, enqueue(taskId, priority), peek(), dequeue() and isEmpty(). Greater numeric priority is served first.…

Implement StablePriorityQueue with an empty constructor, enqueue(taskId, priority), peek(), dequeue() and isEmpty(). Greater numeric priority is served first. Equal priorities are served in enqueue order, never task-ID order. Every enqueue uses a task ID not used earlier in the stream, even if the earlier task was removed. peek reads the next task without mutation; dequeue removes and returns that task. Both return -1 when empty. Return one result per call: null for construction/enqueue, the strict task ID for peek/dequeue, and a strict boolean for isEmpty.

- `operations` and `arguments` are aligned arrays with 1–1,001 total records, including one initial `StablePriorityQueue` constructor with `[]`. - Every `enqueue` has `[taskId, priority]`. Task IDs are globally fresh strict integers from 1 to 2,147,483,647; priorities are strict integers from −2,147,483,648 to 2,147,483,647. - `peek`, `dequeue` and `isEmpty` have no arguments. Other methods and repeated constructors are invalid. - Booleans and fractional numbers are not integer arguments. Caller input arrays remain unchanged. - Teaching traces use at most 12 records, task IDs 1–99 and priorities −99 to 99. Coding retains the full bounds above.

Example 1: input operations = ["StablePriorityQueue","enqueue","enqueue","enqueue","peek","dequeue","peek","dequeue","dequeue","isEmpty"], arguments = [[],[99,4],[1,4],[8,9],[],[],[],[],[],[]]; output [null,null,null,null,8,8,99,99,1,true]. ID8 has greater priority. Among the two priority4 tasks, earlier ID99 precedes later ID1.

Example 2: input operations = ["StablePriorityQueue","enqueue","enqueue","enqueue","enqueue","dequeue","enqueue","dequeue","peek"], arguments = [[],[4,1],[3,4],[8,2],[2,7],[],[9,8],[],[]]; output [null,null,null,null,null,2,null,9,3]. Appending a stronger task moves it toward the root; removing it restores the same priority/arrival rule.

Category: Queues. Topic: queue. Difficulty: Medium. Patterns: linear-scan.