DSA - Data Structure & Algorithms, Past Papers, UOS Main Campus

Data Structures and Algorithms BSSE BSIT Final Past Paper – UOS Main Campus

ProgramBSSE
ExamFinal Term
CampusUOS Main
AnswersSolved ✓

University of Sargodha main campus (Department of Computer Science and IT) Data Structures and Algorithms final term exam for BSSE (R), BSSE (S), BSIT (SS1) and BSIT (SS2), 60 marks, 2 hours. Retyped from the original paper (the graph has been redrawn), with solved answers below.

Paper set bySir TehseenName as written on the original paper

Solved answers

Q1 – Stack ADT with a linked list (10)

public interface Stack<T> {
    void push(T item);
    T pop();          // remove and return top
    T peek();         // return top without removing
    boolean isEmpty();
    int size();
}

public class LinkedStack<T> implements Stack<T> {
    private class Node { T data; Node next; Node(T d, Node n) { data = d; next = n; } }
    private Node top = null;
    private int count = 0;

    public void push(T item) { top = new Node(item, top); count++; }
    public T pop() {
        if (isEmpty()) throw new RuntimeException("Stack underflow");
        T item = top.data; top = top.next; count--; return item;
    }
    public T peek() {
        if (isEmpty()) throw new RuntimeException("Stack is empty");
        return top.data;
    }
    public boolean isEmpty() { return top == null; }
    public int size() { return count; }
}

All operations are O(1).

Q2 – Queue ADT with an array (10)

public interface Queue<T> {
    void enqueue(T item);
    T dequeue();
    T front();
    boolean isEmpty();
    boolean isFull();
}

public class ArrayQueue<T> implements Queue<T> {
    private T[] data;
    private int front = 0, rear = -1, count = 0;

    @SuppressWarnings("unchecked")
    public ArrayQueue(int capacity) { data = (T[]) new Object[capacity]; }

    public void enqueue(T item) {
        if (isFull()) throw new RuntimeException("Queue overflow");
        rear = (rear + 1) % data.length;      // circular array
        data[rear] = item; count++;
    }
    public T dequeue() {
        if (isEmpty()) throw new RuntimeException("Queue underflow");
        T item = data[front]; data[front] = null;
        front = (front + 1) % data.length; count--;
        return item;
    }
    public T front() { if (isEmpty()) throw new RuntimeException("Empty"); return data[front]; }
    public boolean isEmpty() { return count == 0; }
    public boolean isFull() { return count == data.length; }
}

All operations are O(1) because the array is used circularly.

Q3 – Selection sort (10)

SELECTION-SORT(A, n)
  for i = 0 to n - 2
      min = i
      for j = i + 1 to n - 1
          if A[j] < A[min] then min = j
      swap A[i] and A[min]

The inner loop runs (n − 1) + (n − 2) + … + 1 = n(n − 1)/2 times in every case, so the time complexity is Θ(n²) (best, average and worst), with only O(n) swaps.

Q4 – Insertion sort (10)

void insertionSort(int[] A) {
    for (int i = 1; i < A.length; 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;
    }
}

Worst case (array in reverse order): element i is compared/shifted i times, so the total is 1 + 2 + … + (n − 1) = n(n − 1)/2 → O(n²). (Best case, already sorted: O(n).)

Q5 – Recurrence with an O(n²) merge (10)

T(n) = 2T(n/2) + cn², T(1) = c.

Recursion tree: level 0 costs cn²; level 1 has 2 nodes of size n/2 → 2·c(n/2)² = cn²/2; level 2 → 4·c(n/4)² = cn²/4; level i → cn²/2ⁱ. There are log₂n levels.

Total = cn²(1 + 1/2 + 1/4 + …) ≤ 2cn² (geometric series). So T(n) = Θ(n²) — the top level dominates. (Master theorem case 3: n² grows faster than n^(log₂2) = n.)

Q6 – BFS from S (10)

Adjacency (alphabetical): S: A, C, G · A: B, S · C: D, E, F, S · G: F, H, S · B: A · D: C · E: C, H · F: C, G · H: E, G.

StepDequeueDiscovered (d, parent)Queue after step
0—S (0, NIL)S
1SA (1, S), C (1, S), G (1, S)A, C, G
2AB (2, A)C, G, B
3CD (2, C), E (2, C), F (2, C)G, B, D, E, F
4GH (2, G)B, D, E, F, H
5–9B, D, E, F, H— (all neighbours already discovered)empty

Final values: d[S] = 0; d[A] = d[C] = d[G] = 1 (parent S); d[B] = 2 (parent A); d[D] = d[E] = d[F] = 2 (parent C); d[H] = 2 (parent G).

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

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.