0

I am practicing Data Structures on HackerRank and i came across a question which says to implement a queue using two stacks so i looked up for it and understood the logic which was quite simple and it was like : Make two stacks Enqueue And Dequeue : for enqueue operation push element in Enqueue stack : for dequeue operation -first check if the Dequeue stack is empty or not ,if empty then pop all elements from Enqueue stack and push them into Dequeue stack so that the Enqueue is totally reversed and now we can pop the top from the Dequeue stack to perform dequeue operation(return the front of the queue) :and if the Dequeue stack is not empty then just pop from that same stcak that would be the front of the queue. below is the an example for loop that transfers all the content of the Enqueue stack and and puts them in Dequeue stack using push and pop operation but the for loop is behaving oddly

let enqueueStack  = [1,2,3,4,5,6]
let dequeueStack = [];
for(let i=0;i<enqueueStack.length;i++){
    dequeueStack.push(enqueueStack.pop())
}
console.log(enqueueStack)
console.log(dequeueStack

and below is the output

[ 1, 2, 3 ]
[ 6, 5, 4 ]

i tried forEach which is also not working

5 Answers5

3

Perfect example for a while loop! As the length of your enqueueStack changes when poping an element. But you don't really care about the length just if enqueueStack has more elements or not.

let enqueueStack  = [1,2,3,4,5,6]
let dequeueStack = [];
while(enqueueStack.length){
    dequeueStack.push(enqueueStack.pop())
}
console.log(enqueueStack)
console.log(dequeueStack)
kevinSpaceyIsKeyserSöze
  • 3,693
  • 2
  • 16
  • 25
1
while(enqueueStack.length > 0) {
    dequeueStack.push(enqueueStack.pop())
}

or

var elementNumber = enqueueStack.length;
for(let i=0;i<elementNumberi++){
    dequeueStack.push(enqueueStack.pop())
}

Array.pop() removes 1 element from an array so its length decreases. When i = 3 the length of enqueueStack is 3 (6 - 3) because you have removed 3 items

Bamak
  • 188
  • 6
1

Using the pop function reduces the length of the enqueueStack, as such instead of querying the length every time you want to compare you should assign it immediately and iterate backwards

let enqueueStack  = [1,2,3,4,5,6]
let dequeueStack = [];
for(let i=enqueueStack.length-1; i>=0; i--){
    dequeueStack.push(enqueueStack.pop())
}
console.log(enqueueStack)
console.log(dequeueStack)

or simply just check if any items are left instead of comparing it with i

let enqueueStack  = [1,2,3,4,5,6]
let dequeueStack = [];
while(enqueueStack.length)
    dequeueStack.push(enqueueStack.pop())
console.log(enqueueStack)
console.log(dequeueStack)
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
1

The issue is about side-effect in loop manipulation. When you check enqueueStack.length, it changes in relation to the pop() operation you did. You can notice by logging the length value:

let enqueueStack  = [1,2,3,4,5,6]
let dequeueStack = [];
for(let i=0;i<enqueueStack.length;i++){
    console.log(`length: ${enqueueStack.length}, i: ${i}`);
    dequeueStack.push(enqueueStack.pop())
}

Without changing approach, a correct solution will be:

let enqueueStack  = [1,2,3,4,5,6]
let dequeueStack = [];
for(let i=0, l=enqueueStack.length;i<l;i++){
    dequeueStack.push(enqueueStack.pop())
}
console.log(enqueueStack)
console.log(dequeueStack)
Greedo
  • 3,438
  • 1
  • 13
  • 28
1

That's because as you start poping items from the enqueue array, the count decreases, after poping 3 items i.e., 6,5,4 the enqueueStack length is 3 and so is i's value. Hence it stops at 3 elements.

let enqueueStack  = [1,2,3,4,5,6]
let dequeueStack = [];
let arrayLength = enqueueStack.length
for(let i=0;i<arrayLength;i++){
    dequeueStack.push(enqueueStack.pop()) // use this to add items in reverse order
//dequeueStack.push(enqueueStack.shift()) add items in the same order 
}
console.log(enqueueStack)
console.log(dequeueStack)
prabhu
  • 878
  • 9
  • 17