## All Weeks Data Structures Coursera Quiz Answers

### Data Structures Coursera Quiz Answers

#### Week 1: Basic Data Structures

Q1. Which of the basic data structures is the most suitable if you need to access its elements by their positions in O(1)O(1) time (this is called random access)?

- Stack
- Queue
- List
**Array**

Q2. Which of the basic data structures is the most suitable if you want to be able to insert elements in the middle in O(1)O(1)?

**List**- Queue
- Stack
- Array

Q3. Which of the basic data structures is the most suitable if you only need to insert the elements in the back and to extract elements from the front?

- Array
- Stack
**Queue**

Q4. Which of the basic data structures is the most suitable if you only need to implement recursion in a programming language? When you make a recursive call, you need to save the function you are currently in and its parameters values in some data structure, so that when you go out of the recursion you can restore the state. When you go out of the recursive call, you will always need to extract the last element that was put in the data structure.

**Stack**- Queue

Q5. Which of the basic data structures is the most suitable if you need to store the directory structure on your hard drive?

- Array
**Tree**- Stack
- List
- Queue

#### Week 2: Dynamic Arrays and Amortized Analysis

Q1. Let’s imagine we add support to our dynamic array for a new operation PopBack (which removes the last element), and that PopBack never reallocates the associated dynamically-allocated array. Calling PopBack on an empty dynamic array is an error.

If we have a sequence of 48 operations on an empty dynamic array: 24 PushBack and 24 PopBack (not necessarily in that order), we clearly end with a size of 0.

What are the minimum and maximum possible final capacities given such a sequence of 48 operations on an empty dynamic array? Assume that PushBack doubles the capacity, if necessary, as in lecture.

- minimum: 32, maximum: 32
**minimum: 1, maximum: 32**- minimum: 24, maximum: 24
- minimum: 1, maximum: 24
- minimum: 1, maximum: 1

Q2. Let’s imagine we add support to our dynamic array for a new operation PopBack (which removes the last element). PopBack will reallocate the dynamically-allocated array if the size is \leq≤ the capacity / 2 to a new array of half the capacity. So, for example, if, before a PopBack the size were 5 and the capacity were 8, then after the PopBack, the size would be 4 and the capacity would be 4.

Give an example of n*n* operations starting from an empty array that require O(n^2)*O*(*n*2) copies.

- PushBack n/2
*n*/2 elements, and then PopBack n/2*n*/2 elements. **Let n***n*be a power of 2. Add n/2*n*/2 elements, then alternate n/4*n*/4 times between doing a PushBack of an element and a PopBack.- PushBack 2 elements, and then alternate n/2-1
*n*/2−1 PushBack and PopBack operations.

Q3. Let’s imagine we add support to our dynamic array for a new operation PopBack (which removes the last element). Calling PopBack on an empty dynamic array is an error.

PopBack reallocates the dynamically-allocated array to a new array of half the capacity if the size is \leq≤ the capacity / 4 . So, for example, if, before a PopBack the size were 5 and the capacity were 8, then after the PopBack, the size would be 4 and the capacity would be 8. Only after two more PopBack when the size went down to 2 would the capacity go down to 4.

We want to consider the worst-case sequence of any n*n* PushBack and PopBack operations, starting with an empty dynamic array.

What potential function would work best to show an amortized O(1)*O*(1) cost per operation?

- Φ(
*h*)=*max*(0,2×*size*−*capacity*) - Φ(
*h*)=2×*size*−*capacity* - Φ(
*h*)=2 **Φ(***h*)=*max*(2×*size*−*capacity*,*capacity*/2−*size*)

Q4. Imagine a stack with a new operation: PopMany which takes a parameter, ii, that specifies how many elements to pop from the stack. The cost of this operation is ii, the number of elements that need to be popped.

Without this new operation, the amortized cost of any operation in a sequence of stack operations (Push, Pop, Top) is O(1)*O*(1) since the true cost of each operation is O(1)*O*(1).

What is the amortized cost of any operation in a sequence of n*n* stack operations (starting with an empty stack) that includes PopMany (choose the best answers)?

