0% found this document useful (0 votes)
9 views3 pages

Custom Thread-Safe Priority Queue Design

This document outlines a coding challenge to design and implement a custom priority queue in Java. The priority queue must: 1) Store items with a generic type and integer priority 2) Dequeue items by priority, ensuring that for every 2 items of a given priority X dequeued, the next item must have priority X+1 3) Be thread-safe for use in multi-threaded environments 4) Block producers when full and consumers when empty Test coverage and documentation are required, with bonus points for generalizing the throttle rate configuration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Custom Thread-Safe Priority Queue Design

This document outlines a coding challenge to design and implement a custom priority queue in Java. The priority queue must: 1) Store items with a generic type and integer priority 2) Dequeue items by priority, ensuring that for every 2 items of a given priority X dequeued, the next item must have priority X+1 3) Be thread-safe for use in multi-threaded environments 4) Block producers when full and consumers when empty Test coverage and documentation are required, with bonus points for generalizing the throttle rate configuration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding Challenge

Java developer position


Confidential

Problem Definition
You are expected to design a custom priority queue. The following is the required logic
for this queue:
● Each item in the queue has Generic Type (T) along with a priority.
● Priority is a positive integer. The highest priority is “1”.
● Multiple items might have the same priority.
● The following logic determines the deque process:
○ Items with higher priority are dequeued first.
○ For every two (2) items dequeued with priority "x", the next item must be
of priority "x+1". We call this the Throttle Rate of a priority class.
○ The two items dequeued must not necessarily be dequeued
consecutively for this constraint to apply. See examples below.
○ Items follow FIFO order in their own priority class.
● The queue must be thread-safe as it is used exclusively in multi-threaded
environments.
● The queue has a fixed, pre-determined size.
● On a full queue, the producer must be blocked till capacity is available.
● On an empty queue, the consumer must be blocked till queue items are
available.

1
Deliverables
1. Identify a scenario where lower priority items may never exit the queue. Provide
a solution.
2. Implement this data structure and incorporate your answer to the deliverable (1).
Please add test coverage to your comfort level.
3. Please use Java 8, Maven, and IntelliJ (alternatively Eclipse). Please submit the
solution as a single zip file, not individual files. If attaching to email message
didn’t work, please share via GDrive/DropBox/etc.

Examples
Example 1:
Please consider the sequence below:
● Enqueue (left to right): 4 1 3 2 1 2
● Dequeue: 1
● Dequeue: 1
● Enqueue: 1
● Dequeue: 2 (Constraint B)
● Dequeue: 1
● Dequeue: 2
● Dequeue: 3

Example 2:
● Enqueue (left to right): 4 1 3 2 2
● Dequeue: 1
● Dequeue: 2
● Enqueue: 1
● Dequeue: 1
● Dequeue: 2
● Dequeue: 3 (Constraint B)
● Dequeue: 4

2
Example 3:
Consider the following sequence of numbers for a single producer (spaces are only for
readability purposes):
41321423241335213612424132152112311

The desired dequeue sequence will be the following for a single consumer:

11211231121123411212322345233434564

Final Notes:
● The items can be enqueued/dequeued at any time in a multi-threaded environment.
● Correctness, simplicity, reusability, and efficiency are the main concerns here.
● Adequate test coverage and code documentation are required.
● Bonus: Generalize the throttle Rate. Please comment on how the data structure in
Problem A can be generalized to accommodate an arbitrary Throttle Rate for each
priority class.

Common questions

Powered by AI

FIFO order within each priority class ensures that items with the same priority are dequeued in the order they were enqueued. This preserves the fairness and predictability of processing tasks at the same priority level, which is critical for applications that depend on order-based processing. It balances the prioritization with fairness for tasks enqueued within the same priority level .

Efficiency and correctness in the queue operations are ensured by using synchronized data structures or locking mechanisms that prevent race conditions. Ensuring that the enqueue and dequeue operations are atomic is crucial. Additionally, managing the throttle rate and enforcing the specific dequeuing order correctly are essential for correctness. Implementing conditions that check the queue's state and throttle constraints before proceeding with operations also contributes to maintaining the required order and efficiency .

The fixed, pre-determined size of the queue means that producers must be blocked when the queue is full, which requires mechanisms for handling blocked threads and possibly queuing them. It necessitates a strategy for waiting producers to signal when space becomes available. It also requires careful management of consumers to ensure they do not attempt dequeuing from an empty queue, necessitating condition checks and proper signaling .

Lower priority items may get indefinitely blocked if the queue continuously receives items of a high priority in a multi-threaded environment. Since the dequeue operation prioritizes higher priorities and implements a throttle rate that allows only one lower priority item to be dequeued after two high-priority items, continuous enqueuing of high-priority items could result in starvation of lower priority items if the queue is consistently filled with high-priority elements without space or opportunity for their release .

Effective testing strategies include unit testing to validate basic operations and edge cases of the queue, stress testing with large volumes of data to ensure performance under load, and concurrency testing to observe behavior when multiple threads interact with the queue. It's also important to test different scenarios of priority enqueuing and dequeuing, as well as fairness testing to ensure the throttle rate mechanism properly functions under various conditions .

Critical factors include the current load and the priority of items already in the queue, system performance requirements, and the overall impact on the system's throughput and latency. The blocking mechanism should ensure that producers or consumers don't indefinitely wait and that system stability is maintained even under peak conditions. Additionally, fairness and efficiency must be balanced, so priority constraints are met without starving other operations .

To generalize the throttle rate, the data structure could maintain a separate throttle rate for each priority class rather than a fixed one. This means different priorities could have customized rules for how frequently they allow lower priority items through. This can be achieved by implementing a mapping of priority levels to throttle rates and dynamically adjusting them based on the system's needs, ensuring both flexibility and efficiency in processing tasks .

The primary challenge is implementing a custom priority queue that not only adheres to traditional priority and FIFO rules but also incorporates a Throttle Rate constraint where, for every two items dequeued of priority 'x', the next item must be of priority 'x+1'. This adds complexity to typical queue operations. Additionally, ensuring thread safety is crucial since the queue will be accessed in a multi-threaded environment, which requires careful synchronization to manage concurrent access without causing race conditions or deadlocks .

A solution to prevent starvation of lower priority items is to implement a fairness mechanism that monitors the dequeue frequency of different priorities. Once it detects that lower priority items are being unfairly starved, it could temporarily adjust the throttle rate to allow an increased frequency of dequeuing lower priority items. Another approach is to periodically boost the priority of older lower priority items, ensuring they eventually get dequeued .

A real-world scenario for this throttled priority queue is in managing network packets in a router. High-priority packets might be critical data like video or audio frames that need timely processing, while lower priority packets might be regular data transfers. The throttle ensures that even lower priority packets are processed in a timely manner to prevent saturation by high-priority traffic, which could otherwise cause significant delays or data loss in essential, though less time-sensitive transfers .

You might also like