Blog
Data Structures & Algorithms Past Paper Spring 2025 – BS 4th Semester Punjab University (CC-213)
University of the Punjab BS 4-year programme, fourth semester Spring 2025, Data Structures & Algorithms (CC-213) 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. Solved answers: time complexity, hash-table search, collisions, pre-order and BFS traversal, stable sorting and linked-list length. All code was compiled and run.
Solved answers
Q1 – Short answers (6 × 5)
1. Time complexity of factorialNestedLoop. The inner loop runs i times for each i = 1…n, so the print statement executes 1 + 2 + … + n = n(n + 1)/2 times. Therefore the time complexity is O(n²) (for n = 10 the statement runs 55 times – checked by running the code).
2. Missing condition in the hash-table search. Inside the loop compare each stored value with the key and return true when it matches:
for (int x : table[index]) {
if (x == key)
return true; // key found in this chain
}
return false; // not foundThe table uses separate chaining (each slot is a list<int>), so only the chain at index = key % 10 needs to be searched. Tested: after inserting 15 and 25, search(25) is true and search(35) is false.3. IoT collisions – cause and solution. Root cause: the hash function is too simple (e.g. key % 10 uses only the last digit) and the sensor values follow patterns (same last digits, multiples of 10, similar timestamps), so many keys map to the same few buckets; a small table size and a high load factor make it worse.
Solutions: use a better hash function that mixes all bits/digits (multiplicative hashing, MurmurHash, or include sensor ID + timestamp); use a prime table size and resize/rehash when the load factor exceeds ~0.7; use double hashing or open addressing with good probing; and keep chaining (or balanced trees in buckets) so collisions cost little.
4. Pre-order traversal. Pre-order visits Root → Left → Right:
30, 20, 10, 25, 37, 40, 35, 50
5. BFS from A. Edges read from the figure: A→B, B→C, B→E, C→F, D→A, E→D, F→E. BFS uses a queue:Step Dequeue Enqueue (unvisited neighbours) Queue 1 A B B 2 B C, E C, E 3 C F E, F 4 E D F, D 5 F – (E visited) D 6 D – (A visited) empty
6. BFS vs DFS – data structure. BFS uses a queue (FIFO), so it visits vertices level by level. DFS uses a stack (LIFO) – either an explicit stack or the recursion call stack – so it goes as deep as possible before backtracking.
Q2 Part A – Stable sorting algorithms
A sorting algorithm is stable if records with equal keys stay in the same relative order after sorting.
| Algorithm | Stable? | Why |
|---|---|---|
| Bubble sort | Yes | Swaps only adjacent elements and only when a[j] > a[j+1] (strictly greater), so equal items never pass each other. |
| Selection sort | No (usual version) | It swaps the minimum into place; the swap can jump an element over an equal one. Example: [4a, 4b, 1] → [1, 4b, 4a]. (It can be made stable by shifting instead of swapping.) |
| Insertion sort | Yes | An element is shifted left only while the previous element is strictly greater, so it stops behind an equal element. |
| Quick sort | No | Partitioning swaps elements across long distances around the pivot, so equal keys can change order. |
| Merge sort | Yes | When merging, if the two front elements are equal, the one from the left half is taken first (<=), preserving order. |
Q2 Part B – Length of a linked list
struct Node {
int data;
Node* next;
};
int getLength(Node* head) {
int length = 0;
Node* current = head;
while (current != nullptr) {
length++; // count this node
current = current->next; // move to the next node
}
return length;
}The missing code increments the counter and moves the pointer forward. The loop stops when current becomes nullptr. Time complexity O(n), space O(1). Tested: a 3-node list returns 3 and an empty list (nullptr) returns 0.
Recursive version: int getLength(Node* h) { return h == nullptr ? 0 : 1 + getLength(h->next); }
Upload a photo in 10 seconds — no form to fill. We clean it, solve it and credit you.
