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

Data Structures and Algorithms Past Paper 2018 – BSCS 3rd Term UOS (CMP-3113)

ProgramBSCS
Term3rd
Year2018
Course codeCMP-3113
AnswersSolved ✓

University of Sargodha BS Computer Science 3rd term Data Structure & Algorithms (CMP-3113) paper, 2018 exam (80 marks, 2:30 hours). The paper has been retyped, and solved answers are given below for all 16 short questions and all subjective questions: DFS/BFS traversal, insertion and selection sort pseudocode, hashing with linear probing, BST traversals and singly linked list operations.

Solved answers

Q1 – Short answers (16 × 2)

i. Big Oh notation: O(g(n)) gives an upper bound on the growth of an algorithm’s running time: f(n) = O(g(n)) if f(n) ≤ c·g(n) for all n ≥ n₀, for some constants c > 0 and n₀. It describes the worst case, e.g. linear search is O(n).

ii. Minimum queues for a priority queue: Two queues – one to store the data and one to store the priorities (or one queue per priority level). A single queue cannot keep both the order of arrival and the priority ordering.

iii. Dequeue (deque): A double-ended queue in which elements can be inserted and deleted at both ends (front and rear). Variants: input-restricted and output-restricted deque.

iv. Dynamic data structures: Structures whose size can grow or shrink at run time using dynamic memory allocation, e.g. linked lists, trees, dynamic stacks and queues built with pointers.

v. Why prefix and postfix: They need no parentheses and no precedence rules, so a computer can evaluate them in one left-to-right scan using a stack. Compilers convert infix expressions to postfix for this reason.

vi. AVL tree: A self-balancing binary search tree in which, for every node, the heights of the left and right subtrees differ by at most 1 (balance factor −1, 0 or +1). Rotations (LL, RR, LR, RL) restore balance after insertion or deletion, keeping search O(log n).

vii. Which can form a full binary tree: In a full binary tree every node has 0 or 2 children, so the number of nodes is always odd (n = 2L − 1, where L is the number of leaves). Of 8, 15, 13 and 14, only 15 and 13 (the odd numbers) can form a full binary tree; 8 and 14 cannot. (15 nodes also give a perfect tree of height 3.)

viii. Inserting into a BST: Start at the root; if the new key is smaller go to the left child, otherwise go to the right child; repeat until an empty (NULL) position is reached and attach the new node there.

ix. Linear vs non-linear: In linear structures elements form a sequence and each has one predecessor and one successor (array, stack, queue, linked list). In non-linear structures an element can be connected to many others in a hierarchy or network (tree, graph).

x. File structure vs storage structure: Storage structure is the representation of data in main memory (RAM) while the program runs; file structure is the organisation of data on secondary storage (disk) in files, which persists after the program ends.

xi. Are linked lists linear? Yes. Although nodes are scattered in memory, they are accessed in a single sequence – each node has exactly one next node – so a linked list is a linear data structure.

xii. Data structure for recursion: A stack (the call stack). Each recursive call pushes an activation record (parameters, local variables, return address), which is popped when the call returns.

xiii. Ordered list: A list whose elements are kept in a specific order (e.g. ascending), so every insertion places the element at its correct position, e.g. a sorted linked list.

xiv. Data abstraction: Showing only the essential operations of a data type and hiding its implementation details, e.g. using a stack only through push and pop without knowing whether it uses an array or a linked list.

xv. Applications of data structures: Operating systems (scheduling queues), compilers (stacks, symbol tables), databases (B-trees, hashing), networking (graphs, routing), artificial intelligence, graphics, file systems and search engines.

xvi. Minimum nodes of a binary tree: Zero – an empty binary tree has no nodes. (A non-empty binary tree has at least 1 node; a binary tree of height h has at least h + 1 nodes.)

Q2 – DFS and BFS of the graph

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.

StepQueue (front → rear)Visited
StartAA
Remove A, add B, D, EB, D, EA, B, D, E
Remove B, add CD, E, CA, B, D, E, C
Remove D (A, C, E seen)E, C—
Remove E, remove Cempty—

BFS (uses a queue): A, B, D, E, C. (Checked with a Python program.)

Q3 – Pseudocode of insertion and selection sort

(a) Insertion sort – builds the sorted part one element at a time by inserting each element into its proper place.

INSERTION-SORT(A, n)
1  for i = 1 to n − 1
2      key = A[i]
3      j = i − 1
4      while j ≥ 0 and A[j] > key
5          A[j + 1] = A[j]
6          j = j − 1
7      A[j + 1] = key

(b) Selection sort – repeatedly selects the smallest remaining element and swaps it into position.

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

Both take O(n²) time in the worst case; insertion sort takes O(n) on an already sorted list.

Q4 – Hash table for “S I L V E R M E D A L” (size 13, linear probing)

Key value = position in the alphabet, h(k) = k mod 13. If the slot is taken, try the next slot (wrapping round after 12).

KeyValue kh = k mod 13ProbesStored at
S196–6
I99–9
L1212–12
V2299 taken10
E55–5
R1855, 6 taken7
M130–0
E555, 6, 7 taken8
D44–4
A11–1
L121212, 0, 1 taken2
Index0123456789101112
KeyMAL–DESREIV–L

Final array: M A L _ D E S R E I V _ L (slots 3 and 11 empty). Checked with a Python program.

Q5 – Binary search tree traversals

(a) Inserting 7, 5, 1, 8, 3, 6, 0, 9, 4, 2:

            7
          /   \
         5     8
        / \     \
       1   6     9
      / \
     0   3
        / \
       2   4

An in-order traversal of any BST visits keys in ascending order: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

(b) Inorder d b e a f c g, preorder a b d e c f g:

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   g

Postorder (Left, Right, Root): d, e, b, f, g, c, a.

Q6 – Insert, delete and search in a singly linked list

Algorithms: Insert – create a node; to insert at the start, point it to head and make it the new head; to insert at the end, walk to the last node and link it. Search – start from head and compare each node until the key is found or NULL is reached. Delete – if head holds the key, move head forward; otherwise find the node before the key, link it to the node after the key and free the deleted node.

All C++ code below was compiled with g++ and tested.

struct Node { int data; Node* next; };

void insertAtBeginning(Node*& head, int x) {
    head = new Node{x, head};
}
void insertAtEnd(Node*& head, int x) {
    Node* n = new Node{x, nullptr};
    if (head == nullptr) { head = n; return; }
    Node* p = head;
    while (p->next != nullptr) p = p->next;
    p->next = n;
}
bool search(Node* head, int key) {
    for (Node* p = head; p != nullptr; p = p->next)
        if (p->data == key) return true;
    return false;
}
bool deleteValue(Node*& head, int key) {        // deletes the first node holding key
    if (head == nullptr) return false;
    if (head->data == key) { Node* t = head; head = head->next; delete t; return true; }
    Node* p = head;
    while (p->next != nullptr && p->next->data != key) p = p->next;
    if (p->next == nullptr) return false;        // not found
    Node* t = p->next;
    p->next = t->next;
    delete t;
    return true;
}
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.