Explain why indexing is O(1) and inserting in the middle is O(n).
Entries sit in contiguous slots, so index i is at base + i × size; an edit in the middle must move every entry after it.
Use it when the question is about a position, a count of moves, or how a list grows. Signal words: the entry at a given position, insert or remove in the middle, how many entries move, the list grows as entries arrive.
Read or write at an index: one address calculation, O(1).
Insert or delete at index i: shift the entries after i by one slot. Start from the end for an insert, and from the start for a delete. That is n - i moves, O(n).
Append: write at the end, and double the room when it is full. O(1) amortised.
It does not suit many edits at the front or in the middle of a long, ordered list. Rebuild in one pass, or use a different structure.
Cost: index O(1), end O(1) amortised, middle O(n), space O(n) with up to twice the room reserved.
def insert_at(values, index, value):
# [1] Make room: a copy with one more slot at the end.
items = list(values) + [0]
n = len(values)
# [2] Shift the tail right by one place, starting from the last entry, so that nothing is overwritten before it is read.
for j in range(n, index, -1):
items[j] = items[j - 1]
# [3] Write the new value into the gap.
items[index] = value
# [4] Return the longer list.
return itemspublic int[] insertAt(int[] values, int index, int value) {
// [1] Make room: a copy with one more slot at the end.
int n = values.length;
int[] items = new int[n + 1];
for (int i = 0; i < n; i++) {
items[i] = values[i];
}
// [2] Shift the tail right by one place, starting from the last entry, so that nothing is overwritten before it is read.
for (int j = n; j > index; j--) {
items[j] = items[j - 1];
}
// [3] Write the new value into the gap.
items[index] = value;
// [4] Return the longer list.
return items;
}vector<int> insertAt(vector<int>& values, int index, int value) {
// [1] Make room: a copy with one more slot at the end.
vector<int> items = values;
items.push_back(0);
int n = (int)values.size();
// [2] Shift the tail right by one place, starting from the last entry, so that nothing is overwritten before it is read.
for (int j = n; j > index; j--) {
items[j] = items[j - 1];
}
// [3] Write the new value into the gap.
items[index] = value;
// [4] Return the longer list.
return items;
}function insertAt(values, index, value) {
// [1] Make room: a copy with one more slot at the end.
const items = [...values, 0];
const n = values.length;
// [2] Shift the tail right by one place, starting from the last entry, so that nothing is overwritten before it is read.
for (let j = n; j > index; j--) {
items[j] = items[j - 1];
}
// [3] Write the new value into the gap.
items[index] = value;
// [4] Return the longer list.
return items;
}def count_copies(appends):
# [1] Start with room for one entry and nothing stored.
capacity = 1
size = 0
copies = 0
# [2] Append one entry at a time.
for _ in range(appends):
# [3] When the array is full, allocate twice the room and copy every stored entry across.
if size == capacity:
copies += size
capacity *= 2
# [4] Store the new entry in the next free slot.
size += 1
# [5] Return how many entries were copied in all.
return copiespublic long countCopies(int appends) {
// [1] Start with room for one entry and nothing stored.
long capacity = 1;
long size = 0;
long copies = 0;
// [2] Append one entry at a time.
for (int a = 0; a < appends; a++) {
// [3] When the array is full, allocate twice the room and copy every stored entry across.
if (size == capacity) {
copies += size;
capacity *= 2;
}
// [4] Store the new entry in the next free slot.
size++;
}
// [5] Return how many entries were copied in all.
return copies;
}long long countCopies(int appends) {
// [1] Start with room for one entry and nothing stored.
long long capacity = 1;
long long size = 0;
long long copies = 0;
// [2] Append one entry at a time.
for (int a = 0; a < appends; a++) {
// [3] When the array is full, allocate twice the room and copy every stored entry across.
if (size == capacity) {
copies += size;
capacity *= 2;
}
// [4] Store the new entry in the next free slot.
size++;
}
// [5] Return how many entries were copied in all.
return copies;
}function countCopies(appends) {
// [1] Start with room for one entry and nothing stored.
let capacity = 1;
let size = 0;
let copies = 0;
// [2] Append one entry at a time.
for (let a = 0; a < appends; a++) {
// [3] When the array is full, allocate twice the room and copy every stored entry across.
if (size === capacity) {
copies += size;
capacity *= 2;
}
// [4] Store the new entry in the next free slot.
size++;
}
// [5] Return how many entries were copied in all.
return copies;
}This version loops for j in range(index + 1, n + 1) and copies items[j] = items[j - 1]. Each copy overwrites the entry that the next copy needs.
This version loops for j in range(n, index + 1, -1), so it never copies into place index + 1. The entry that belongs there stays as it was.
This version allocates one extra slot each time the array is full, so every append after that is a full copy. It is correct, and it is quadratic.
This version reasons that arrays have O(1) access, so the 30,000 front inserts take 30,000 steps. It confuses reading an index with editing at an index.
The app also keeps a feed where every new item goes to the front, and the feed reaches 100,000 items. Inserting each item at the front of an array makes 4,999,950,000 moves in all.
systemdesignmastery.dev/dsa/cheatsheets/arrays-in-memory