| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice Test
Share
medium Solved by 3 students
ArrayList Insertion Time Complexity
< Prev
Next >

Determine the overall time complexity when inserting n elements into the middle of an ArrayList using add(index, element).

import java.util.ArrayList;
public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        int n = 100; // assume n elements
        for (int i = 0; i < n; i++) {
            int mid = list.size() / 2;
            list.add(mid, i);
        }
    }
}
A

O(n^2)

B

O(1)

C

O(n)

D

O(n log n)

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1kaisuki13 0m 00s 80
#2hiboyshello02 0m 00s -20
#3ponneban00003 4m 04s -274
#4y.seong202713 9m 40s -500
APFIVE © 2020.
Email: [email protected]|Privacy Policy