CSC211 Ā· TU past paper
Data Structures and Algorithms 2079 question paper
The complete TU 2079 exam paper for Data Structures and Algorithms (CSC211), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- Q1.10 marksNumericalAVL tree and Balancing algorithm, ApplicatHideAnswer
Why do we need to balance the binary search tree? Justify with an example. Create an AVL tree from the data 24, 12, 8, 15, 35, 30, 57, 40, 45, 78.[10]
- Q2.10 marksNumericalConversion from infix to postfix/prefix exHideAnswer
How recursive algorithm uses stack to store intermediate results? Illustrate with an example. Convert the infix expression A+B*(C/D+F)-G/H into postfix expression using stack.[10]
- Q3.10 marksBasic operations in Linked ListHideAnswer
How do you insert and delete a node at kth position of the doubly linked list? Describe the process of implementing stack and queue using linked list.[10]
- Q4.5 marksNumericalComparison Sorting AlgorithmsHideAnswer
Sort the numbers 82, 73, 12, 39, 26, 88, 2, 9, 60, 41 using shell sort. [5]
- Q5.5 marksIntroduction to Searching, Search AlgorithHideAnswer
Write a program to implement binary search. [5]
- Q6.5 marksNumericalDefinition and Representation of Graphs, GHideAnswer
Find the MST of following graph using Prim's algorithm. [5]
- Q7.5 marksNumericalHashingHideAnswer
Assume you have to store the data {0,1,2,4,5,7} into a hash table of size 5, with hash function, $h(x) = x % 5$. Apply linear probing and double hashing as collision resolution techniques. [5]
- Q8.5 marksNumericalDivide and Conquer SortingHideAnswer
In which case the position of pivot element in quick sort always either in the last or the first position? Create a max heap from the numbers {10,12,53,34,23,77,59,66,5,8}. [5]
- Q9.5 marksNumericalConversion from infix to postfix/prefix exHideAnswer
Evaluate the postfix expression 574-*8/4+ using stack. [5]
Evaluating Postfix Expression:
5 7 4 - * 8 / 4 +Given Data
- Postfix expression:
5 7 4 - * 8 / 4 + - Data structure to use: Stack
Algorithm (Postfix Evaluation)
- Scan the expression left to right.
- If the symbol is an operand, push it onto the stack.
- If the symbol is an operator, pop the top two operands:
op2= first pop (right operand)op1= second pop (left operand)- Compute
op1 <operator> op2and push the result.
- The final remaining stack element is the result.
Step-by-Step Trace
Step Symbol Action Stack (bottom ā top) 1 5Push 5 5 2 7Push 7 5, 7 3 4Push 4 5, 7, 4 4 -Pop 4, Pop 7 ā $7 - 4 = 3$; Push 3 5, 3 5 *Pop 3, Pop 5 ā $5 \times 3 = 15$; Push 15 15 6 8Push 8 15, 8 7 /Pop 8, Pop 15 ā $15 / 8$; Push result ... 8 4Push 4 (result), 4 9 +Pop 4, Pop (result) ā add; Push final
Handling the Division at Step 7
The value $15 / 8 = 1.875$.
Case A: Integer division (common in textbook/C-style examples): $$15 / 8 = 1$$ Then:
- Step 8: Push 4 ā stack:
1, 4 - Step 9: $1 + 4 = 5$
Final result = 5
Case B: Real (floating-point) division: $$15 / 8 = 1.875$$ Then:
- Step 8: Push 4 ā stack:
1.875, 4 - Step 9: $1.875 + 4 = 5.875$
Final result = 5.875
Result
Since this is a data structures problem typically evaluated with integer arithmetic, the standard exam answer is:
$$\boxed{5\ 7\ 4\ -\ *\ 8\ /\ 4\ +\ =\ 5}$$
(If real-number division is intended, the result would be $5.875$.)
The trace gives a final value of 5 (using integer division).
- Postfix expression:
- Q10.5 marksAsymptotic notations and common functionsHideAnswer
Why do we need asymptotic notation? Describe about Big oh notation with its curve. [5]
Asymptotic Notation and Big Oh Notation
Why Do We Need Asymptotic Notation? (2 marks)
When we analyze algorithms, we need a way to measure and compare their efficiency (time and space) without depending on machine speed, programming language, or hardware. Asymptotic notation provides a mathematical framework to describe the behavior of an algorithm as the input size grows toward infinity.
Key reasons we need asymptotic notation:
- To compare two algorithms independently of machine or implementation details.
- To describe the growth rate of an algorithm's running time or space requirement.
- To focus on the dominant term of a function and ignore constants and lower-order terms, which become insignificant for large inputs.
- To express best case, worst case, and average case complexity in a standard, concise way.
For example, if an algorithm takes T(n) = 5n² + 3n + 10 steps, asymptotic notation lets us simply say it grows as n², ignoring less significant terms.
Big Oh Notation O(g(n)) (3 marks)
Definition:
"When we have only asymptotic upper bound then we use O notation. If f and g are any two functions from set of integers then function f(x) is said to be big Oh of g(x)."
Formally:
f(n) = O(g(n)) if and only if there exist positive constants c and nā such that:
$$f(n) \leq c \cdot g(n) \quad \text{for all } n \geq n_0$$
- f(n) is the actual running time of the algorithm.
- g(n) is the bounding function (upper bound).
- c and nā are positive constants.
This means Big Oh gives the worst-case or upper bound on the growth of an algorithm.
Example
If f(n) = 4n + 2, then:
- 4n + 2 ⤠5n for all n ℠2
- So f(n) = O(n), with c = 5 and nā = 2
Curve / Graphical Representation
Running Time | c * g(n) | ./ | ./ | .../ <-- f(n) always stays | .../ below c*g(n) after nā | .../ | .../ |../ |_________________________ n nā- For all values of n ā„ nā, the curve of f(n) lies at or below the curve of c * g(n).
- Big Oh notation thus represents the asymptotic upper bound.
Key Points
Property Description Notation O(g(n)) Meaning Upper bound on running time Use case Worst-case analysis Condition f(n) ⤠c * g(n) for all n ā„ nā Common Big Oh Complexities (Best to Worst)
$$O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!)$$
Conclusion: Big Oh notation is the most widely used asymptotic notation because it tells us the maximum time an algorithm will ever take, helping us guarantee performance in the worst case.
- Q11.5 marksLinear Queue, Circular Queue, Priority QueHideAnswer
Define queue. Explain about enqueue and dequeue operation in circular queue. [5]
- Q12.5 marksLinear Queue, Circular Queue, Priority QueHideAnswer
Write short notes on: a. Priority Queue b. Breadth First traversal of a graph [5]