0% found this document useful (0 votes)
2 views12 pages

2023-dsa-exam2

The document outlines the structure and instructions for the Data Structure and Algorithms Exam 2 scheduled for December 18, 2023. It consists of three parts: multiple choice questions, open questions, and C code questions, with a total of 90 points available. The exam rules include restrictions on materials allowed, conduct during the exam, and consequences for academic dishonesty.

Uploaded by

Ali Bissenov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

2023-dsa-exam2

The document outlines the structure and instructions for the Data Structure and Algorithms Exam 2 scheduled for December 18, 2023. It consists of three parts: multiple choice questions, open questions, and C code questions, with a total of 90 points available. The exam rules include restrictions on materials allowed, conduct during the exam, and consequences for academic dishonesty.

Uploaded by

Ali Bissenov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structure and Algorithms

900295SCIY

EXAM 2
18 December 2023, 09:00-10:30
Duration of the exam is 90 minutes

This exam was made by Malvin Gattinger


and peer-reviewed by Lorenzo Galeotti.

Student name

On the following page, you will find important information about the
examination. Before starting with the examination you should read
this information.
Instructions

1. This exam consists of three parts: multiple choice questions, open questions,
and C code questions.
2. You are allowed to use a blue or black pen, but nothing else.
3. Please write your name on the front page before you answer the questions.
4. Please do not remove the staple from the exam and keep all sheets connected!
5. Write your answers on this exam, in the empty space after the question.
6. You are not allowed to leave the examination room during the first 15 minutes
of the examination and during the final half hour of the exam.
7. Please switch off any electronic devices and put them in your bag. Your bag
needs to be closed.
8. Any violation of AUC’s rules on fraud may lead to sanctions, ultimately to the
exclusion of all examinations for one year (AS&P appendix 2, Regulations
governing fraud and plagiarism).
9. The number of points per question is specified in the table below.
10. Your exam grade will be (10 + points received) / 10.

Part Question points maximum

I: Multiple Choice ten questions, each 3 points 30

II: Open Questions 1. identify binary trees 9

2. draw tries 10

3. draw heaps 11

III: C Code 1. correct removeMax() 10

2. edgeCount for ListGraph 20

Total 90
Data Structure and Algorithms page 3 of 12 2023 Exam 2

Part I: Multiple Choice Questions


1. Which of the following statements about trees is true?
⃝ A tree with n elements has at most height n − 2.
⃝ A tree with height h has at most h + 2 elements.
⃝ A tree with n elements has at least height n − 1.
⃝ A tree with height h has at least h + 1 elements.
2. Which of the following statements about max-heaps is true?
⃝ A max-heap always has an even number of elements.
⃝ A max-heap with n elements can have a height above n.
⃝ A max-heap with n elements can have a height below n.
⃝ A max-heap cannot have duplicate elements.
3. Suppose a heap has height h and contains n items. Which of the following statements is true?
⃝ The worst-case time complexity of downheap is in O( nh )
⃝ The worst-case time complexity of downheap is in O(log h)
⃝ The worst-case time complexity of downheap is in O(log n)
⃝ The worst-case time complexity of downheap is in O(h − n)
4. Consider the following implementation of addInSearchTree.
1 Tree addInSearchTree(Tree t, int n) {
2 if (t == NULL) {
3 return newTree(n, emptyTree(), emptyTree());
4 }
5 if ( XXX ) {
6 t->leftChild = addInSearchTree(t->leftChild,n);
7 } else if ( YYY ) {
8 t->rightChild = addInSearchTree(t->rightChild,n);
9 }
10 return t;
11 }

Which conditions should replace XXX in line 5 and YYY in line 7 ?


⃝ n > t->item and n < t->item
⃝ n < t->item and n > t->item
⃝ n > t->item and n == t->item
⃝ n == t->item and n < t->item
5. Suppose we have a standard trie of height h. Which of the following is true?
h
⃝ The number of words in the trie is at most 2.
⃝ The number of words in the trie is at most h.
⃝ The time complexity of checking whether a word is in the trie is in O(h).
⃝ The time complexity of adding a word to the trie is in O(log h).
Data Structure and Algorithms page 4 of 12 2023 Exam 2

6. Recall that an Euler path is a path in which every edge occurs exactly once.
Consider the graph below.
C

A B D

