Exam II Flashcards

1
Q

What is the time complexity of binary search?

A

O(log n) — it repeatedly halves the search space.

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

Why must data be sorted for binary search?

A

Binary search relies on comparing the target to the middle item and eliminating half the search space; this only works with sorted data.

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

What is the main idea of selection sort?

A

Repeatedly find the smallest element and swap it with the current index.

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

Time complexity of selection sort?

A

O(n²) — regardless of input order.

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

What is the best-case time complexity of insertion sort?

A

O(n) — when the array is already sorted.

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

Why is insertion sort efficient on nearly sorted arrays?

A

Fewer shifts are needed since elements are already close to their final positions.

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

What makes shell sort faster than insertion sort?

A

It sorts distant elements first, reducing the number of final swaps needed in later passes.

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

Worst-case time complexity of shell sort (with good gaps)?

A

O(n^1.5)

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

Merge sort time and space complexity?

A

Time: O(n log n), Space: O(n)

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

what type of algorithm is merge sort?

A

divide and conquer

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

Best and average case complexity of quicksort?

A

O(n log n)

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

what makes quicksort fast in practice?

A

in-place sorting and efficient partitioning

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

What is radix sort’s time complexity?

A

O(n * d) — d = number of digits.

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

when is radix sort most efficient?

A

When the number of digits (d) is small compared to n.

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

how does a queue differ from a stack?

A

Queue = FIFO, Stack = LIFO.

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

What’s the benefit of using a circular array for a queue?

A

Avoids unnecessary resizing by reusing space.

17
Q

what does a deque support that a queue does not?

A

Insertion and removal from both front and back.

18
Q

How are priority queues different from regular queues?

A

Items are dequeued based on priority, not insertion order.

19
Q

Which implementation can insert in O(log n)?

A

Array-based with binary search + shift.

20
Q

What is the time complexity of brute-force string matching?

21
Q

What does Boyer-Moore improve?

A

Skips ahead using bad character and good suffix rules → O(n/m) avg.

22
Q

What is a rolling hash useful for?

A

Efficient substring hash updates (used in Rabin-Karp).

23
Q

What is the key advantage of hashing?

A

Near constant-time lookups (O(1)).

24
Q

What’s a collision in a hash table?

A

Two keys hash to the same index.

25
Name a collision resolution technique.
Separate chaining, linear probing, quadratic probing, double hashing.
26
What does Iterator provide?
hasNext(), next(), remove() — for sequential access.
27
What does ListIterator add?
Previous(), add(), set(), index tracking.
28