Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

FAZAIA COLLEGE OF EDUCATION FOR WOMEN

SARWAR ROAD, LAHORE CANTT

MID TERM EXAMINATION SOLUTION NOV – 2021


BSCS –III Data Structure and Algorithm
Time Allowed: 2Hours Max Marks: 35

(OBJECTIVE)
SECTION – A
Time allowed: 15 minutes Marks: 10
Note: Each statement carries one mark. Cutting & overwriting is not allowed.
Q.1: Encircle the correct option.

i. Best Algorithm is
a. Solve with in minimum time. b. Give good accuracy
c. Minimum time d. Minimum cost

ii. Spares table is used for


a. Address store b. Information storage
c. Data storage d. None of them

iii. Any algorithms, which is not optimal?


a. Good algorithm b. Average Algorithm
c. Nice Algorithm d. Average Algorithm & bad

iv. Which algorithm is best algorithm and complexity?


a. Tight bound b. Lower bound
c. Upper bound d. All of them

v. None linear data structure is called


a. Array b. Queue
c. Links d. Tree

vi. Circle queues calculate rear position by


a. Rear = (rear+1) %maxSize b. Rear+1
c. maxSize d. (rear+1) %maxSize

vii. Elements are removed from queue in a math equation.


a. Plus b. Add
c. Division d. None
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT
viii. Stack follow the principle
a. FILO b. FIFO
c. LIFO d. FOFI

ix. Element can be added in a queue from one end is called


a. Back b. Front
c. Both of them d. None

x. If queue have five spaces and 50% queue is full, remaining places are
a. One b. One and Two
c. Two and Three d. None
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT
MID TERM EXAMINATION SOLUTION NOV – 2021
BSCS –III Data Structure and Algorithm
Time: 1Hrs 45 Mins Max Marks: 25
______________________________________________________________________________

(SUBJECTIVE)
SECTION – B
(5x3=15marks)
Q.2: Attempt the following questions. All questions carry equal marks.

i. What is skip list and circular queues, what is difference between them.

Answer:

Circular Queue is also a linear data structure, which follows the principle of FIFO(First In First Out),
but instead of ending the queue at the last position, it again starts from the first position after the last,
hence making the queue behave like a circular data structure.

In case of a circular queue, head pointer will always point to the front of the queue, and tail pointer will
always point to the end of the queue.

A skip list is a probabilistic data structure. The skip list is used to store a sorted list of elements or
data with a linked list. It allows the process of the elements or data to view efficiently. In one single
step, it skips several elements of the entire list, which is why it is known as a skip list.

ii. What is skip list and singly link list, what is difference between them.

Answer:

Skip List

A skip list is a probabilistic data structure. The skip list is used to store a sorted list of elements or
data with a linked list. It allows the process of the elements or data to view efficiently. In one single
step, it skips several elements of the entire list, which is why it is known as a skip list.
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT
Link List

A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another
link. Linked list is the second most-used data structure after array. Following are the important terms
to understand the concept of Linked List.

iii. Solve the following by using Master Theorem.


a) - T (n) = 32T¿) + n.

Answer:

a=32, b=16, k=1

Condition of Master’s Theorem:

Case:1 a>bk =O (n logab)

b) - T (n) = 8T¿) + n2.

Answer:

a= 8, b=12 ,K=2

Condition of Master’s Theorem:

Case: 3 a<bk =O(nk)

c) - T (n) = 8T¿) + n3.

Answer:

a=8, b=9, k=3

Condition of Master’s Theorem:

Case: 3 a<bk =O(nk)

iv. Find the time complexity of Nested Loops.


for (i = 1; i<=n; i++) {
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT
for(j=1; j<=n; j++){
k = k+1

}}

Answer:

Total time = c* n*n = cn2 = O(n2)

So, the time complexity is n2.. One n is for the inner loop and one for outer loop.

v. What is Order Notation, Upper Bounding Function and Lower Bounding Function.

Answer:

Upper Bounding Function

• Def: f(n)= O(g(n)) if  c >0 and n0 > 0 such that f(n)  cg(n) for all n  n0.

• How to show O (Big-Oh) relationships?

 f(n) = O(g(n)) iff limn   <  including the case where limit is 0

Lower Bounding Function

• Def: f(n)= (g(n)) if  c > 0 and n0 > 0 such that 0  f(n)  cg(n) for all n  n0.

• How to show  (Big-Omega) relationships?

• f(n) =  (g(n)) iff limn   > 0 including the case where limit is 
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT

SECTION – C
(2×5=10marks)

Q.3: Attempt the following questions.

(A): Explain the Following

i. What is difference between sparse tables and self-organized of list.

Sparse Tables

Sparse Table is a data structure, that allows answering range queries. It can answer most range
queries in O(logn), but its true power is answering range minimum queries (or equivalent range
maximum queries). ... The only drawback of this data structure is, that it can only be used on
immutable arrays.

Self-Organized of List

A self-organizing list is a list that reorders its elements based on some self-organizing heuristic to
improve average access time. The aim of a self-organizing list is to improve efficiency of linear search
by moving more frequently accessed items towards the head of the list.

ii. Write the code for link list, which have four nodes.

#include<iostream>
#include<conio.h>
using namespace std;
struct node
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT
{
int data;
node *link;
};
class list
{
private:
node *start, *cur,*temp;
public:
list()
{
start=NULL;
}
add_item(int n)
{
if(start==NULL)
{
start=new node;
start->link=NULL;
}
else
{
cur=start;
while(cur->link!=NULL)
cur=cur->link;
temp=new node;
temp->data=n;
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT
temp->link=start;
cur->link=temp;
}
}
insert(int x)
{
temp=new node;
temp->data=x;
temp->link=start;
start=temp;
}
print()
{
cur=start;
cout<<"\nvalue of list \n\n";
while(cur->link!=NULL)
{
cout<<cur->data<<endl;
cur=cur->link;
}
cout<<cur->data;
}

};
main()
{
list obj;
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT
int val,n;
cout<<"enter five values\n";
for(int i=1;i<=5;i++)
{
cin>>val;
obj.add_item(val);
}
cout<<"enter value to insert?";
cin>>n;
obj.insert(n);
obj.print();
}
(B): Explain the Following

i. What is Queue, implementation of Queue through Constructor, check Queue is empty, full,
front and rear.
FAZAIA COLLEGE OF EDUCATION FOR WOMEN
SARWAR ROAD, LAHORE CANTT

ii. What is stack, push 5 elements into stack and pop two elements.

A stack is a conceptual structure consisting of a set of homogeneous elements and is based on the
principle of last in first out (LIFO). It is a commonly used abstract data type with two major operations,
namely push and pop. Push and pop are carried out on the topmost element, which is the item most
recently added to the stack. The push operation adds an element to the stack while the pop operation
removes an element from the top position. The stack concept is used in programming and memory
organization in computers.

E
D D
C C C
B B B B
A A A A A

We Push five element in stack.

You might also like