ProgrammingEasyAccenture · Cognizant pattern

What is the time complexity of get(i) on a Java ArrayList and on a LinkedList?

  1. A. O(1) and O(n)
  2. B. O(1) and O(1)
  3. C. O(log n) and O(n)
  4. D. O(n) and O(1)

Correct answer: A. O(1) and O(n)

ArrayList is backed by an array, so it jumps straight to index i. LinkedList must walk node by node from the nearer end, which is O(n).

More OOP with C++ and Java questions

Exams that ask this topic