🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: STL Containers: all 9 questions

Quiz: STL Containers · Question 9 of 9

Inserting at the front of a large vector with v.insert(v.begin(), x) is slow. Why?

Inserting at the front of a large vector with v.insert(v.begin(), x) is slow. Why?

  1. A.vector must sort itself after every insert
  2. B.Every existing element has to shift one place to the right
  3. C.vector allocates a new block for each insert
  4. D.Inserting at the front is not allowed
Answer & explanation

Correct answer: Every existing element has to shift one place to the right

Shifting n elements costs O(n); deque or list handle front insertion better.

Can you get the rest right without peeking?

Take the timed Quiz: STL Containers quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →