Blog
Data Structures and Algorithms Past Paper 2018 – BSSE 3rd Term UOS (CMP-3112)
University of Sargodha BS Software Engineering 3rd term Data Structure & Algorithms (CMP-3112) paper, 2018 exam (80 marks, 2:30 hours). The paper has been retyped without the handwriting on the original, and solved answers are given below for all 16 short questions and all subjective questions: DFS/BFS, insertion and merge sort, BST traversals, recursive binary search, infix to postfix with a stack, hashing with chaining and adjacency lists.
Solved answers
Q1 – Short answers (16 × 2)
I. Two non-linear data structures: Tree and graph.
II. References for a queue: Two – front (for deletion) and rear (for insertion).
III. Hash table: A data structure that stores key–value pairs in an array, using a hash function to compute the index of each key. It is used for very fast insertion, search and deletion – O(1) on average – e.g. symbol tables, caches, dictionaries.
IV. Dynamic data structures: Structures whose size changes at run time using dynamic memory, e.g. linked lists, trees and linked stacks/queues.
V. Why prefix and postfix: They need no parentheses or precedence rules, so expressions can be evaluated in one scan with a stack; compilers convert infix to postfix for evaluation.
VI. Sorting techniques using recursion: Merge sort and quick sort.
VII. Complete binary tree: All levels are completely filled except possibly the last, which is filled from left to right.
VIII. Inserting into a BST: Compare the key with the root; go left if smaller, right if larger; repeat until a NULL position is found and attach the new node there.
IX. Two parameters defining a graph: A set of vertices V and a set of edges E: G = (V, E).
X. Nodes in a full binary tree with n leaves: Every internal node has two children, so internal nodes = n − 1 and total nodes = 2n − 1.
XI. Structures for graph traversal: DFS uses a stack (or recursion); BFS uses a queue.
XII. Structure for recursion: A stack (the call stack of activation records).
XIII. Why Dijkstra’s algorithm: To find the shortest paths from one source vertex to all other vertices in a weighted graph with non-negative edge weights, e.g. in routing and maps.
XIV. Data abstraction: Using a data type through its operations while hiding implementation details, e.g. a queue used only through enqueue and dequeue.
XV. Applications of data structures: Operating systems, compilers, databases (indexes, hashing), networks and routing, AI, graphics and file systems.
XVI. Nodes in a complete binary tree of level 5: If levels are numbered 1 to 5 (root at level 1), a full complete tree has at most 25 − 1 = 31 nodes, with 16 nodes on level 5; at least 16 nodes (24) are needed to reach level 5. (If the root is level 0, the maximum is 26 − 1 = 63.)
Q2 – DFS and BFS starting from A
Adjacency (from the arrows): A → B, D, E; B → C, D; C → none; D → A, C, E; E → none. Neighbours are visited in alphabetical order, starting from A.
DFS (uses a stack / recursion): visit A → go to B → go to C (dead end, back to B) → go to D (A already visited, C visited) → go to E. DFS order: A, B, C, D, E.
| Step | Queue (front → rear) | Visited |
|---|---|---|
| Start | A | A |
| Remove A, add B, D, E | B, D, E | A, B, D, E |
| Remove B, add C | D, E, C | A, B, D, E, C |
| Remove D (A, C, E seen) | E, C | — |
| Remove E, remove C | empty | — |
BFS (uses a queue): A, B, D, E, C. (Checked with a Python program.)
Q3 – Sorting functions (array passed as parameter)
All C++ code below was compiled with g++ and tested.
(a) Insertion sort
void insertionSort(int a[], int n) {
for (int i = 1; i < n; i++) {
int key = a[i], j = i - 1;
while (j >= 0 && a[j] > key) { // shift larger elements right
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}(b) Merge sort – call mergeSort(a, 0, n - 1):
void merge(int a[], int l, int m, int r) {
vector<int> L(a + l, a + m + 1), R(a + m + 1, a + r + 1);
int i = 0, j = 0, k = l;
while (i < (int)L.size() && j < (int)R.size())
a[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < (int)L.size()) a[k++] = L[i++];
while (j < (int)R.size()) a[k++] = R[j++];
}
void mergeSort(int a[], int l, int r) {
if (l >= r) return; // one element: already sorted
int m = (l + r) / 2;
mergeSort(a, l, m); // sort left half
mergeSort(a, m + 1, r); // sort right half
merge(a, l, m, r); // merge the two sorted halves
}Q4 – Binary search tree and traversals
(a) Inserting 14, 5, 7, 1, 6, 10, 11, 17 from left to right:
14
/ \
5 17
/ \
1 7
/ \
6 10
\
11| Traversal | Order |
|---|---|
| Pre-order (Root, Left, Right) | 14, 5, 1, 7, 6, 10, 11, 17 |
| In-order (Left, Root, Right) | 1, 5, 6, 7, 10, 11, 14, 17 |
| Post-order (Left, Right, Root) | 1, 6, 11, 10, 7, 5, 17, 14 |
(Checked with a Python program.)
(b)
Preorder gives the root first: a is the root. In the inorder d b e a f c g, the left subtree is {d, b, e} and the right subtree is {f, c, g}. Next in preorder is b (root of the left subtree, with d on its left and e on its right) and then c (root of the right subtree, with f left and g right).
a
/ \
b c
/ \ / \
d e f gPostorder (Left, Right, Root): d, e, b, f, g, c, a.
Q5 – Recursive binary search and counting nodes
All C++ code below was compiled with g++ and tested.
// (a) returns the index of key in sorted a[low..high], or -1
int binarySearch(int a[], int low, int high, int key) {
if (low > high) return -1; // not found
int mid = (low + high) / 2;
if (a[mid] == key) return mid;
if (key < a[mid]) return binarySearch(a, low, mid - 1, key);
return binarySearch(a, mid + 1, high, key);
}
// (b) number of nodes in a singly linked list
struct Node { int data; Node* next; };
int countNodes(Node* head) {
int count = 0;
for (Node* p = head; p != nullptr; p = p->next) count++;
return count;
}Q6 – Infix to postfix and recursive array sum
(a) A + (((B − C) * (D − E) + F) / G) $ (H − J), where $ is exponentiation (highest precedence, right-associative):
| Symbol | Stack | Postfix output |
|---|---|---|
| A | A | |
| + | + | A |
| ( ( ( | + ( ( ( | A |
| B | + ( ( ( | A B |
| − | + ( ( ( − | A B |
| C | + ( ( ( − | A B C |
| ) | + ( ( | A B C − |
| * | + ( ( * | A B C − |
| ( | + ( ( * ( | A B C − |
| D − E ) | + ( ( * | A B C − D E − |
| + | + ( ( + | A B C − D E − * |
| F | + ( ( + | A B C − D E − * F |
| ) | + ( | A B C − D E − * F + |
| / | + ( / | A B C − D E − * F + |
| G | + ( / | A B C − D E − * F + G |
| ) | + | A B C − D E − * F + G / |
| $ | + $ | A B C − D E − * F + G / |
| ( H − J ) | + $ | A B C − D E − * F + G / H J − |
| end | A B C − D E − * F + G / H J − $ + |
Postfix: A B C − D E − * F + G / H J − $ + (checked with a Python stack program).
(b) Recursive sum of all elements of an array:
All C++ code below was compiled with g++ and tested.
int sumArray(int a[], int n) {
if (n == 0) return 0; // empty array
return a[n - 1] + sumArray(a, n - 1); // last element + sum of the rest
}Q7 – Hashing with chaining and drawing a graph
(a) h(x) = x mod 5: 35→0, 2→2, 18→3, 6→1, 3→3, 10→0, 8→3, 5→0. New keys are added at the end of each chain:
slot 0: 35 -> 10 -> 5
slot 1: 6
slot 2: 2
slot 3: 18 -> 3 -> 8
slot 4: (empty)(b) From the adjacency lists: A → B, A → C, A → D; B → A; C → B, C → D; D has no outgoing edges.
| Vertex | Arrows drawn from it |
|---|---|
| A | A → B, A → C, A → D |
| B | B → A |
| C | C → B, C → D |
| D | none |
So the directed graph has 6 edges: (A,B), (A,C), (A,D), (B,A), (C,B), (C,D). D has out-degree 0 and in-degree 2.
