Algorithms and Data Structures · Junior · Question 21 of 75

Why can a queue built on push and shift become slow on large inputs?

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.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 21

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz