Heaps Flashcards

(50 cards)

1
Q

What is a heap?

A

A special tree-based data structure that satisfies the heap property.

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

What is the heap property?

A

In a max-heap, each parent is greater than or equal to its children; in a min-heap, each parent is less than or equal to its children.

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

What is a max-heap?

A

A heap where the largest element is at the root.

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

What is a min-heap?

A

A heap where the smallest element is at the root.

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

What is a binary heap?

A

A binary tree that maintains the heap property and is complete (all levels filled except possibly the last).

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

What is a priority queue?

A

An abstract data type where each element has a priority and elements are served based on priority.

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

How is a priority queue typically implemented?

A

Using a binary heap.

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

What are the main operations of a heap?

A

Insert, extract-min/extract-max, peek-min/peek-max, and heapify.

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

What is the time complexity of inserting into a binary heap?

A

O(log n)

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

What is the time complexity of extracting the root from a binary heap?

A

O(log n)

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

What is the time complexity of peeking at the root of a heap?

A

O(1)

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

What is heapify?

A

The process of rearranging elements to maintain the heap property.

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

What is the time complexity of heapify?

A

O(log n) for a node, O(n) for an entire array.

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

What is build-heap?

A

Converting an arbitrary array into a heap.

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

What is the time complexity of build-heap?

A

O(n)

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

How is a heap represented in an array?

A

The root is at index 0, left child at 2i+1, right child at 2i+2.

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

What is the parent index of a node at index i in a heap array?

A

(i - 1) // 2

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

How do you insert into a heap?

A

Add the element to the end and bubble it up to restore the heap property.

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

How do you remove the root of a heap?

A

Replace it with the last element and bubble it down (heapify).

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

What is the space complexity of a heap with n elements?

A

O(n)

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

What is a complete binary tree?

A

A binary tree where all levels are fully filled except possibly the last, which is filled left to right.

22
Q

What is a d-ary heap?

A

A generalization of a binary heap where each node has d children.

23
Q

What is a Fibonacci heap?

A

A complex heap structure with better amortized performance for some operations.

24
Q

What is the time complexity of decrease-key in a Fibonacci heap?

A

O(1) amortized.

25
What is the benefit of heaps in Dijkstra’s algorithm?
Efficiently retrieves the next node with the lowest tentative distance.
26
What is a common application of heaps?
Priority scheduling, Dijkstra’s algorithm, Huffman encoding.
27
How do you implement a priority queue with changing priorities?
Use a decrease-key or update operation if supported.
28
What is heap sort?
A comparison-based sorting algorithm that uses a heap.
29
What is the time complexity of heap sort?
O(n log n)
30
Is heap sort stable?
No, it is not a stable sort.
31
Is heap sort in-place?
Yes, it uses constant extra space.
32
How do you find the k largest elements in an array?
Use a min-heap of size k.
33
How do you find the k smallest elements in an array?
Use a max-heap of size k.
34
What is a min-priority queue?
A priority queue that serves elements with the smallest priority value first.
35
What is a max-priority queue?
A priority queue that serves elements with the largest priority value first.
36
What is the difference between a queue and a priority queue?
Queue is FIFO; priority queue is based on priority.
37
What happens if you insert elements with equal priority in a heap?
Order is not guaranteed; depends on implementation.
38
Can a heap be used for real-time scheduling?
Yes, to schedule tasks by priority or deadline.
39
What is a k-ary heap?
A heap where each node has up to k children.
40
What is lazy deletion in a heap?
Marking elements as deleted instead of removing them immediately.
41
What is the root of a heap?
The element with the highest priority — either max or min.
42
What is an advantage of using a heap over a sorted list?
Faster insertions and deletions (O(log n) vs O(n)).
43
What is the disadvantage of heaps?
They do not support fast search or ordered traversal.
44
How is a heap used in median-finding algorithms?
Use two heaps: a max-heap for the lower half and a min-heap for the upper half.
45
What is a skew heap?
A self-adjusting heap without structural constraints.
46
What is a leftist heap?
A binary heap variant that favors the shortest path to a leaf on the right.
47
What is the amortized cost of operations in a Fibonacci heap?
Insert and decrease-key are O(1); extract-min is O(log n).
48
What is the best-case time complexity of insert in a binary heap?
O(1), when the element is placed without bubbling up.
49
Can you use a heap for top-k problems?
Yes, by maintaining a heap of size k.
50
What is the difference between a binary search tree and a heap?
A BST supports ordered traversal; a heap supports efficient max/min retrieval.