Suppose that we say:

ArrayList<Integer> lst = ...;
int sum = 0;
for (int i = 0; i < lst.size(); i++ )
    sum += lst.get(i);

What is the Big O of this code?

  • A: O(1)
  • B: O(n)
  • C: O(n log n)
  • D: O(n2)
  • E: O(n3)

    Answer: B

    Since ArrayList is basically an array in its implementation, the .get(i) operation is O(1).

    Contents    Page-10    Prev    Next    Page+10    Index