Skip to content
Sharp

Collections · interview question

Choose the typical (average) time complexity of each operation.

Matrix / gridMedium

Choose for each row.

Choose one option per row
ItemO(1)O(log n)O(n)
`ArrayList.get(i)`
`LinkedList.get(i)`
`HashMap.get(key)`
`TreeMap.get(key)`
`ArrayList.add(e)` at the end (amortized)
Answer and explanation

Correct answer

  • ArrayList.get(i): O(1)
  • LinkedList.get(i): O(n)
  • HashMap.get(key): O(1)
  • TreeMap.get(key): O(log n)
  • ArrayList.add(e) at the end (amortized): O(1)

Why

LinkedList rarely wins in practice. Even inserts in the middle need an O(n) walk to find the position, and arrays are cache-friendly.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →