Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Unity University

College of Engineering, Technology and Computational Sciences

Department of Computer Science


Data Structure and Algorithm

Part I : Algorithm analysis


1. for (I = 1; i<=n; i++)
for(j = 2; j<=n; j+=2)
sum = sum + i*j;

a. Size __________________________________
b. Complexity ____________________________
c. Order _________________________________
d. Big-O _________________________________

2. for (I = 1; i<=n; i++)


for(j = 2; j<=n; j+=2)
for(k = 3; k<=n; k+=3)
sum = sum + i*j*k;

a. size __________________________________
b. Complexity ____________________________
c. Order _________________________________
d. Big-O _________________________________

3. { I = 0;
while(I <= n )
{

Prepared by : worku w., Unity University Page 1 of 12


for(j = 2; j<=n; j+=2)
sum = sum + i*j;
i++;
}
}
a. size __________________________________
b. Complexity ____________________________
c. Order _________________________________
d. Big-O _________________________________

4. for (I = 0; i<n; i++)


for(j = 0; j<n; j++)
cin>>a[i][j];
for(I = 0; i<n; i++)
for(j = 0; j<n; j++)
sum = sum + a[i][j];

a. Size __________________________________
b. Complexity ____________________________
c. Order _________________________________
d. Big-O _________________________________

4. Define the following terms


a. data structure
b. linked list
c. primitive data structure
d. non-primitive data structure
e. array
f. pointer
g. structure
h. complexity
i. size
j. order
k. Big-O
l. Stack
m. Queue
n. Tree

Prepared by : worku w., Unity University Page 2 of 12


5. a. Given the array A[7] = {50,10,30,80,5,40,20,60}. Do the analysis to
sort the list in descending order of their size and design algorithm to
implement the analysis for the three simple sorting methods , bubble,
selection and insertion,

5. b. Give the array U[] = {‘U’,’N’,I’,’V’,’E’,’R’,’S’,’I’,’T’,’Y’}. Do the


analysis to sort the list in ascending order of the alphabet and design
algorithm to implement the analysis for the three simple sorting methods.

PART I : For each of the following choose the best answer and write the
letter of your choice on the answer sheet

1. The study of data structures usually involves analyzing algorithms in terms of :


a. Output size b. Program code size c. Speed of a machine d. size of data to be
processed e. All

2. A data structure whose operations are already implemented as a machine instruction is:
a. Primitive data structure b. Non-primitive data structure
c. Non-primitive linear data structure d. Both b and c e. None

3. After nlogn, the next function which describes algorithm running time in order of increasing
growth rate is:
a. n 2logn b. logn c. n3 d. n2 e. None

4. Which of the following is non-primitive linear data structure?


a. Array b. Linked List c. Tree d. Graph e. Binary
Search Tree

5. Which of the following operations on lists can be easily implemented using linked lists than
arrays?
a. Creation b. accessing an element c. Deletion d. b and c e. All

6. One of the following operations will not be successful if a stack is already full?
a. Push b. Top c. Pop d. Create e. IsEmpty
7. Which data structure reduces the running- time complexity of most operations to O(logn)
a. Array b. Linked List c. Stack d. Tree e. None

8. Data structure helps designing program which satisfies the following except

Prepared by : worku w., Unity University Page 3 of 12


a. large b. reliable c. efficient
d. flexible e. expandable f. none
9. A finite set of unambiguous instructions which, if followed, can accomplish certain
Task
a. order of algorithm b. complexity
c. algorithm d. analysis of algorithm e. none
10. One of the following sorting methods has the lowest number of interchange/swap
a. insertion b. selection
c. bubble d. all e. none
11 The case when an algorithm requires small number steps
a. the worst case b. the best case
c. the average case d. none
12 Which of the following is the time complexity of sequential search under worst case?
a. O(n2) b. O(n)
c. O(log2n) d. O(log2n)+1 e. O(1)
13 One of the following conditions must be satisfied to apply binary search
a. the list must be sorted b. size must be known in advance
c. the list be an array d. content of the list must be integral values
e. all except d
14 One of the following is not non-primitive data structure
a. string b. stack
c. double d. tree e. none
15 Data structures which are operated directly upon by machine level instruction
a. primitive b. non-primitive
C. linear d. non-linear
16 An ordered set consisting of a variable number of objects
a. batch b. array
c. list d. pointer
17. Linked list is more efficient than array for the following operations except
a. size b. insertion
c. delete d. traversal e. none
18. One of the following linked list function returns a pointer to the node requested from
List of available nodes
a. get_node b. position
c. free_node d. insert_last
e. node_to_delete f. delete_last
19. What are the contents of the stack after the following operation from top to bottom?
Create(s);
Push(s,40);
Push(s,80);
Pop(s);
Push(s,100);
Stacktop(s);
Push(s,10);
Push(s,20);
Pop(s);

