---
# System prepended metadata

title: Priority Queue - Hadrian

---

# Priority Queue Challenge

## Context

In this challenge, you'll be implementing an in-memory priority queue in TypeScript. Your priority queue will need to support a few operations:

* Adding an item with a given priority
* Removing the item with the highest priority and returning it
* Peeking at the item with the highest priority without removing it

Your system will demonstrate some nuanced behaviors:

* When attempting to pop or peek at an item and the queue is empty, **the operation must block** (via promise resolution) until an item is available.
* The priority of items dictates the order of popping or peeking; lower numerical values represent higher priorities.
* If two items have the same priority, the first to be pushed will be first to be popped

A detailed specification of the queue's expected behavior is provided in the TypeScript interface you will implement (see Getting Started, below).

## Expectations

This priority queue is envisioned as an in-memory construct for the scope of this exercise, without the complexity of persistence, error handling, or scalability considerations expected in a production environment. This is a focused exercise to evaluate your grasp of asynchronous programming patterns and data structure manipulation in TypeScript. Your focus should be on completing as much of the exericse as possible before refactoring for pleasing code aesthetics or performance.

## Getting Started

Please visit the [TypeScript Playground][1], where you'll find an interface for the priority queue (PriorityQueue) along with a basic, unimplemented class (`InMemoryPriorityQueue`) conforming to this interface. Additionally, several tests are provided to guide you through implementing the queue's functionality. We recommend letting the tests drive your implementation, one step at a time. Good luck!

[1]: https://www.typescriptlang.org/play/?ts=5.4.3#code/PQKhCgAIUgRBTAZgSwHbwM6QPYAd4BOAhgC7LapaLYGRGS4HlMkCekAjgK7w+QAWRVABMANmgDmkZCXgBbLNkSQ2+SAAMAKuoB0kAJKyFdAvCgxGzGcgBe8YZABGRDPZyo6kVFzmFkAYyJRSAA3IJ4AGkgAdxl+SFFsaMIvH0dCLDRhANJJAWQJfkJzBiYaGVYdc2BwNFkCRCJ-eEgABTKWVgBFHh4AHk0APkgAbyhIUAhIaZgAQWFhLCFpIxi4ugwMbH9kUjdLcrYVbBUizl74KunocehIAAFcImI5UqsjgFpIADkfP0DgmFRHxqLQZPI3odKpAADJJFJAniZEQ5WS3GDTfgFIq0A6dK4zB5PF4rCFfTRncGvJQqVhqLTqY50BZMkhnbi8S7omrTXBcDD8AAUeIqAC5UnJ0gQ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