Quick revision
The short answer
ArrayList.get is O(1) because it reads an array slot by index, and add at the end is amortized O(1) because the array occasionally grows by about 1.5 times. ArrayList.remove(0) is O(n) since every other element shifts left, LinkedList.get(i) is O(n) because it walks nodes from the nearer end, HashMap.get is O(1) on average and O(log n) in the worst case for a treeified bucket since Java 8, and TreeMap.get is O(log n) on a red-black tree. Building a string with += in a loop of n iterations copies O(n^2) characters in total, while StringBuilder makes it O(n).
What to remember
In this answer, we saw that ArrayList.get is O(1) because it indexes an array, add at the end is amortized O(1) because the array grows by about 1.5 times, and remove(0) is O(n) because every element shifts. LinkedList.get(i) is O(n) because it walks from the nearer end, HashMap.get is O(1) on average and O(log n) in the worst case since Java 8 treeification, TreeMap.get is O(log n) on a red-black tree, and += on a String in a loop copies O(n^2) characters while StringBuilder keeps it O(n).
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ArrayList.get is O(1) because it reads an array slot by index, and add at the end is amortized O(1) because the array occasionally grows by about 1.5 times. ArrayList.remove(0) is O(n) since every other element shifts left, LinkedList.get(i) is O(n) because it walks nodes from the nearer end, HashMap.get is O(1) on average and O(log n) in the worst case for a treeified bucket since Java 8, and TreeMap.get is O(log n) on a red-black tree. Building a string with += in a loop of n iterations copies O(n^2) characters in total, while StringBuilder makes it O(n).
Why this question is asked
Complexity questions about the standard library look like trivia, but they explain real slowdowns: an endpoint that removes from the front of a list, a loop that calls get(i) on a LinkedList, a log line built with += in a loop. Interviewers want the reason behind each answer, because the reason is what lets us predict the cost of code we have not seen before. In this answer, we will recap what big-O and amortized cost mean, then go through the list-based operations, the map lookups and string concatenation, each with the reason and a small example.
1A Quick Recap of Big-O
Big-O describes how the work of an operation grows as the data grows, ignoring constant factors. O(1) means the work does not depend on the size, O(log n) means it grows by a fixed amount each time the size doubles, and O(n) means it grows in proportion to the size. For a single operation, we usually quote the average case and mention the worst case when they differ.
Amortized cost is a third view: the average cost per operation over a long sequence of operations, even if a few individual operations are expensive. We will need it for ArrayList.add.
| Operation | Cost | Reason |
|---|---|---|
ArrayList.get(i) | O(1) | direct index into the backing array |
ArrayList.add(x) at the end | amortized O(1) | occasional growth by about 1.5 times |
ArrayList.remove(0) | O(n) | System.arraycopy shifts n - 1 elements |
LinkedList.get(i) | O(n) | walks nodes from the nearer end |
HashMap.get(k) | O(1) average, O(log n) worst | one bucket; treeified buckets since Java 8 |
TreeMap.get(k) | O(log n) | red-black tree height is logarithmic |
| s += x, n times | O(n^2) characters copied | each += copies the whole string so far |
2ArrayList: get, add and remove(0)
An ArrayList keeps its elements in a plain Object[] called elementData. get(i) checks the index and returns elementData[i], one memory read wherever i is, so it is O(1).
add(x) at the end writes into the next free slot, which is O(1) as long as there is room. When the array is full, ArrayList allocates a new array about 1.5 times larger (newCapacity = old + old / 2) and copies everything across, which costs O(n) for that one call. Because the capacity grows by a factor rather than by a fixed amount, these copies become rarer as the list grows, and the total copying over n adds stays proportional to n. So the amortized cost of add is O(1).
remove(0) is different. After removing the first element, ArrayList keeps the elements contiguous by shifting every remaining element one position left with System.arraycopy. That is n - 1 moves, so O(n). arraycopy is very fast per element, but on a list of a million elements, removing from the front in a loop is a million times a million moves.
Notice that the fix is a different structure, not a faster loop. ArrayDeque keeps a head index into a circular array, so removing from the front just moves the index.
3LinkedList.get(i): Walking the Nodes
A LinkedList stores each element in its own node object with links to the previous and next nodes. There is no array, so to find element i it must follow links. The JDK implementation is a little smarter than starting at the head every time: if i is in the first half, it walks forward from the first node, otherwise it walks backward from the last. That halves the work but it is still O(n).
The trap is a loop that looks harmless. for (int i = 0; i < list.size(); i++) list.get(i) on a LinkedList does about n/4 steps on average per get, so the whole loop is O(n^2). Iterating with a for-each loop or an Iterator is O(n) in total, because the iterator remembers its current node.
4HashMap.get and TreeMap.get
HashMap.get hashes the key, picks a bucket with a bit mask, and compares the key against the few entries in that bucket. If keys spread evenly, a bucket holds about one entry, so the cost does not depend on the map's size: O(1) on average. The worst case is when many keys share a bucket. Before Java 8 such a bucket was a linked list, and lookups in it were O(n). Since Java 8, once a bucket has 8 entries and the table has at least 64 buckets, HashMap converts the bucket to a red-black tree, which makes the worst case O(log n). There is a fine print: the tree can only order keys efficiently when they implement Comparable; for colliding keys that are not Comparable it may still have to search much of the bucket.
TreeMap keeps all its entries in one red-black tree, a binary search tree with colouring rules that keep its height at most about twice log2(n). get(k) compares k with the root and goes left or right at each level, so it takes O(log n) comparisons: about 20 for a million entries. That is slower than a HashMap lookup, but TreeMap gives sorted iteration and range queries in return.
5String += in a Loop
A Java String is immutable: once created, its characters never change. So s += x cannot append to s. It creates a new String containing a copy of all of s plus x, and makes s point to it. Since Java 9 the compiler implements += with an invokedynamic call to StringConcatFactory, which makes a single concatenation efficient, but it still produces a new string each time, so the copying in a loop remains.
StringBuilder works like an ArrayList of characters: it appends into a buffer that grows by a factor when full, so the total work is O(n). For joining a collection, String.join or Collectors.joining use a builder internally.
6Follow-up questions
What does amortized mean exactly?
Amortized cost is the total cost of a sequence of operations divided by the number of operations, in the worst case over the sequence. For ArrayList.add, most calls cost one write, and a few calls cost a full copy when the array grows. Because the capacity grows by 1.5 times each time, the copies over n adds sum to a constant multiple of n, so each add costs O(1) on average over the sequence. It is a guarantee about the sequence, not a probability: no sequence of n adds can cost more than O(n) in total, although any single add may cost O(n).
Is ArrayList.add(0, x) also O(n)?
Yes. Inserting at the front shifts every existing element one position right with System.arraycopy, the mirror image of remove(0). Inserting in the middle shifts the elements after the position, so it is O(n - i).
When is LinkedList faster than ArrayList?
Almost never in practice. Its one advantage is O(1) insertion or removal at a position we already hold through a ListIterator. Finding that position is O(n), and its per-node allocation and poor cache locality make it slower than ArrayList or ArrayDeque for nearly every workload.
7Conclusion
In this answer, we saw that ArrayList.get is O(1) because it indexes an array, add at the end is amortized O(1) because the array grows by about 1.5 times, and remove(0) is O(n) because every element shifts. LinkedList.get(i) is O(n) because it walks from the nearer end, HashMap.get is O(1) on average and O(log n) in the worst case since Java 8 treeification, TreeMap.get is O(log n) on a red-black tree, and += on a String in a loop copies O(n^2) characters while StringBuilder keeps it O(n).
Open with
ArrayList.get is O(1): it is a bounds check and a read from the backing array. Adding at the end is amortized O(1): usually it just writes the next slot, and when the array is full it allocates one about 1.5 times bigger and copies, but because growth is geometric the total copying over n adds is proportional to n. remove(0) is O(n), because System.arraycopy shifts every remaining element one place left, so consuming a list from the front is quadratic and I would use an ArrayDeque instead.
Then, if they want more
LinkedList.get(i) is O(n): there is no array, so it walks node by node, starting from whichever end is nearer. HashMap.get is O(1) on average, since it hashes to one bucket with about one entry; since Java 8 a crowded bucket turns into a red-black tree, so the worst case is O(log n), best when keys are Comparable.
TreeMap.get is O(log n) because it walks a balanced red-black tree, about 20 levels for a million entries. And s plus-equals x in a loop is O(n squared) in characters copied, because strings are immutable and every step copies the whole string so far; StringBuilder appends into a growable buffer, so it is O(n).
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Give the time complexity of these Java operations and the reason: ArrayList.get...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/72203f22a1/