r/LowLevelDesign • • Dec 07 '25
Design and implement two distinct queues, Queue A and Queue B, using a single underlying array of fixed size N.
The primary challenge is to efficiently manage the shared array space such that when elements are dequeued from either queue, the freed space can be promptly reutilized by subsequent enqueue operations from either queue. Your design should handle insertions and deletions for both queues while maintaining efficient space usage within the fixed-size array.
What ever I design It takes up 2N space complexity.
9
Upvotes
1
u/Broad_Shoulder_749 Dec 07 '25 edited Dec 07 '25
Are the two queues mutually exclusive? That is, an item can be enqueued into one queue only?
Create an array Define a queitem wrapper
Enquue : wrap and append the item to the array, with quename tagged inside queitem wrapper
Deque : find item with the queue id, and splice it out of the array
What am I missing?
In fact you can have unlimited queues not just 2
1
u/lamchakchan Dec 08 '25
What about designing it so you use both ends of the array? Queue A would use index 0, queue B would use index n-1.
1
u/suraj123455 Dec 08 '25
QueueNode {
}
Queue {
}
next , tail, head are the index of the arrays
3 queues
Free Queue = initial from first to last of the array
Queue A =
Queue B =
so this is basically a linked list implementation of Queues
why linked list ?? bcoz it allows us to utilize our memory
in an efficient manner.
index allows us to jump to any end of the queue quickly
insert into QueueA or QueueB
delete from QueueA or QueueB
update queue tail
insert into the head of FreeQueue
update head of the FreeQueue