Arrays Flashcards

(50 cards)

1
Q

What is an array?

A

A linear data structure that stores elements in a contiguous block of memory.

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

How are elements accessed in an array?

A

By using an index, with indexing typically starting at 0.

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

What is the time complexity of accessing an element by index in an array?

A

O(1), because arrays support constant-time random access.

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

What is the time complexity of inserting an element at the end of an array?

A

O(1) for static arrays if space is available, O(n) for dynamic arrays when resizing is required.

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

What is the time complexity of inserting at the beginning of an array?

A

O(n), since all other elements must be shifted.

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

What is the time complexity of deleting the last element in an array?

A

O(1) for most implementations.

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

What is the time complexity of deleting the first element in an array?

A

O(n), due to the need to shift remaining elements left.

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

What does it mean that arrays have a fixed size?

A

In static arrays, the size is determined at creation and cannot be changed without reallocating memory.

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

What is a dynamic array?

A

An array that can resize itself automatically when more space is needed (e.g., Python lists, Java ArrayList).

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

How does a dynamic array resize?

A

By allocating a larger block of memory (usually double the size) and copying over elements.

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

What is the tradeoff in using dynamic arrays?

A

They offer flexibility in size but may have occasional O(n) operations during resizing.

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

What happens when you access an index outside the bounds of an array?

A

It results in an error or undefined behavior, depending on the language.

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

What is the difference between an array and a linked list?

A

Arrays offer fast random access; linked lists offer faster insertion and deletion at arbitrary positions.

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

What is array traversal?

A

Visiting each element in the array, typically using a loop.

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

What is the time complexity of traversing an array?

A

O(n), where n is the number of elements.

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

How can you reverse an array in place?

A

By swapping elements from the start and end moving toward the middle.

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

What does it mean to rotate an array?

A

Shifting all elements left or right by a certain number of positions.

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

What is the difference between shallow and deep copy in arrays?

A

Shallow copy copies references; deep copy duplicates the actual values.

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

What is a 2D array?

A

An array where each element is itself an array, forming rows and columns.

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

How is a 2D array stored in memory?

A

Typically in row-major order, where rows are stored one after another.

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

What is the time complexity of searching an unsorted array?

A

O(n), since each element may need to be checked.

22
Q

What is the time complexity of searching a sorted array using binary search?

A

O(log n), since the array can be halved repeatedly.

23
Q

What are common algorithms used with arrays?

A

Sorting (merge sort, quicksort), searching (linear, binary), sliding window, prefix sum.

24
Q

What is the sliding window technique?

A

A method for maintaining a subset of elements while iterating, often used for subarrays.

25
What is the prefix sum technique?
Creating an array where each index holds the cumulative sum up to that point, useful for range queries.
26
What are common operations on arrays?
Insert, delete, update, access, search, sort, reverse.
27
What is array resizing?
The process of creating a larger array and copying all elements to it.
28
What is the space complexity of an array?
O(n), where n is the number of elements stored.
29
How do arrays compare to hash tables in access time?
Arrays have O(1) access by index, but not by key; hash tables support O(1) access by key.
30
What is array indexing used for?
Accessing or modifying specific elements using their position in the array.
31
What does it mean to slice an array?
Creating a new array from a subset of the original array’s elements.
32
What is an in-place operation on an array?
An operation that modifies the array without using extra space.
33
What is the difference between append and insert in an array?
Append adds to the end; insert places an element at a specific index, shifting others.
34
What is a sorted array?
An array where elements are arranged in ascending or descending order.
35
Why is sorting important in arrays?
Sorted arrays enable faster searching and are often required in algorithms.
36
What is an example of an array-based queue?
A circular buffer or ring buffer.
37
What is an array-based stack?
A stack implemented using an array with a pointer to the top.
38
How are multi-dimensional arrays represented in memory?
As a single block of memory using calculated offsets.
39
What is an advantage of arrays over linked lists?
Faster element access due to index-based addressing.
40
What is a limitation of arrays?
Fixed size and costly insertions/deletions at arbitrary positions.
41
What are boundary conditions in array algorithms?
Conditions that deal with the first and last elements; common source of bugs.
42
What does O(n²) array operation look like?
Nested loops that access elements, such as comparing all pairs of elements.
43
What are jagged arrays?
Arrays where each row can have a different number of columns.
44
What is memory locality in arrays?
Elements are stored contiguously, which improves cache performance.
45
How is an array different from a pointer in C/C++?
An array is a fixed-size sequence; a pointer can refer to any memory location.
46
What is array flattening?
Converting a multi-dimensional array into a 1D array.
47
What is a sentinel value in an array?
A special value used to mark the end of data or an uninitialized slot.
48
What is an example of an array-based algorithm?
Kadane’s algorithm for finding the maximum subarray sum.
49
What is an out-of-bounds error?
Trying to access an index not within the valid range of the array.
50
When should arrays not be used?
When frequent insertions/deletions at arbitrary positions are required, linked lists are more suitable.