Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 18, 2022 03:09 am GMT

JavaScript Interview Coding Test Problem 8

Learn how to create a queue when the only data structure you have access to is a stack.
Instructions
Write a function that creates a queue using two stacks. Besides these twD stacks, you may not use any additional data structures in your implementation. It should have the methods enqueue , dequeue , and s ize .

Feel free to change the code provided below when you start. Its meant to be a guide.
Solution

1   class Queue {2     constructor() {3       this._enqueueStorage[];4       this._dequeueStorage[];5     }67     enqueue(1tem) (8       this. enqueuestorage.push(item); 9  }1011    dequeue( ) (12      if(this. dequeueStorage.length) (13        return this. dequeueStorage.pop();14  }1516  1f(th1s ._enqueue5torage . 1ength) (17    while(this. enqueue5torage.length) {18    this. dequeuestorage.push(this. enqueue5torage.pop()); 192021      return this. dequeueStorage.pop(); 222324     console.warn('Attempting to dequeue from an empty queue');25  return undefined; 26     

How it Works
We have two storage stacks, this ._enqueueStorage and
this . dequeuestorage . To see how they interact, lets go thrDugh an example.
Enqueuing
We want to put 5 elements onto the queue: 1, 2, 3, 4, and s . We enqueue
all of these, and they all go into enqueuestorage .

enqueuestorage: [1, 2, 3, 4, 5] dequeuestorage: []

We now want to dequeue an item. Lets dive into our dequeue function.
Dequeuing
We skip the if-statement on line 12 since dequeuestorage is empty. We move
on to line 16.

Inside that if-statement (line 16), we pop every item off of enqueuestorage and put them into dequeuestorage . When the while-loop (lines 17 - 19) is finished, enqueuestorage is empty and dequeuestorage has the five items, but in reverse.
enqueuestorage: [) dequeuestorage: [5, 4, 3, 2, 1]
On line 21, we pop dequeuestorage and return the item, i.
enqueuestorage: [] dequeuestorage: [5, 4, 3, 2]
If we want to dequeue again, we can enter the dequeue method once more. This time we go into the first if-statement and pop an item off of dequeuestorage and return it.
enqueuestorage: [] dequeuestorage: [5, 4, 3]
We can keep dequeuing if we like. As long as dequeuestorage has items in it, the last item will be popped off and returned to us.
Summary
These steps together make it so our queue functions properly. Enqueuing pushes items onto one stack. Dequeuing pops them from the second stack. When the second stack is empty and we want to dequeue, we empty the first stack onto the second, reversing the order of items.
Enqueue Time
Every enqueue event is a simple push onto a stack. This means that each enqueue has a time complexity of:
0(1)
Dequeue Time
So, for most cases, we have o(1) , and for a few rare cases, we have o(n) We can merge these into a single time complexity.
Queue Lifecycle
Lets track the life of three items in our queue. Starting with an empty queue, lets enqueue i , 2, and 3 . This puts them on our first stack.
enqueuestorage: [1, 2, 3] dequeuestorage: []
So far, weve performed 3 operations: inserting each of those items onto the stack.
Lets dequeue all three items. First, all items from enqueuestorage get
transferred to dequeuestorage .
enqueuestorage: [] dequeuestorage: [1, 2, 3]
Well say that a transference is 1 Dperation. This is another 3 operatiDns total, bringing us up to 6.


Original Link: https://dev.to/stormytalent/javascript-interview-coding-test-problem-8-4c4i

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To