E
Which of the following is true?
⃝ This graph has an Euler cycle.
⃝ If we add a second edge between A and B then the graph has an Euler cycle.
⃝ There is an edge such that if we delete it then the graph has an Euler cycle.
⃝ If we add an edge between A and D, then the graph has an Euler cycle.

7. Suppose we need to represent a graph with n nodes and m edges. Which of the following is true?
⃝ The amount of memory needed by the two-dimensional array representation is in O(n).
⃝ The amount of memory needed by the two-dimensional array representation is in O(n2 ).
⃝ The amount of memory needed by the adjacency list representation is in O(n).
⃝ The amount of memory needed by the adjacency list representation is in O(1).

8. Which of the following statements about search algorithms and spanning trees is true?
⃝ The spanning tree obtained with Depth-First Search is always a line.
⃝ The spanning tree obtained with Depth-First Search can contain cycles.
⃝ The spanning tree obtained with Breadth-First Search is never a line.
⃝ The spanning tree obtained with Breadth-First Search can be a line.
9. Let n be the number of nodes and m the number of edges in a graph. Which of the following
statements about the worst-case time-complexity of search algorithms is true?
⃝ The complexity of Depth-First Search is in O(n).
⃝ The complexity of Breadth-First Search is in O(n).
⃝ The complexity of Breadth-First Search is in O(m + n).
⃝ The complexity of Dijkstra’s algorithm is in O(m + n).
10. Suppose we execute Dijkstra’s algorithm on the graph shown below, starting at node 1.

5 2
1 2 3

1 5 1

1 7
4 5 6

Which of the following is true?


⃝ The pseudo-distance of node 2 will be changed at least twice.
⃝ Node 2 will be removed from the priority queue after node 4.
⃝ Node 4 will be removed from the priority queue after node 2.
⃝ The resulting path to node 6 will include 5.
Data Structure and Algorithms page 5 of 12 2023 Exam 2

Part II: Open Questions


1. 9 points Consider the following binary trees:

(a) (b) (c)


1 5 4

3 3 7 2 3

For each of the three trees, say

• whether it is a search tree,


• whether it is a max-heap and
• whether it is a min-heap.
If a tree has the property, just say that it does, without explanation. If a tree does not have the
property, then give an explanation why, referring to specific values.
Data Structure and Algorithms page 6 of 12 2023 Exam 2

2. 10 points Consider the following set of words W :

W = {NORWAY, NOT, ONE, ONLY, POLAND, POLLY, PORTO}

(a) Draw a standard trie for the set W .


(b) Draw a compressed trie for the set W .
Data Structure and Algorithms page 7 of 12 2023 Exam 2

3. 11 points Consider a max-heap represented by an array. Recall that we do not use the 0th entry of
the array and store the root of the heap in array position 1. Suppose the heap contains four elements
and the contents of the array are [0, 6, 2, 5, 2, 0, 0, 0].
(a) Draw the heap as a tree.
(b) Execute enqueue(9), then removeMax(), then enqueue(7) on the heap from (a) and draw
the resulting heap as a tree. (You do not have to provide intermediate steps here — please use
the scratch paper for that.)
Data Structure and Algorithms page 8 of 12 2023 Exam 2

Part III: C Questions


1. 10 points Recall the following definition from our library implementing a max-heap.

typedef struct Heap {


int* array;
int front; // first free position in the array
int size; // size of the array
} Heap;

Consider the following implementation of the function removeMax.


1 int removeMax(Heap* hp) {
2 if (isEmptyHeap(*hp)) {
3 heapEmptyError();
4 }
5 int n = hp->array[1];
6 hp->front++;
7 hp->array[n] = hp->array[hp->front];
8 upheap(hp, 1);
9 return n;
10 }

There are three mistakes in this function. Find them and say how they should be corrected.
Data Structure and Algorithms page 9 of 12 2023 Exam 2

2. 20 points Consider the following typedef to represent a directed unweighted graph. For each node
we use a linked list to store its outgoing edges.
typedef struct ListGraph {
int N; // number of nodes
List* links; // array of adjacency lists
} ListGraph;

0 1
(a) Write C code to represent the graph using this typedef.
You may use all functions from the linked list library.
(b) Write a C function int edgeCount(ListGraph g) that given a graph returns its number
of edges.
Data Structure and Algorithms page 10 of 12 2023 Exam 2

Additional writing space:


Data Structure and Algorithms page 11 of 12 2023 Exam 2

Additional writing space:


Data Structure and Algorithms page 12 of 12 2023 Exam 2

Additional writing space:

You might also like