← Quiz: Java Collections (List and Set): all 10 questions
Quiz: Java Collections (List and Set) · Question 9 of 10
What does it cost to insert an element at index 0 of an ArrayList with n elements?
What does it cost to insert an element at index 0 of an ArrayList with n elements?
- A.O(1)
- B.O(log n)
- C.O(n), because every element must shift one place right
- D.O(n log n)
Answer & explanation
Correct answer: O(n), because every element must shift one place right
An ArrayList is a dynamic array, so inserting at the front moves all the existing elements.
Can you get the rest right without peeking?
Take the timed Quiz: Java Collections (List and Set) quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →