B. Using unshift adds element at the front, not the end
C. Array should be initialized as null, not []
D. Missing return statement in enqueue method
Solution
Step 1: Understand unshift behavior
unshift adds element at the start (front) of the array, not the end.
Step 2: Compare with enqueue operation
Enqueue must add element at the end, so unshift is incorrect here.
Final Answer:
Using unshift adds element at the front, not the end -> Option B
Quick Check:
Enqueue adds at end, unshift adds at front [OK]
Hint: Use push() for enqueue, not unshift() [OK]
Common Mistakes:
Confusing unshift with push
Thinking unshift adds at end
Ignoring method behavior differences
5. You have a queue implemented with an array. You want to enqueue multiple elements [7, 8, 9] at once while preserving their order. Which code snippet correctly enqueues all elements in order?
hard
A. queue.unshift(7, 8, 9);
B. queue.concat([7, 8, 9]);
C. queue.push([7, 8, 9]);
D. [7, 8, 9].forEach(e => queue.push(e));
Solution
Step 1: Analyze each option for enqueue behavior
queue.push([7, 8, 9]); adds the entire array as one element, not individual elements. queue.concat([7, 8, 9]); creates a new array but does not modify queue. [7, 8, 9].forEach(e => queue.push(e)); pushes each element individually, preserving order. queue.unshift(7, 8, 9); adds elements at front, reversing order.
Step 2: Choose the method that enqueues elements individually at the end
Using forEach with push adds each element in order at the end of queue.
Final Answer:
[7, 8, 9].forEach(e => queue.push(e)); -> Option D
Quick Check:
forEach + push enqueues elements in order [OK]
Hint: Use forEach with push to enqueue multiple elements in order [OK]
Common Mistakes:
Using push with array adds nested array
Using unshift reverses order
Forgetting to assign concat result back to queue
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.