01 - Linear - Basic - Array Flashcards

(33 cards)

1
Q

What is an array?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the “killer feature” of arrays?

A

Direct random access by index in constant time O(1).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does O(1) mean in array access?

A

It means access time does not grow with the array size; it is constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an array API?

A

An abstraction built on top of the raw array that provides extra functionality, safety, and optimizations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Array vs ArrayList in Java (size)

A

Array: fixed size. ArrayList: automatically resizable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Array vs ArrayList in Java (types)

A

Array: stores primitives and objects. ArrayList: stores only objects (primitives via wrappers).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array vs ArrayList in Java (methods)

A

Array: manual operations. ArrayList: built-in methods such as add, remove, get, set, size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

4 Array characteristics

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is index access O(1)?

A

Because the address of the element is computed as base + index × element_size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Average cost of inserting at the end of a full array

A

Amortized O(1) in a dynamic array; O(n) worst case due to reallocation and copying.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does insertion work in a simple array?

A

Choose a position, shift elements to the right, and write the new value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does removal work in a simple array?

A

Remove the element, shift subsequent elements to the left to close the gap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Disadvantage of inserting at the beginning of an array

A

Requires shifting all elements: cost O(n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When to use a fixed array?

A

When the size is known and immutable and you want maximum efficiency and simplicity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When to use an ArrayList?

A

When you need dynamic growth/shrink and want a rich API of methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a dynamic array?

A

An array that automatically resizes (usually doubling capacity) when full.

17
Q

Growth (resize) strategy

A

When size == capacity, allocate a larger array (e.g., ×2) and copy elements.

18
Q

Time complexity of push in a dynamic array

A

Amortized O(1); occasionally O(n) when resizing occurs.

19
Q

Time complexity of access and set in a dynamic array

A

O(1) for index-based get and set.

20
Q

Time complexity of insert/delete in the middle of a dynamic array

A

O(n) due to shifting.

21
Q

Interview best practices (arrays)

A

Understand O(1) access, shifting costs, amortized resizing, and array vs linked list use cases.

22
Q

Basic operations on arrays (Java)

A

get/set by index, insert by shifting, remove by shifting, and bounds checking.

23
Q

Difference between capacity and size in a dynamic array

A

Capacity: allocated space. Size: number of stored elements.

24
Q

Pseudocode: insert at end (no resize)

A

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!”);
}

25
Pseudocode: insert at end (with resize)
allocate b[2*cap]; copy; a = b; cap *= 2; a[size] = x; size++. public void insertAtEnd(int value) { // if array is full, resize if (size == array.length) { int[] newArray = new int[array.length * 2]; // copy old elements for (int i = 0; i < size; i++) { newArray[i] = array[i]; } array = newArray; } // insert at the end array[size] = value; size++; }
26
Pseudocode: insert at i (0 ≤ i ≤ size)
shift j = size−1…i to j+1; a[i] = x; size++. public void insertAt(int i, int value) { if (i < 0 || i > size) { throw new IndexOutOfBoundsException("Index: " + i); } // assuming capacity > size // shift elements to the right for (int j = size; j > i; j--) { array[j] = array[j - 1]; } array[i] = value; size++; }
27
Pseudocode: remove at i (0 ≤ i < size)
shift j = i+1…size−1 to j−1; size--. public int removeAt(int i) { if (i < 0 || i >= size) { throw new IndexOutOfBoundsException("Index: " + i); } int removedValue = array[i]; // Shift elements left for (int j = i; j < size - 1; j++) { array[j] = array[j + 1]; } size--; // reduce logical size return removedValue; }
28
Trade-off of shrinking capacity automatically
Reduces memory but frequent resizes may degrade performance; use thresholds.
29
How to avoid autoboxing with ArrayList of primitives
Use primitive arrays or specialized libraries; ArrayList requires wrapper types.
30
Why is ArrayList.get(i) O(1)?
Because it uses an internal array and accesses by index directly.
31
Array bounds checking
Always ensure 0 ≤ i < size to avoid exceptions or data corruption.
32
How do you create and initialize an array in Java?
// Create an array with a fixed size int[] numbers = new int[5]; // Initialize values numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; // Or create and initialize in one line int[] numbers2 = { 10, 20, 30, 40, 50 };
33
Give examples of the use of arrays of Objects
´´´java Object[] objects = new Object[10]; String[] strings = new String[]; Person[] persons = new Person[10]; ´´´