Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Data Structures and Algorithm - Mid Term

The document contains questions for a mid-term exam on data structures and algorithms. Question 1 asks to find the time complexity of 3 code snippets in terms of the variable n. Question 2 asks to write a program to store integer values in a stack and remove the largest value. Question 3 asks to implement a queue using a linked list, insert values, remove those greater than 50, and leave the queue in original order.

Uploaded by

Nishad Ahamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Data Structures and Algorithm - Mid Term

The document contains questions for a mid-term exam on data structures and algorithms. Question 1 asks to find the time complexity of 3 code snippets in terms of the variable n. Question 2 asks to write a program to store integer values in a stack and remove the largest value. Question 3 asks to implement a queue using a linked list, insert values, remove those greater than 50, and leave the queue in original order.

Uploaded by

Nishad Ahamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

UNIVERSITY OF MORATUWA

Faculty of Information Technology


IN 2110 - Data Structures and Algorithm
Mid-Term Examination 2022
Time: 1 ½ hours (90 minutes)

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)

int testFunction (int n, int m, int sum) {


if (n < 10) {
return n;
} else {
for (int i = 0; i < n; ++i) {
sum++;
return testFunction (n - 2, m, sum);
}
}
}

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.

public interface Stack {

public boolean isEmpty();


public void push(int d);
public int pop();
public int peek();
}

(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)

You might also like