Algorithms and Data Structures · Junior · Question 21 of 75
Why can a queue built on push and shift become slow on large inputs?
- ✓Correct answer:
shiftmoves all remaining elements, which is O(n) - 2
pushcopies the whole array every time - 3Arrays in JavaScript can't hold more than 10,000 items
Explanation
Each shift re-indexes the rest of the array, so dequeuing n items can cost O(n²) in total. For heavy use keep a head index and read arr[head++], or use a linked list or a ring buffer.