**O(1) because there wouldn’t be that many PopMany operations.****O(1) because the sum of the costs of all PopMany operations in a total of n operations is O(n).****O(1) because we can define \Phi(h) = sizeΦ(***h*)=*size*.**O(n) because we could push n-1***n*−1 items and then do one big PopMany(*n*−1) that would take O(n) time.**O(1) because we can place one token on each item in the stack when it is pushed. That token will pay for popping it off with a PopMany.**

#### Week 3: Data Structures

#### Quiz 1: Priority Queues

Q1. How many edges of this binary tree violate the min-heap property? In other words, for how many edges of the tree, the parent value is greater than the value of the child?

Answers: 5

Q2. This binary tree contains 13 nodes, and hence we have 13 subtrees here (rooted at each of 13 nodes). How many of the subtrees are complete?

Answers : 11

Q3. Consider a complete binary tree represented by an array [19,14,28,15,16,7,27,15,21,21,5,2][19,14,28,15,16,7,27,15,21,21,5,2].

How many edges of this tree violate the max-heap property? In other words, for how many edges of the tree, the parent value is smaller than the value of the child?

Answers : 5

Q4. Assume that a max-heap with 10^5

elements is stored in a complete 5-ary tree. Approximately how many comparisons a call to Insert() will make?

**8**- 18
- 28
- 38

Q5. Assume that a max-heap with 10^6

elements is stored in a complete 7-ary tree. Approximately how many comparisons a call to ExtractMax() will make?

- 5
- 500
**50**

Q6. Assume that we represent a complete dd-ary tree in an array A[1…n] (this is a 1-based array of size nn). What is the right formula for the indices of children of a node number i?

- {(
*i*−1)*d*+2,…,(*i*−1)*d*+*d*+1} - {
*id*+2,…,min{*n*,*id*+*d*+1}} **{(***i*−1)*d*+2,…,min{*n*,(*i*−1)*d*+*d*+1}}- {(
*i*−1)*d*+1,…,min{*n*,(*i*−1)*d*+*d*}}

#### Quiz 2: Disjoint Sets

Q1. Consider the following program:

```
1.for i from 1 to 12:
2.MakeSet(i)
3.Union(2, 10)
4.Union(7, 5)
5.Union(6, 1)
6.Union(3, 4)
7.Union(5, 11)
8.Union(7, 8)
9.Union(7, 3)
10.Union(12, 2)
```

Assume that the disjoint sets data structure is implemented as an array {\tt smallest}[1 \dots 12]smallest[1…12]: {\tt smallest}[i]smallest[i] is equal to the smallest element in the set containing ii.

What is the output of the following program? As an answer, enter four integers separated by spaces.

Answers : 1331

Q2. Consider the program:

```
1. for i from 1 to 12:
2. MakeSet(i)
3. Union(2, 10)
4. Union(7, 5)
5. Union(6, 1)
6. Union(3, 4)
7. Union(5, 11)
8. Union(7, 8)
9. Union(7, 3)
10.Union(12, 2)
```

Assume that the disjoint sets data structure is implemented as disjoint trees with union by rank heuristic.

Compute the product of the heights of the resulting trees after executing the code. For example, for a forest consisting of four trees of height 1, 2, 3, 1 the answer would be 6. (Recall that the height of a tree is the number of edges on a longest path from the root to a leaf. In particular, the height of a tree consisting of just one node is equal to 0.)

Answers : 2

Q3. Consider the following program:

```
1. for i from 1 to n:
2. MakeSet(i)
3. for i from 1 to n-1:
4. Union(i, i+1)
```

Assume that the disjoint sets data structure is implemented as disjoint trees with union by rank heuristic.

What is the number of trees in the forest and the maximum height of a tree in this forest after executing this code? (Recall that the height of a tree is the number of edges on a longest path from the root to a leaf. In particular, the height of a tree consisting of just one node is equal to 0.)

- n trees, the maximum height is 1.
- n/2 trees, the maximum height is 2.
- One tree of height 1.
**One tree of height log_2**- log_2 n trees, the maximum height is 1.
- Two trees, both of height 1.

Q4. Consider the following program:

```
1. for i from 1 to 60:
2. MakeSet(i)
3. for i from 1 to 30:
4. Union(i, 2*i)
5. for i from 1 to 20:
6. Union(i, 3*i)
7. for i from 1 to 12:
8. Union(i, 5*i)
9. for i from 1 to 60:
10. Find(i)
```

