DSA - Data Structure & Algorithms, Past Papers

Data Structure & Algorithm MSc IT 3rd Term Past Paper 2019 UOS

Data Structure and Algorithm MSc IT 3rd term past paper 2019 University of Sargodha

University of Sargodha M.Sc. IT 3rd term Data Structure & Algorithm (CMP-3112) past paper 2019, retyped from the original paper, with solved answers below.

Solved answers

Q1 – Short questions

(i) Node of a doubly linked list: it has three parts — a pointer to the previous node, the data, and a pointer to the next node: struct Node { Node *prev; int data; Node *next; };

(ii) Non-linear data structures: trees and graphs.

(iii) A stack is used in recursion, because each call’s local variables and return address must be saved and the most recent call must finish first (last-in, first-out).

(iv) Queue operations: enqueue (insert at rear) and dequeue (delete from front).

(v) A + B * C → ABC*+ (multiplication has higher precedence).

(vi) Collision resolution: open addressing (linear probing, quadratic probing, double hashing) and separate chaining.

(vii) Two conditions of recursion: there must be a base (terminating) case, and each recursive call must move closer to the base case.

(viii) Recursive sorting algorithms: merge sort and quick sort.

(ix) Strictly binary tree: a binary tree in which every non-leaf node has exactly two children (non-empty left and right subtrees).

(x) A strictly binary tree with n leaves has 2n − 1 nodes, so with 9 leaves it has 2(9) − 1 = 17 nodes.

(xi) Height of a tree: the number of edges on the longest path from the root to a leaf (some books count levels). Example: a root with one child that has one child has height 2.

(xii) Merging: combining two sorted lists into one sorted list by repeatedly comparing their first elements and taking the smaller one.

(xiii) Bubble sort performs more swaps — it can swap on every comparison, O(n²) swaps, while selection sort does at most n − 1 swaps.

(xiv) Quick sort selects a pivot element and partitions the array around it.

(xv) Binary search tree: for every node, all keys in its left subtree are smaller and all keys in its right subtree are larger, so searching, insertion and deletion take O(h) time and in-order traversal gives sorted order.

(xvi) Two references — front and rear.

Q2 – Linked list and circular queue

struct Node {
    int data;
    Node *next;
};
Node *head = NULL;

// (a) add a node at the start (works also when the list is empty)
void addAtStart(int value) {
    Node *n = new Node;
    n->data = value;
    n->next = head;      // NULL if the list was empty
    head = n;            // new node becomes the first node
}

// (b) delete (dequeue) from a circular queue implemented with an array
const int SIZE = 10;
int q[SIZE];
int front = -1, rear = -1;

int deleteQ() {
    if (front == -1) {                 // queue empty
        cout << "Queue underflow";
        return -1;
    }
    int item = q[front];
    if (front == rear)                 // only one element
        front = rear = -1;
    else
        front = (front + 1) % SIZE;    // wrap around
    return item;
}

Q3 – Infix to postfix: ((A + B) / (C + D) $ (E / F)) + (G + H) / K

Rules: operands go directly to the output; “(” is pushed; on “)” pop until “(”; an operator pops operators of higher (or equal, for left-associative) precedence first. Precedence: $ (exponent, right-associative) > *, / > +, −.

SymbolStackPostfix output
((
(((
A((A
+((+A
B((+AB
)(AB+
/(/AB+
((/(AB+
C(/(AB+C
+(/(+AB+C
D(/(+AB+CD
)(/AB+CD+
$(/$AB+CD+
((/$(AB+CD+
E(/$(AB+CD+E
/(/$(/AB+CD+E
F(/$(/AB+CD+EF
)(/$AB+CD+EF/
)emptyAB+CD+EF/$/
++AB+CD+EF/$/
(+(AB+CD+EF/$/
G+(AB+CD+EF/$/G
++(+AB+CD+EF/$/G
H+(+AB+CD+EF/$/GH
)+AB+CD+EF/$/GH+
/+/AB+CD+EF/$/GH+
K+/AB+CD+EF/$/GH+K
endemptyAB+CD+EF/$/GH+K/+

Postfix: AB+CD+EF/$/GH+K/+

Q4 – BST for 45, 32, 90, 34, 68, 72, 15, 24, 30, 66, 11, 50, 10

45
              /      \
            32        90
           /  \      /
         15    34   68
        /  \       /  \
      11    24    66   72
     /        \   /
   10          30 50
  • Pre-order (root, left, right): 45, 32, 15, 11, 10, 24, 30, 34, 90, 68, 66, 50, 72
  • In-order (left, root, right): 10, 11, 15, 24, 30, 32, 34, 45, 50, 66, 68, 72, 90
  • Post-order (left, right, root): 10, 11, 30, 24, 15, 34, 32, 50, 66, 72, 68, 90, 45

Q5 – Insertion sort and recursive sum

void insertionSort(int a[], int n) {
    for (int i = 1; i < n; i++) {
        int key = a[i];
        int j = i - 1;
        while (j >= 0 && a[j] > key) {   // shift larger elements right
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;
    }
}

// (b) recursive sum of 1..n; sum(10) returns 55
int sum(int n) {
    if (n == 1) return 1;      // base case
    return n + sum(n - 1);     // recursive case
}
// call: cout << sum(10);   // 55

Q6 – Delete from sorted array; count odd nodes

// (a) delete an item from a sorted array; returns the new size
int deleteItem(int a[], int n, int item) {
    int low = 0, high = n - 1, pos = -1;
    while (low <= high) {                 // binary search
        int mid = (low + high) / 2;
        if (a[mid] == item) { pos = mid; break; }
        else if (a[mid] < item) low = mid + 1;
        else high = mid - 1;
    }
    if (pos == -1) { cout << "Item not found"; return n; }
    for (int i = pos; i < n - 1; i++)     // shift left
        a[i] = a[i + 1];
    return n - 1;
}

// (b) count nodes having odd values in a linked list
int countOdd(Node *head) {
    int count = 0;
    for (Node *p = head; p != NULL; p = p->next)
        if (p->data % 2 != 0) count++;
    return count;
}

Q7 – Linear probing with h(k) = key % 9 (10 slots, 0–9)

Keyh(k) = key % 9Collision / probesFinal slot
257—7
33—3
516—6
371—1
303slot 3 full → 44
797slot 7 full → 88
55—5
2355, 6, 7, 8 full → 99
Slot0123456789
Key—37—330551257923

All 8 keys are accommodated; slots 0 and 2 remain empty. Key 23 needed 4 probes because of clustering, a known drawback of linear probing.

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.