Varibale size data structures Flashcards

(19 cards)

1
Q

Why aren’t fixed-size arrays always sufficient?

A

Because we often don’t know in advance how much data we’ll need, and predicting the required size is generally undecidable.

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

What is an Abstract Data Type (ADT)?

A

A specification of data operations (like add, get, isEmpty) independent of implementation.

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

What makes a stack different from a queue?

A

A stack uses LIFO (last-in, first-out); a queue uses FIFO (first-in, first-out).

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

What is the main advantage of linked lists over arrays?

A

They can grow dynamically and use non-contiguous memory, making them efficient in fragmented memory environments.

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

What does each node in a singly linked list contain?

A

A data item and a reference to the next node.

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

Why are null pointers dangerous in linked lists?

A

They can cause NullPointerExceptions if dereferenced; forgetting to check for null is a common bug.

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

What does loitering mean in memory management?

A

Holding a reference to an unused object, preventing garbage collection and wasting memory.

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

How do you prevent loitering in array-based stacks?

A

Explicitly set the array slot to null after removing an item.

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

What is the advantage of using a resizable array over a fixed-size one?

A

It allows the data structure to grow as needed, handling dynamic data sizes.

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

What is the amortized cost of resizing an array by doubling its size?

A

O(1) — the cost of resizing is spread across many insertions.

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

Why is shrinking arrays tricky?

A

Frequent shrinking can lead to performance degradation; it should be done cautiously, e.g., when the array is <25% full.

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

How does a circular buffer help implement a queue?

A

It allows reuse of array space using modular arithmetic, avoiding the need for shifting elements.

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

What is the typical complexity of linked list operations?

A

O(1) for add, get, and isEmpty (at head); O(n) for random access.

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

What are the benefits of linked lists in memory-constrained systems?

A

They allocate nodes individually and can better utilize fragmented memory.

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

What is the amortized time complexity of adding to a resizable array?

A

O(1) — due to occasional resizing.

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

Why can’t we generally decide the maximum size a program needs?

A

Because determining program behavior like loop bounds or halting conditions is undecidable.

17
Q

What happens if you don’t null out references in arrays after removal?

A

Objects may linger in memory due to loitering, causing memory leaks.

18
Q

What is the main downside of linked lists?

A

Slower random access compared to arrays — must traverse linearly.

19
Q

Why are null pointer bugs common in Java?

A

Java allows uninitialized references; dereferencing them causes runtime crashes if not checked.