Prepared by : worku w., Unity University Page 4 of 12


a. 20,10,100,80,40 b. 100,80,40
c. 10,100,40 d. 20,10 e. 10,40

Consider the following infix expression to answer question13,14


A-(B*(C+D)/E)
20. Which of the following is the correct prefix form of the above expression?
a. A-/*B+CDE b. A*/B+CDE
c. –A/*B+CDE d. /+*-ABCDE e. none
21. Which of the following is the correct postfix form of the expression?
a. ABCD+E/*- b. ABC*D+E/-
c. ABC+DE/*- d. ABCD+*E/- e. ABCDE+*/-
22. How do you identify a full stack which is implemented using an array whose size is
max
a. when s.top == max-1 b. when s.top > max-1
c. when s.top < max d. when s.top == max e. none
23. What is the order of computing the sum of a two dimensional array of integers
a. n b. n2
c. n3 d. log2n
Consider the following circular queue specification to answer questions 17,18,19
const max = 5;
struct queue
{int data[max];
int front;
int rear;
};
queue q;
q.front = 0;
q.rear = 0;
enqueue(q,16); enqueue(q,12); x = dequeue(q); enqueue(q,20)
24. After applying the following queue operations, what will be the value of q.front and
q.rear respectively
a. 1 and 3 b. 3 and 1
c. 0 and 3 d. 3 and 0 e. none
25. The following operations are applied just after the above operations
enqueue(q,18); enqueue(q,19); x = dequeue(q); enqueue(q,65);
now what will be the respective values of q.front and q.rear?
a. 2 and 5 b. 2 and 0
c. 2 and 1 d. 2 and 2 e. none
26. Which one of the following operation is not valid operation after the last operation
in question 18
a. front b. dequeue
c. enqueue d. display e. none

27. Why do we make the position of the array pointed by ‘front’ vacant in a circular
queue
a. to facilitate insertion b. to identify empty and full condition of the queue

Prepared by : worku w., Unity University Page 5 of 12


c. to facilitate deletion d. to initialize the queue
28. If the inorder and preorder traversal of a binary tree are D,B,F,E,G,H,A,C and
A,B,D,E,F,G,H,C respectively then, the postorder traversal of that tree is :
a. D,F,G,A,B,C,H,E b. F,H,D,G,E,B,C,A
c. C,G,H,F,E,D,B,A d. D,F,H,G,E,B,C,A e. none
29. A binary tree with the property, all the data in any given node of the tree is greater
than or equal to the data in its left and right subtree
a. binary tree with heap property b. binary tree with ordering property
c. binary expression tree d. complete binary tree
30. Which of the following techniques lists the content of nodes of a binary search tree in
ascending order
a. postorder b. inorder
c. preorder d. none
31. Whe following sequence of operations are applied on stack:
push(s,1), push(s,2), pop(s),push(s,1), push(s,2),pop(s), pop(s), pop(s), push(s,2), pop(s)
32. Which of the following is incorrect
a. pointers are used for allocating memory dynamically
b. dynamic memory allocation is preferred when size is not predictable
c. stack can be used to convert any infix expression to prefix
d. evaluate prefix expression
e. except d
f. none

Part II – True or False. Write TRUE if the statement is true, otherwise FALSE on the space provide
(5 marks)

9. _________An abstract data structure (ADT) can be implemented by using an array or a


linked list.
10. _________Doubly linked list implementation of a data structure facilitates operations to be
performed than singly linked lists.
11. _________Binary search is preferred when data is stored in a linked list than linear search.
.
12. _________Tree data structure is appropriate to implement evaluation of arithmetic
expressions and the file systems of most operating systems
13. _________The number of sub trees of a node is the height of the node.

1. The degree of a tree is the same as the depth of the tree


2. Both insertion into and deletion from any contiguous list involves physical
movement of data
3. If a stack is implemented as linked list there is no chance of underflow
4. In circular array implementation of a queue, there is no physical data movement
When enqueue and dequeue operations are applied
5. All leaf nodes in a binary tree with ordering property have the same depth

PART III : For each of the following questions give your answer on the space provided
Part III – Short Answers. Write your answers on the blank spaces provided after each

Prepared by : worku w., Unity University Page 6 of 12


question.(34 marks)

14. What does it mean time complexity of an algorithm? (1 mark)

15. Give examples of an algorithm that is O(1) and O(n3). Give simple C++ statements. (2 marks)

O(1) O(n3)

