Exam prep Flashcards

(40 cards)

1
Q

What is an algorithm?

A

An algorithm is a detailed, step-by-step method for solving a problem using a computer. It transforms an idea into a structured solution.
ALTERNATIVE
A detailed idea for how a problem can be solved using a computer program.

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

What is a data structure?

A

A data structure is an abstract way to organize data to enable efficient operations. It’s a core tool for writing fast programs and can often exist only in the programmer’s mind.

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

What is the purpose of time complexity?

A

Time complexity measures how long an algorithm takes to run based on the input size N. It focuses on complexity classes using asymptotic notation that ignore constant factors and lower-order terms.

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

What is the 3-SUM problem?

A

Given N unique integers, how many combinations of three numbers add up to zero.

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

What is tilde notation?

A

Asymptotic approximation, used informally to describe rough behavior for large inputs.

‘For large N, this behaves like…’

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

What is theta notation?

A

Tight bounds. Describes the actual/typical growth.

‘It’s exactly this, more or less, every time.’

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

What is big O notation?

A

Big O notation represents an upper bound. It describes the worst-case time complexity.

‘It won’t be slower than this.’

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

What is a heap?

A

A heap is a complete binary tree where in a min-heap, every parent is less than or equal to its children, and in a max-heap, every parent is greater than or equal to its children.

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

What is the time complexity for sorting an array into a binary heap?

A

Building a binary heap from an unsorted array takes O(n) time, where n is the number of elements. This is due to the ‘heapify’ process, which is more efficient than repeatedly inserting elements one by one (which would be O(n 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:

for (int i = 0; i < N; i++) {
System.out.println(i);
}

A

This loop runs O(N) times because it iterates exactly N times, printing each value of i once.

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

What is the time complexity of:

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.println(i + “ “ + j);
}
}

A

This nested loop runs in O(N²) time because the inner loop executes N times for each of the N iterations of the outer loop, resulting in N × N = N² total operations.

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

What is the time complexity of:

for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
System.out.println(i + “ “ + j);
}
}

A

This nested loop runs in O(N²) time because it forms a triangular number pattern that grows quadratically (specifically N(N-1)/2 operations).

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

Time complexity:

for (int i = 1; i < N; i = i * 2) {
System.out.println(i);
}

A

This loop runs in O(log N) time because the counter grows exponentially (doubling each time), resulting in approximately log₂N iterations.

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

Time complexity:

for (int i = N; i > 0; i = i / 2) {
for (int j = 0; j < i; j++) {
System.out.println(i + “ “ + j);
}
}

A

This nested loop runs in O(N) time because the total operations form a geometric series that sums to approximately 2N.

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

Time complexity:

for (int i = 0; i < N; i++) {
for (int j = 1; j < 1000; j = j * 2) {
System.out.println(i + “ “ + j);
}
}

A

The time complexity is O(N) because the outer loop runs N times while the inner loop always runs log₂1000 ≈ 10 times (constant).

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

Time Complexity:

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
System.out.println(i + j + k);
}
}
}

A

This runs in O(N³) time because all three loops are linearly tied to N, multiplying their iterations: N × N × N = N³.

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

Time complexity:

for (int i = N; i > 0; i = i / 2) {
System.out.println(i);
}

A

This loop runs in O(log N) time because the counter decreases exponentially (by dividing by 2 each time).

18
Q

What is the theta complexity of ‘for i = 0 to N’?

19
Q

What is the theta complexity for ‘for i = 0 to N, for j = 0 to N’?

20
Q

What is the theta complexity for ‘for i = 0 to N, for j = 0 to i’?

21
Q

What is the theta complexity for ‘for i = N; i < 0, i /= 2’?

22
Q

What is the theta complexity for ‘for 0 to N³’?

23
Q

What is the theta complexity for a logarithmic loop nested in linear?

A

theta N log N

24
Q

What are the two key properties of a heap?

A

Heaps must be complete binary trees with no gaps filled left-to-right, and satisfy either the min-heap or max-heap property.

25
What are the time complexities for the following heap operations: Insert (push), Delete (pop), Find min/max, Build heap (heapify)?
Inserting or deleting in a heap takes logarithmic time, finding the min or max is constant time, and building a heap from an array takes linear time.
26
How is a binary heap usually stored in memory?
A binary heap is stored as an array where the parent of index i is at (i-1)/2, the left child at 2i+1, and the right child at 2i+2.
27
Name three real-world uses of heaps.
Heaps are used for priority queues in graph algorithms, heap sort, and efficiently finding the k-th largest or smallest element.
28
What's the difference between min-heap and max-heap roots?
In a min-heap, the root is the smallest element, while in a max-heap, the root is the largest element.
29
What does heapify do?
Heapify is the process of converting an unsorted array into a valid heap in linear time.
30
Is this a valid Min-Heap? 3 / \ 5 8 / 10
The tree with root 3, children 5 and 8, and grandchild 10 is a valid min-heap because every parent is less than or equal to its children.
31
What is Dijkstra's algorithm?
Dijkstra's Algorithm finds the shortest path from a start node to all other nodes in a graph with non-negative edge weights.
32
What are the key requirements of Dijkstra's algorithm?
Dijkstra's Algorithm only works on graphs with non-negative edge weights because negative weights can cause incorrect shortest-path calculations.
33
What data structures does Dijkstra's algorithm use?
Dijkstra's uses a priority queue (min-heap) to greedily select the next node with the smallest tentative distance and a distance array to track shortest paths.
34
What does relaxation do?
During relaxation, the algorithm checks if the path to a neighbor through the current node is shorter than the known path, and updates the distance if true.
35
What is a weighted graph?
A weighted graph assigns a numerical value (weight) to each edge, representing cost, distance, or capacity between nodes.
36
What is an unweighted graph?
An unweighted graph has no edge weights; all edges are treated equally, often implying a weight of 1.
37
How many children can each node have in a binary tree?
A maximum of two
38
What is a leaf?
It is a node with no children
39
What is Depth-First-Search (DFS)?
The algorithm starts from a given source and explores all reachable vertices from the given source.
40
What is Breadth-First-Search (BFS)?
It begins with a node, then first traverses all its adjacent nodes. Once all adjacent are visited, then their adjacent are traversed.