What is an array?
The array is a linear, sequential data structure that stores elements accessible by indices; in Java it has fixed size and a defined type at creation.
What is the “killer feature” of arrays?
Direct random access by index in constant time O(1).
What does O(1) mean in array access?
It means access time does not grow with the array size; it is constant.
What is an array API?
An abstraction built on top of the raw array that provides extra functionality, safety, and optimizations.
Array vs ArrayList in Java (size)
Array: fixed size. ArrayList: automatically resizable.
Array vs ArrayList in Java (types)
Array: stores primitives and objects. ArrayList: stores only objects (primitives via wrappers).
Array vs ArrayList in Java (methods)
Array: manual operations. ArrayList: built-in methods such as add, remove, get, set, size.
4 Array characteristics
1)Fixed size and contiguous elements in memory -
2) Index access in O(1). - Random Access
3) Insertion/removal in the middle requires shifting elements: O(n).
4) Can hold anything (primitives and objects)
Why is index access O(1)?
Because the address of the element is computed as base + index × element_size.
Average cost of inserting at the end of a full array
Amortized O(1) in a dynamic array; O(n) worst case due to reallocation and copying.
How does insertion work in a simple array?
Choose a position, shift elements to the right, and write the new value.
How does removal work in a simple array?
Remove the element, shift subsequent elements to the left to close the gap.
Disadvantage of inserting at the beginning of an array
Requires shifting all elements: cost O(n).
When to use a fixed array?
When the size is known and immutable and you want maximum efficiency and simplicity.
When to use an ArrayList?
When you need dynamic growth/shrink and want a rich API of methods.
What is a dynamic array?
An array that automatically resizes (usually doubling capacity) when full.
Growth (resize) strategy
When size == capacity, allocate a larger array (e.g., ×2) and copy elements.
Time complexity of push in a dynamic array
Amortized O(1); occasionally O(n) when resizing occurs.
Time complexity of access and set in a dynamic array
O(1) for index-based get and set.
Time complexity of insert/delete in the middle of a dynamic array
O(n) due to shifting.
Interview best practices (arrays)
Understand O(1) access, shifting costs, amortized resizing, and array vs linked list use cases.
Basic operations on arrays (Java)
get/set by index, insert by shifting, remove by shifting, and bounds checking.
Difference between capacity and size in a dynamic array
Capacity: allocated space. Size: number of stored elements.
Pseudocode: insert at end (no resize)
if size < cap: a[size] = x; size++.
int[] array = new int[5]; // fixed size array
int size = 3; // current number of elements
int element = 10; // element to insert
if (size < array.length) {
array[size] = element; // insert at end
size++; // update size
} else {
System.out.println(“Array is full, cannot insert!”);
}