Blog
Data Structure & Algorithms Past Paper 2015 – MScIT – UOS
Data Structure & Algorithms Past Paper 2015 – MSc IT (text version)
University of Sargodha – M.Sc. I.T. 3rd Term Exam 2015, Information Technology
Paper: Data Structure & Algorithms (CMP-3112) · Time: 2:30 hours · Maximum marks: 80
Objective part (compulsory, 16 × 2)
- Define data structure.
- What are the eight primitive types in Java?
- Differentiate between reference type and primitive type.
- List five operations that can be applied to a reference type.
- List two advantages of linked list over array.
- What is garbage collection?
- If a class provides no constructor, what is the result?
- What is collection API?
- To solve a problem two algorithms are required to run; the first requires O(N²) and the second O(N). What is the total cost of solving the problem?
- Group the following into equivalent Big-Oh functions: x², x, x² + x, x² − x, and x³/(x − 1).
- Show the results of the sequence add(4), add(8), add(1), add(6), remove(), remove() when add and remove correspond to the basic operations of (a) a stack (b) a queue.
- List the four fundamental rules of recursion.
- What does a hash function do?
- Differentiate between best case and worst case analysis.
- Differentiate between queue and priority queue.
- List two shortcomings of Dijkstra’s algorithm.
Subjective part (attempt four out of six, 4 × 12)
- Q.2 Give an interface and implementation of queue.
- Q.3 Show a dry run of merge sort on the sequence 6, 7, 2, 4, 3, 2, 8, 9, 1, 5, 10.
- Q.4 Write an algorithm (or program) of insertion sort and perform its analysis.
- Q.5 Describe a recursive routine using an appropriate example.
- Q.6 Define binary tree, complete binary tree and full binary tree. Also, what is the maximal number of nodes on a given level of a binary tree?
- Q.7 Show a dry run of Dijkstra’s algorithm on the given graph (vertices V0–V6; edges V0→V1 (2), V0→V3 (1), V1→V3 (3), V1→V4 (10), V2→V0 (4), V2→V5 (5), V3→V2 (2), V3→V4 (2), V3→V5 (8), V3→V6 (4), V4→V6 (6), V6→V5 (1)).
Solved answers – DSA MSc IT 2015
Short questions
- A data structure is a way of organizing and storing data so it can be accessed and modified efficiently, together with the operations on it – e.g. arrays, linked lists, stacks, queues, trees, graphs, hash tables.
- byte, short, int, long, float, double, char, boolean.
- A primitive variable stores its value directly (int x = 5) and is copied by value; a reference variable stores the address of an object on the heap (String s, arrays, class objects), can be null, and assignment copies the reference, not the object.
- Assignment (=), equality comparison (== / !=), the dot operator to access fields and methods, the cast operator, instanceof, and passing as a parameter.
- Linked lists grow and shrink dynamically (no fixed size or resizing), and insertion/deletion at a known position takes O(1) without shifting elements.
- Garbage collection is Java’s automatic reclamation of memory occupied by objects that are no longer referenced, so programmers do not free memory manually.
- Java provides a default no-argument constructor that initializes fields to default values (0, false, null). If any constructor is written, the default is not provided.
- The Collections API is Java’s library of data-structure interfaces and classes (java.util): Collection, List, Set, Queue, Map with implementations such as ArrayList, LinkedList, HashSet, TreeMap and PriorityQueue, plus algorithms in the Collections class.
- O(N²) + O(N) = O(N²) – the larger term dominates.
- x², x² + x, x² − x and x³/(x − 1) are all O(x²); x alone is O(x).
- (a) Stack: push 4, 8, 1, 6; pop → 6, pop → 1; left 4, 8 (8 on top). (b) Queue: enqueue 4, 8, 1, 6; dequeue → 4, dequeue → 8; left 1, 6 (1 at front).
- (1) Base cases solved without recursion; (2) make progress toward a base case; (3) design rule – assume the recursive calls work; (4) compound interest rule – never duplicate work by solving the same instance in separate calls.
- A hash function maps a key to an index (table position) in a hash table, ideally spreading keys evenly, so search, insert and delete take O(1) on average.
- Best case is the minimum running time over all inputs of size N (e.g. insertion sort on sorted data, O(N)); worst case is the maximum (reverse-sorted data, O(N²)) and gives a guaranteed bound.
- A queue is FIFO – the earliest inserted item leaves first. A priority queue removes the item with the highest priority (e.g. the minimum) regardless of insertion order; usually implemented with a binary heap.
- It does not work correctly with negative edge weights, and it computes paths from a single source only and explores in all directions, which is costly on large graphs (O(E log V) with a heap).
Subjective questions
Q.2 Queue interface and implementation (circular array; Java, compiled and tested)
interface Queue<T> {
void enqueue(T x); // insert at the back
T dequeue(); // remove and return the front
T getFront(); // return the front without removing
boolean isEmpty();
void makeEmpty();
}
public class ArrayQueue<T> implements Queue<T> {
private T[] items;
private int front, back, size;
@SuppressWarnings("unchecked")
public ArrayQueue() {
items = (T[]) new Object[10];
makeEmpty();
}
public boolean isEmpty() { return size == 0; }
public void makeEmpty() { size = 0; front = 0; back = -1; }
public void enqueue(T x) {
if (size == items.length) grow();
back = (back + 1) % items.length; // circular increment
items[back] = x;
size++;
}
public T dequeue() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
T x = items[front];
items[front] = null;
front = (front + 1) % items.length;
size--;
return x;
}
public T getFront() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
return items[front];
}
@SuppressWarnings("unchecked")
private void grow() { // double the array
T[] bigger = (T[]) new Object[items.length * 2];
for (int i = 0; i < size; i++)
bigger[i] = items[(front + i) % items.length];
items = bigger;
front = 0;
back = size - 1;
}
}Enqueue and dequeue are O(1) (amortized O(1) when the array doubles). Enqueuing 1…12 and dequeuing twice gives 1, 2 and a front of 3.
Q.3 Merge sort dry run
[6 7 2 4 3 2 8 9 1 5 10]
split -> [6 7 2 4 3 2] [8 9 1 5 10]
split -> [6 7 2] [4 3 2] [8 9 1] [5 10]
split -> [6 7][2] [4 3][2] [8 9][1] [5][10]
split -> [6][7] [4][3] [8][9]
merge: [6][7] -> [6 7] [4][3] -> [3 4]
[6 7]+[2] -> [2 6 7] [3 4]+[2] -> [2 3 4]
[2 6 7]+[2 3 4] -> [2 2 3 4 6 7]
[8][9] -> [8 9] [5][10] -> [5 10]
[8 9]+[1] -> [1 8 9]
[1 8 9]+[5 10] -> [1 5 8 9 10]
final: [2 2 3 4 6 7] + [1 5 8 9 10]
-> [1 2 2 3 4 5 6 7 8 9 10]Merge sort splits the list in halves until single elements remain, then merges sorted halves; time O(N log N).
Q.4 Insertion sort
public static void insertionSort(int[] a) {
for (int p = 1; p < a.length; p++) {
int tmp = a[p];
int j = p;
while (j > 0 && tmp < a[j - 1]) { // shift larger items right
a[j] = a[j - 1];
j--;
}
a[j] = tmp;
}
}
// {34, 8, 64, 51, 32, 21} -> [8, 21, 32, 34, 51, 64]Analysis: for position p the inner loop runs at most p times, so the worst case (reverse order) is 1 + 2 + … + (N − 1) = N(N − 1)/2 = O(N²). Best case (already sorted): the inner test fails immediately, O(N). Average case: about N²/4 comparisons, O(N²). It sorts in place (O(1) extra space) and is stable.
Q.5 Recursive routine: a routine that calls itself on a smaller instance of the same problem until it reaches a base case.
public static long factorial(int n) {
if (n <= 1) // base case
return 1;
return n * factorial(n - 1); // progress toward the base case
}
// factorial(5) = 5*4*3*2*1 = 120factorial(5) → 5 × factorial(4) → … → factorial(1) = 1 (base case), and the results multiply back up to 120. Each call reduces n (progress), satisfying the rules of recursion; time O(n), stack space O(n).
Q.6 A binary tree is a tree in which every node has at most two children (left and right). A full binary tree is one in which every node has either 0 or 2 children. A complete binary tree has every level completely filled except possibly the last, which is filled from left to right (the shape used by binary heaps). The maximum number of nodes on level i (root at level 0) is 2i; a tree of height h has at most 2h+1 − 1 nodes.
Q.7 Dijkstra’s algorithm from V0 (* = vertex known/finalized)
Start V0. d = known distance, (p) = previous vertex
Step Select V0 V1 V2 V3 V4 V5 V6
0 - 0 inf inf inf inf inf inf
1 V0 0* 2(V0) inf 1(V0) inf inf inf
2 V3 0* 2 3(V3) 1* 3(V3) 9(V3) 5(V3)
3 V1 0* 2* 3 1* 3 9 5 (V1->V4: 12 > 3, no change)
4 V2 0* 2* 3* 1* 3 8(V2) 5 (V2->V0 ignored, known)
5 V4 0* 2* 3* 1* 3* 8 5 (V4->V6: 9 > 5, no change)
6 V6 0* 2* 3* 1* 3* 6(V6) 5*
7 V5 0* 2* 3* 1* 3* 6* 5*
Final: V0=0, V1=2, V2=3, V3=1, V4=3, V5=6, V6=5
Paths: V5 = V0->V3->V6->V5, V2 = V0->V3->V2, V4 = V0->V3->V4Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
