DSA - Data Structure & Algorithms, Past Papers, University of the Punjab

Data Structure and Algorithm Past Paper Spring 2025 – BS 4th Semester Punjab University (IT-207)

Semester4th
Year2025
Course codeIT-207
AnswersSolved ✓

University of the Punjab BS 4-year programme, fourth semester Spring 2025, Data Structure and Algorithm (IT-207) paper (60 marks, 3 hours). The question paper is the official University of the Punjab paper published on pu.edu.pk (BS 4-year programme past papers, Spring 2025); it is retyped here and the answers are written by Shining Study. Worked solutions: step count, Big-O with c and N₀, problem size in one minute, prefix conversion, sparse-matrix triplets, quick sort partition (returns 5), max-heap insert/delete trees and a tested swapEnds() for a singly linked list.

Solved answers

Q1 – Short questions (5 × 6)

1. Step count of the loop (i += 3). The body runs for i = 0, 3, 6, … while i < n, i.e. ⌈n/3⌉ times. Steps: initialisation i = 0 → 1; condition checks → ⌈n/3⌉ + 1; increments i = i + 3 → ⌈n/3⌉; body temp → ⌈n/3⌉. Total = 3⌈n/3⌉ + 2 ≈ n + 2 steps, so the segment is O(n). (Example n = 10: body 4 times, 14 steps.)

2. Big-O of 5N³ + 2N² + 4N + 3. For N ≥ 1: 5N³ + 2N² + 4N + 3 ≤ 5N³ + 2N³ + 4N³ + 3N³ = 14N³. So T(N) ≤ cN³ with c = 14, N₀ = 1, hence T(N) = O(N³). (A tighter pair is c = 6, N₀ = 4, since N³ ≥ 2N² + 4N + 3 from N = 4.)

3. How large a problem in one minute? T(N) = 5N is linear, so time is proportional to N: 2 s ↔ 1000 inputs, so 60 s ↔ 1000 × 60/2 = 30,000 inputs. (From 5·1000·k = 2 s, k = 1/2500 s; 60 = 5N/2500 → N = 30,000.)

4. Prefix of M + N * O / P % Q − R. *, / and % have the same precedence, left to right: ((N * O) / P) % Q. Then + and − left to right: (M + (((N*O)/P)%Q)) − R.
Prefix: − + M % / * N O P Q R.

5. Sparse matrix in 3-column (triplet) form. The 5 × 6 matrix has 7 non-zero values. Row 0 of the array stores (rows, columns, non-zero count); then one row per non-zero element (row, column, value), using 0-based indexes:

RowColumnValue
[0]567
[1]011
[2]134
[3]205
[4]253
[5]327
[6]412
[7]446

Q2 (1) – Quick sort partition, pivot 6

Array: 6 7 9 1 4 2 10 5 3 8 (indexes 0–9). Pivot = 6. i moves right from index 1 to find an element > 6; j moves left from index 9 to find an element ≤ 6; swap while i < j; finally swap the pivot with a[j].

i=1 (7), j=8 (3)  -> swap: 6 3 9 1 4 2 10 5 7 8
i=2 (9), j=7 (5)  -> swap: 6 3 5 1 4 2 10 9 7 8
i=6 (10), j=5 (2) -> i > j, stop
swap pivot a[0] with a[5]:   2 3 5 1 4 6 10 9 7 8

a) The partition returns 5 – the final index of the pivot 6.
b) This index is the pivot’s correct sorted position: all elements on its left (2 3 5 1 4) are ≤ 6 and all on its right (10 9 7 8) are > 6. Quick sort then recursively sorts the two sub-arrays a[0…4] and a[6…9]; the pivot itself is never moved again.

Q2 (2) – Max heap 20 12 9 10 2 6

a) As a complete binary tree (children of i at 2i+1, 2i+2):

          20
        /    \
      12      9
     /  \    /
   10    2  6

b) Insert 15: place at the next position (index 6, right child of 9); 15 > 9 so swap; 15 < 20 so stop. Array: 20 12 15 10 2 6 9

          20
        /    \
      12      15
     /  \    /  \
   10    2  6    9

Delete the root (20): move the last element 9 to the root, then heapify down: children 12 and 15 → swap with 15; 9’s child 6 is smaller, stop. Array: 15 12 9 10 2 6

          15
        /    \
      12      9
     /  \    /
   10    2  6

Q2 (3) – swapEnds()

The function finds the last node and the node before it in one traversal, then relinks pointers (no new nodes, data not changed). Cases: empty and one-node lists are unchanged; two nodes are handled separately because first and last are neighbours.

#include <iostream>
#include <initializer_list>
using namespace std;

class LinkedList;
class Node {
    friend class LinkedList;
private:
    int data;
    Node* next;
};

class LinkedList {
private:
    Node* head;
public:
    LinkedList() : head(nullptr) {}
    void build(initializer_list<int> v) {          // test helper
        Node** t = &head;
        for (int x : v) { *t = new Node; (*t)->data = x; (*t)->next = nullptr; t = &(*t)->next; }
    }
    void print() {
        cout << "(";
        for (Node* p = head; p; p = p->next) cout << p->data << (p->next ? ", " : "");
        cout << ")" << endl;
    }
    void swapEnds();
};

void LinkedList::swapEnds() {
    // 0 or 1 node: nothing to swap
    if (head == nullptr || head->next == nullptr)
        return;

    Node* first = head;
    Node* prev = nullptr;          // node before the last node
    Node* last = head;
    while (last->next != nullptr) {   // one traversal
        prev = last;
        last = last->next;
    }

    if (first->next == last) {     // exactly 2 nodes
        last->next = first;
        first->next = nullptr;
        head = last;
        return;
    }

    // 3 or more nodes
    last->next = first->next;      // last takes first's place
    prev->next = first;            // first goes to the end
    first->next = nullptr;
    head = last;
}

int main() {
    LinkedList a, b, c, d;
    a.swapEnds(); a.print();
    b.build({17}); b.swapEnds(); b.print();
    c.build({1, 2}); c.swapEnds(); c.print();
    d.build({1, 3, 4, 2, 5}); d.swapEnds(); d.print();
}

Output: (), (17), (2, 1), (5, 3, 4, 2, 1) – matching the examples. Time O(n), extra space O(1). (Compiled and tested with g++; build and print are only test helpers.)

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)
Have another year of DSA - Data Structure & Algorithms?

Upload a photo in 10 seconds — no form to fill. We clean it, solve it and credit you.

Missing years for this subject
202620242023

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.