Data Structures and Algorithm - Mid Term
Data Structures and Algorithm - Mid Term
Question 01
Find the worst-case time complexity of following code segments in terms of the variable n.
Explain the steps of time complexity calculations. (Marks: 40)
(1)
void testFunction(int n) {
for(int i=0; i < n; i++) {
for(int j=0; j < 10; j++) {
for(int k=0; k < n; k++) {
for(int m=0; m < 20; m++) {
System.out.println("*");
}
}
}
}
}
(2)
int testFunction(int n) {
int value = 60;
for(int i=0; i < n; i++) {
for(int j=i; j >= 5; j--) {
value--;
}
}
return value;
}
(3)
Question 02
You are given a list of eight integer values (23, 20, 90, 45, 75, 24, 30, 48) and consider they are
stored in a stack. You have to find the highest value and get out that value out from this stack.
Finally, stack should be leaved as 23, 20, 45, 75, 24, 30, 48.
Write a program (Java code or pseudo code) to store these values in a stack and get the largest
value out of these values. Assume stack class is already implemented and the implementation of
the stack interface is given as follows. Use stack functions directly in your code.
(Marks: 30)
Question 03
Implement a Queue using a Linked List and insert 10 integer values to it. Then find and delete
the values which are greater than 50. Finally, leave the queue in the original order. Clearly
explain the steps of this process using suitable representations.
(Marks:30)