16. Device sample data types to demonstrate the best case and worst-case behaviors of each of the
following algorithms:
Bubble sort, Selection sort, Linear search, and Binary search. (6 marks)

Sample data
Algorithm
Best-Case Worst-Case

Bubble Sort

Selection Sort

Linear Search

17. Searching an item from a Complete Binary Search Tree (BST) of size n has running-time complexity
O(nlogn) in the worst-case. Explain. (2 marks)

18. Algorithm A does a certain task in a “time” 100n2logn and algorithm B does the same task in a “time”
of 5000n2 + n where n is the number of elements processed. (4 marks)
a. What are the Big-O requirements of each algorithm?

i. O(A):___________________________________

ii. O(B):___________________________________

b. Which algorithm is efficient by Big-O standards? ________________________________

Prepared by : worku w., Unity University Page 7 of 12


c. Under what condition, if any, would the less efficient algorithm execute more quickly than the
more efficient algorithm?

________________________________________________________________________________
__________________________________________________________________________________

19. Give examples of applications that need Stack data structure (LIFO basis) and Queue data structure
(FIFO basis) for manipulation. (3 marks)

Data Application Example


Struct
ure

i._______________________________________________________________________

Stack ii.______________________________________________________________________

iii.______________________________________________________________________

i._______________________________________________________________________

Queue ii.______________________________________________________________________

iii.________________________________________________________________________

20. Compare and contrast the Sequential and Linked List storage representations of lists. (2 marks)

Sequential Representation Linked List Representation

21. Evaluate the following two Postfix arithmetic expressions. (2 marks)

Prepared by : worku w., Unity University Page 8 of 12


a. 6 4 + 1 - 3 / b. 1 4 18 6 / 3 + + 5 * +

22. Convert the following infix arithmetic expression in to postfix (RPN). Trace using Stack Algorithm in
conversion process. (2marks)

A + (B - C) *D+B

1. consider the following infix expression


A+(B+C*D-(E+F/B))/B
a. write the equivalent prefix form : ________________________________
b. write the equivalent postfix form : _______________________________
c. show how the postfix form is evaluated using stack if
A = 7, B = 4, C = 6, D = 2, E = 3, F = 8

2. consider the prefix expression : +-$ABC*D**EFG


a. write the equivalent infix form : _________________________________
b. evaluate the prefix expression, if : _______________________________
A = 2,B = 3, C = 1, D = 2, E = 3, F = 4, G = 5

Prepared by : worku w., Unity University Page 9 of 12


3. consider the following binary tree with ordering property

root

70

65 90

60
68 85 95

40 62 69 80 88

a. what is the depth of the tree : ______________________


b. inorder traversal : _______________________________
c. preorder traversal : ______________________________
d. postorder traversal : _____________________________
e. redraw the binary tree after addition of a node containing 86

f. redraw the binary tree after deletion of a node containing 65

4. draw a binary expression tree for the following arithmetic expression


A+(B-C)*D$(E*F)

5. draw a possible binary tree with ordering property containing the following
integers
3,12,17,49,22,65,51,56,70,68

PART IV : For each of the following questions, write a function on the space provided

1. write a function stores the common elements of two linked lists of integer headed
by head1 and head2 into the third list headed by head3 .
use the following declaration

struct node
{ int data;
node *next;
};
typedef node *pnode;

Prepared by : worku w., Unity University Page 10 of 12


void common(pnode head1, pnode head2, pnode &head3)
2. write a function that split a given linked list of integer into odd and even linked
lists

3. write a function that deletes and returns the top element of a stack. Use the
following declaration

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

struct stack
{
node *top;
};

4. write a function that calculates average, sum, minimum and maximum of a stack

5. write a function that inserts an item into a given queue. Use the following declaration
struct node
{int data;
node *next;
};

struct queue
{ node *front;
node *rear;
};
float average(queue q)

6. write a function that sorts elements of a stack in ascending order of its size.

7. write a function that calculates average, sum, minimum and maximum of a queue.

8. write a program that reverse elements of a stack

9. write a program that move elements of stack s into stack ss and stack ss store the
elements in the same order of stack s. (HINT: uses additional variable, or
additional stack.)
10. write a function that reverse elements of a linked list
11. write a function that converts infix notation to postfix
12. write a function that converts postfix into infix

Prepared by : worku w., Unity University Page 11 of 12


13. write a function that converts infix notation to prefix
14. what are some applications of stack
15. what are some applications of queue
16. what are some applications of tree
17. write a function that counts the number of leaf node of a given binary tree
18. write a function that calculates the depth of given binary tree
19. write a function that counts the number of nodes

Prepared by : worku w., Unity University Page 12 of 12

You might also like