Assume that the disjoint sets data structure is implemented as disjoint trees with union by rank heuristic and with path compression heuristic.

Compute the maximum height of a tree in the resulting forest. (Recall that the height of a tree is the number of edges on a longest path from the root to a leaf. In particular, the height of a tree consisting of just one node is equal to 0.)

Answers : 1

#### Quiz 3: Priority Queues and Disjoint Sets

Q1. You know from the lectures that a heap can be built from an array of nn integers in O(n)O(n) time. Heap is ordered such that each parent node has a key that is bigger than both children’s keys. So it seems like we can sort an array of nn arbitrary integers in O(n)O(n) time by building a heap from it. Is it true?

**No**- Yes

Q2. You’ve organized a party, and your new robot is going to meet and greet the guests. However, you need to program your robot to specify in which order to greet the guests. Of course, guests who came earlier should be greeted before those who came later. If several guests came at the same time or together, however, you want to greet first the older guests to show them your respect. You want to use a min-heap in the program to determine which guest to greet next. What should be the comparison operator of the min-heap in this case?

```
1. def GreetBefore(A, B):
2. if A.arrival_time != B.arrival_time:
3. return A.arrival_time > B.arrival_time
4. return A.age < B.age
```

**1. def GreetBefore(A, B):
2. if A.arrival_time != B.arrival_time:
3. return A.arrival_time > B.arrival_time
4. return A.age > B.age**

```
1. def GreetBefore(A, B):
2. if A.arrival_time != B.arrival_time:
3. return A.arrival_time < B.arrival_time
4. return A.age < B.age
```

```
1. def GreetBefore(A, B):
2. if A.arrival_time != B.arrival_time:
3. return A.arrival_time < B.arrival_time
4. return A.age > B.age
```

Q3. You want to implement a Disjoint Set Union data structure using both path compression and rank heuristics. You also want to store the size of each set to retrieve it in O(1)O(1). To do this, you’ve already created a class to store the nodes of DSU and implemented the FindFind method using the path compression heuristic. You now need to implement the UnionUnion method which will both use rank heuristics and update the size of the set. Which one is the correct implementation?

```
1. def Union(a, b):
2. pa = Find(a)
3. pb = Find(b)
4. if pa.rank <= pb.rank:
5. pa.parent = pb
6. if pa.rank == pb.rank:
7. pb.rank += 1
8. else:
9. pb.parent = pa
```

```
1. def Union(a, b):
2. pa = Find(a)
3. pb = Find(b)
4. if pa.rank <= pb.rank:
5. pa.parent = pb
6. pb.size += pa.size
7. if pa.rank == pb.rank:
8. pb.rank += 1
9. else:
10. pb.parent = pa
```

**1. def Union(a, b):
2. pa = Find(a)
3. pb = Find(b)
4. if pa.rank <= pb.rank:
5. pa.parent = pb
6. pb.size += pa.size
7. else:
8. pb.parent = pa
9. pa.size += pb.size**

#### Week 4: Data Structures

#### Quiz 1: Hash Tables and Hash Functions

Q1. What is the size of the array needed to store integer keys with up to 1212 digits using direct addressing?

- 12
- 10^12
**2^ 12**

Q2. What is the maximum possible chain length for a hash function h(x) = x mod 1000 used with a hash table of size 1000 for a universe of all integers with at most 1212 digits?

**10^12**- 1
- 10^9

Q3. You want to hash integers from 00 up to 1000000. What can be a good choice of pp for the universal family?

- 999997
**1000003**- 1000002

Q4. How can one build a universal family of hash functions for integers between -1000000 (minus one million) and 1000000 (one million)?

**First, add 1000000 to each integer and get the range of integers between 00 and 2000000. Then use the universal family for integers with p = 2000003**- First, add 1000000 to each integer. Then use the universal family for integers with p = 1000003
- Take the universal family for integers with p = 1000003

#### Quiz 2: Hashing

Q1. What is the minimum size of an array that can be used in the direct addressing scheme to store a map from 7-digit phone numbers to names?

**10000000**- 20000000
- 1000000

Q2. If it is guaranteed that the total length of all occurrences of a Pattern*Pattern* in a Text*Text* is at most L*L*, which of the below estimates of the average running time of Rabin-Karp’s algorithm to find all occurrences of the Pattern*Pattern* in the Text*Text* is the most tight out of the correct ones?

*O*(∣*Text*∣+∣*Pattern*∣)*O*(∣*Text*∣+∣*Pattern*∣+*L*)*O*(∣*Text*∣∣*Pattern*∣*L*)*O*(∣*Text*∣∣*Pattern*∣+*L*)

Q3. Let us slightly change the polynomial hash function for strings and set h(S) = (\sum\limits_{j = 0}^{|S|-1} x^{|S|-1-j}S[j]) \bmod p*h*(*S*)=(*j*=0∑∣*S*∣−1*x*∣*S*∣−1−*jS*[*j*])mod*p*. Let us fix some Text*Text* and some Pattern*Pattern*. Denote by H[i]*H*[*i*] the hash function of the substring Text[i..i+|Pattern|-1]*Text*[*i*..*i*+∣*Pattern*∣−1] of the Text*Text* starting from position i*i* and having the same length as Pattern*Pattern* (for all appropriate positions i*i* where the Pattern*Pattern* can occur in the Text*Text*). Which of the below formulas is the correct recurrence to compute H[i + 1]*H*[*i*+1] given H[i]*H*[*i*]?

*H*[*i*+1]=(*xH*[*i*]+*Text*[*i*+∣*Pattern*∣−1]−*x*∣*Pattern*∣*Text*[*i*])mod*p**H*[*i*+1]=(*xH*[*i*]+*x*∣*Pattern*∣*Text*[*i*+∣*Pattern*∣]−*Text*[*i*])mod*p**H*[*i*+1]=(*xH*[*i*]+*Text*[*i*+∣*Pattern*∣]−*x*∣*Pattern*∣*Text*[*i*])mod*p**H*[*i*]=(*xH*[*i*+1]+*Text*[*i*]−*x*∣*Pattern*∣*Text*[*i*+∣*Pattern*∣])mod*p*

#### Week 5: Binary Search Trees

Q1. Your colleague proposed a different definition of a binary search tree: it is such binary tree with keys in the nodes that for each node the key of its left child (if exists) is less than its key, and the key of its right child (if exists) is bigger than its key. Is this a good definition for a binary search tree?

**No**- Yes

Q2. Suppose we enforce the AVL tree condition only for the root of the tree, but not for all the nodes. Can such tree be unbalanced?

**Yes**- No

Q3. Can the Insert operation be implemented given only Split and Merge operations?

**Yes. First create a new tree with single key – the key to be inserted. Then split the current tree by this key. Then merge the left splitted part with the new tree. Then merge the result with the right splitted part.**- Yes. First create a new tree with single key – the key to be inserted. Then merge current tree with the new tree.
- No

Q4. Can the Delete operation be implemented given only Split and Merge operations?

- No
**Yes. Suppose we are deleting key xx. Split by the key twice: one split such that all the keys < x x>x go to the right. Then merge the left part of the first split and the right part of the second split – thus leaving out the node with key xx if there was such a node.**- Yes. Suppose we are deleting key xx. Split by the key twice: one split such that all the keys < x x>x go to the right. Then merge everything back.

#### Week 6: Splay Trees

Q1. What is going to happen if you forget to splay the last accessed vertex in the implementation of FindFind in case the key was not found in the splay tree?

**The tree will work too slow on some sequences of operations.**- Some of the tree operations will work incorrectly after that.

Q2. What will happen if you splay the node with the smallest key in a splay tree?

- The root of the new tree won’t have right child.
- The root of the new tree won’t have children.
- The root of the new tree will have both children.
**The root of the new tree won’t have left child.**

Q3. What will happen if you select a node NN, splay its predecessor PP (the node with the largest key smaller than the key of NN), then splay the node NN itself?

**N will be the root, P will be the left child of the root, P won’t have a right child.**- P will be the root.
- N will be the root, P will be the right child of the root.
- N will be the root, P will be the left child of the root, P won’t have a left child.

#### Get All Course Quiz Answers of **Entrepreneurship Specialization**

Entrepreneurship 1: Developing the Opportunity Quiz Answers

Entrepreneurship 2: Launching your Start-Up Quiz Answers

Entrepreneurship 3: Growth Strategies Coursera Quiz Answers

Entrepreneurship 4: Financing and Profitability Quiz Answers