SlideShare ist ein Scribd-Unternehmen logo
1 von 105
Which of the following is faster?
A binary search of an ordered set of elements in an array
or a sequential search of the elements?
Question No : 1
The binary search is faster than the sequential
search.
The complexity of binary search is 'log n' whereas the
complexity of a sequential search is 'n'.
In a binary search, each time we proceed, we have to deal
with only half of the elements of the array compared to the
previous one. So the search is faster.
Answer
List out the areas in which data structures are
applied extensively?
Question No : 2
Compiler
Design
Operating
System
Database
Management
System
Statistical
analysis
package
Numerical
Analysis
Graphics
Artificial
Intelligence
Simulation
Answer
Which data structure is used to perform recursion?
Question No : 3
Stack.
Because of its LIFO (Last In First Out) property it remembers
its caller and hence knows where to return to when the
function has to return.
Recursion makes use of system stack for storing the return
addresses of the function calls. Every recursive function
has its equivalent iterative (non-recursive) function.
Even when such equivalent iterative procedures are written,
explicit stack is to be used.
Answer
Tree can have duplicate values :
True (or) False?
Question No : 4
True
Tree defines the structure of an acyclic graph but does not
disallow duplicates.
Answer
The size of a Tree is the number of nodes in the
Tree : True (or) False?
Question No : 5
True
The size denotes the number of nodes, height denotes the
longest path from leaf node to root node.
Answer
Ram is told to sort a set of Data using Data structure.
He has been told to use one of the following Methods
a. Insertion
b. Selection
c. Exchange
d. Linear
Now Ram says a Method from the above can not be
used to sort. Which is the method?
Question No : 6
d. Linear
Using insertion we can perform insertion sort, using
selection we can perform selection sort, and using
exchange we can perform bubble sort.
But no sorting method is possible using linear method;
Linear is a searching method
Answer
Ashok is told to manipulate an Arithmetic
Expression. What is the data structure he will use?
a. Linked List
b. Tree
c. Graph
d. Stack
Question No : 7
d. Stack
Stacks are used to evaluate the algebraic or arithmetic
expressions using prefix or postfix notations
Answer
There are 8,15,13,14 nodes in 4 different trees.
Which of them could form a full binary tree?
a. 8
b. 15
c. 13
d. 14
Question No : 8
In general, there are 2n – 1 nodes in a full
binary tree.
By the method of elimination: Full binary tree contains
odd number of nodes.
So there cannot be a full binary tree with 8 or 14 nodes.
With 13 nodes, you can form a complete binary tree but
not a full binary tree.
Full and complete binary trees are different
All full binary trees are complete binary trees but not vice
versa
Answer
Full binary Tree:
A binary tree is a full binary tree if and only if:
Each non leaf node has exactly two child nodes
All leaf nodes have identical path length
It is called full since all possible node slots are
occupied A
B C
D E F G
Answer
Complete binary Tree:
A complete binary tree (of height h) satisfies the
following conditions:
Level 0 to h-1 represent a full binary tree of height h-1
One or more nodes in level h-1 may have 0, or 1 child
nodes
Answer
A
B C
D E F G
H I J K
How many null branches are there in a binary tree
with 20 nodes?
Question No : 9
21 (null branches)
Let’s consider a tree with 5 nodes
So the total number of null nodes in a binary tree of n
nodes is n+1
Answer
Null branches
Write an algorithm to detect loop in a linked list.
You are presented with a linked list, which may have a
"loop" in it. That is, an element of the linked list may
incorrectly point to a previously encountered element,
which can cause an infinite loop when traversing the list.
Devise an algorithm to detect whether a loop exists in a
linked list. How does your answer change if you cannot
change the structure of the list elements?
Question No : 10
One possible answer is to add a flag to each element of
the list.
You could then traverse the list, starting at the head
and tagging each element as you encounter it.
If you ever encountered an element that was already
tagged, you would know that you had already visited it
and that there existed a loop in the linked list.
What if you are not allowed to alter the structure of the
elements of the linked list?
Answer
The following algorithm will find the loop:
a) Start with two pointers ptr1 and ptr2.
b) Set ptr1 and ptr2 to the head of the linked list.
c) Traverse the linked list with ptr1 moving twice as fast as ptr2
(for every two elements that ptr1 advances within the list,
advance ptr2 by one element).
d) Stop when ptr1 reaches the end of the list, or when ptr1 = ptr2.
e) If ptr1 and ptr2 are ever equal, then there must be a loop in the
linked list. If the linked list has no loops, ptr1 should reach the
end of the linked list ahead of ptr2
Answer
The Operation that is not allowed in a binary search
tree is
a. Location Change
b. Search
c. Deletion
d. Insertion
Question No : 11
a. Location Change
Answer
Array is a type of ________________ data structure.
a. Non Homogenous
b. Non Linear
c. Homogenous but not Linear
d. Both Homogenous and Linear
Question No : 12
d. Both Homogenous and Linear.
Answer
The address of a node in a data structure is called
a. Pointer
b. Referencer
c. Link
d. All the above
Question No : 13
d. All the above
Answer
The minimum number of edges in a connected
cycle graph on n vertices is ________
a. n
b. n + 1
c. n – 1
d. 2n
Question No : 14
a. n
Answer
The total number of passes required in a selection
sort is
a. n + 1
b. n – 1
c. n
d. n * n
Question No : 15
b. n – 1
Answer
The node that does not have any sub trees is
called ___________
a. Null Node
b. Zero Node
c. Leaf Node
d. Empty Node
Question No : 16
c. Leaf Node
Answer
Linked List is a
a. Static Data Structure
b. Primitive Data Structure
c. Dynamic Data Structure
d. None of the above
Question No : 17
c. Dynamic Data Structure
Answer
Which data structure is needed to convert infix
notation to postfix notation.
a. Tree
b. Linear Linked List
c. Stack
d. Queue
Question No : 18
c. Stack
Answer
If every node u in Graph (G) is adjacent to every
other node v in G, it is called as _____ graph.
a. Directed Graph
b. Complete Graph
c. Connected Graph
d. Multi Graph
Question No : 19
b. Complete Graph
Answer
Bubble sort is an example of
a. Selection sort technique
b. Exchange sort technique
c. Quick sort technique
d. None of the options
Question No : 20
b. Exchange sort technique
Answer
How do you chose the best algorithm among
available algorithms to solve a problem
a. Based on space complexity
b. Based on programming requirements
c. Based on time complexity
d. All the above
Question No : 21
d. All the above
Answer
Which of the following are called descendants?
a. All the leaf nodes
b. Parents, grandparents
c. Root node
d. Children, grandchildren
Question No : 22
d. Children, grandchildren
Answer
Choose the limitation of an array from the below
options.
a. Memory Management is very poor
b. Searching is slower
c. Insertion and deletion are costlier
d. Insertion and Deletion is not possible
Question No : 23
c. Insertion and deletion are costlier
(It involves shifting rest of the elements)
Answer
Areas where stacks are popularly used are.
a. Subroutines
b. Expression Handling
c. Recursion
d. All the above
Question No : 24
d. All the above
Answer
How would you implement queue using stack(s)?
Question No : 25
Use a temp stack
Data In into queue
Push the element into the original stack
Data Out from queue
Pop all the elements from stack into a temp stack
pop out the first element from the temp stack
Answer
Write a C program to compare two linked lists.
Question No : 26
int compare_linked_lists(struct
node *q, struct node *r){
static int flag;
if((q==NULL ) && (r==NULL)){
flag=1;
}
else{
if(q==NULL || r==NULL){
flag=0;
}
if(q->data!=r->data){
flag=0;
}
else{
compare_linked_lists(q-
>link,r->link);
}
}
return(flag);
}
Answer
Write a C program to return the nth node from the
end of a linked list.
Question No : 27
Suppose one needs to get to the 6th node from the end in the LL. First,
just keep on incrementing the first pointer (ptr1) till the number of
increments cross n (which is 6 in this case)
STEP 1 : 1(ptr1,ptr2) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
STEP 2 : 1(ptr2) -> 2 -> 3 -> 4 -> 5 -> 6(ptr1) -> 7 -> 8 -> 9 -> 10
Now, start the second pointer (ptr2) and keep on incrementing it till the
first pointer (ptr1) reaches the end of the LL.
STEP 3 : 1 -> 2 -> 3 -> 4(ptr2) -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
(ptr1)
So here you have the 6th node from the end pointed to by ptr2!
Answer
struct node {
int data;
struct node *next;
}mynode;
mynode * nthNode(mynode *head, int n /*pass 0 for last node*/)
{
mynode *ptr1,*ptr2;
int count;
if(!head) {
return(NULL);
}
ptr1 = head;
ptr2 = head;
count = 0;
Answer
while(count < n) {
count++;
if((ptr1=ptr1->next)==NULL) {
//Length of the linked list less than n. Error.
return(NULL);
} }
while((ptr1=ptr1->next)!=NULL) {
ptr2=ptr2->next;
}
return(ptr2);
}
Answer
Write a C program to insert nodes into a linked list
in a sorted fashion?
Question No : 28
Answer
// Special case code for the head end
void linkedListInsertSorted(struct node**
headReference, struct node* newNode)
{
// Special case for the head end
if (*headReference == NULL || (*headReference)->data
>= newNode->data)
{
newNode->next = *headReference;
The solution is to iterate down the list looking for the correct place to
insert the new node. That could be the end of the list, or a point just
before a node which is larger than the new node.
Let us assume the memory for the new node has already been
allocated and a pointer to that memory is being passed to this
function.
Answer
*headReference = newNode;
}
else {
// Locate the node before which the insertion is to
happen!
struct node* current = *headReference;
while (current->next!=NULL && current->next->data <
newNode->data)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
Write a C program to remove duplicates from a
sorted linked list?
Question No : 29
Answer
// Remove duplicates from a sorted list
void RemoveDuplicates(struct node* head) {
struct node* current = head;
if (current == NULL) return; // do nothing if the
list is empty
// Compare current node with next node
while(current->next!=NULL)
{
As the linked list is sorted, we can start from the beginning of
the list and compare adjacent nodes.
When adjacent nodes are the same, remove the second one.
There's a tricky case where the node after the next node needs
to be noted before the deletion.
Answer
if (current->data == current->next->data)
{
struct node* nextNext = current->next->next;
free(current->next);
current->next = nextNext;
}
else
{
current = current->next; // only advance if no
deletion
}
}
}
Write a C program to find the depth or height of a
tree.
Question No : 30
Answer
#define max(x,y) ((x)>(y)?(x):(y))
struct Bintree {
int element;
struct Bintree *left;
struct Bintree *right;
};
typedef struct Bintree* Tree;
int height(Tree T) {
if(!T)
return -1;
else
return (1 + max(height(T->left), height(T->right)))
}
Write C code to determine if two trees are identical
Question No : 31
Answer
struct Bintree {
int element;
struct Bintree *left;
struct Bintree *right;
};
typedef struct Bintree* Tree;
int CheckIdentical( Tree T1, Tree T2 )
{
if(!T1 && !T2) // If both tree are NULL then return true
return 1;
Answer
else if((!T1 && T2) || (T1 && !T2)) //If either of one is
NULL, return false
return 0;
else
return ((T1->element == T2->element) &&
CheckIdentical(T1->left, T2-i>left)
&& CheckIdentical(T1->right, T2->right));
// if element of both tree are same and left and right
tree is also same then both
trees are same
}
Write a C code to create a copy of a Tree
Question No : 32
Answer
mynode *copy(mynode *root)
{
mynode *temp;
if(root==NULL)return(NULL);
temp = (mynode *) malloc(sizeof(mynode));
temp->value = root->value;
temp->left = copy(root->left);
temp->right = copy(root->right);
return(temp);
}
Which of the following are called siblings
a. Children of the same parent
b. All nodes in the given path upto leaf node
c. All nodes in a sub tree
d. Children, Grand Children
Question No : 33
a. Children of the same parent
Answer
Linked List can grow and shrink in size dynamically
at __________ .
a. Runtime
b. Compile time
Question No : 34
a. Runtime
Answer
The postfix of A+(B*C) is
a. ABC*+
b. AB+C*
c. ABC+*
d. +A*BC
Question No : 35
a. ABC*+
Answer
Data structure using sequential allocation is called
a. Linear Data Structure
b. Non-Linear Data Structure
c. Non-primitive Data Structure
d. Sequence Data Structure
Question No : 36
a. Linear Data Structure
Answer
A linear list in which elements can be added or
removed at either end but not in the middle is
known as
a. Tree
b. Queue
c. Dequeue
d. Stack
Question No : 37
a. Dequeue
Answer
The average number of key comparisons done in a
successful sequential search in list of length n is
a. n+1/2
b. n-1/2
c. n/2
d. log n
Question No : 38
a. n+1/2
Answer
A full binary tree with n leaves contains __________
a. nlog2n nodes
b. 2^n nodes
c. (2n-1) nodes
d. n nodes
Question No : 39
c. (2n-1) nodes
Answer
If a node has positive outdegree and zero indegree,
it is called a __________.
a. Source
b. Sink
c. outdegree node
d. indegree node
Question No : 40
a. Source
Answer
The postfix notation for ((A+B)^C-(D-E)^(F+G)) is
a. AB + C*DE—FG+^
b. ^-*+ABC –DE + FG
c. ^+AB*C—DE^+FG
d. ABC + CDE *-- FG +^
Question No : 41
a. AB + C*DE—FG+^
Answer
If you are using C language to implement the
heterogeneous linked list, what pointer type will
you use?
Question No : 42
The heterogeneous linked list contains different
data types in its nodes and we need a pointer to
connect them.
It is not possible to use ordinary pointers for this.
So we use void pointer.
Void pointer is capable of storing pointer to any
type of data (eg., integer or character) as it is a
generic pointer type.
Answer
What is heap sort?
Question No : 43
A Heap is an almost complete binary tree. In this tree, if the
maximum level is i, then, upto the (i-1)th level should be complete.
At level i, the number of nodes can be less than or equal to 2^i. If
the number of nodes is less than 2^i, then the nodes in that level
should be completely filled, only from left to right
The property of an ascending heap is that, the root is the lowest and
given any other node i, that node should be less than its left child
and its right child. In a descending heap, the root should be the
highest and given any other node i, that node should be greater than
its left child and right child.
Answer
To sort the elements, one should create the heap first. Once the
heap is created, the root has the highest value. Now we need to
sort the elements in ascending order. The root can not be
exchanged with the nth element so that the item in the nth
position is sorted. Now, sort the remaining (n-1) elements. This
can be achieved by reconstructing the heap for (n-1) elements.
Answer
heapsort() {
n = array(); // Convert the tree into
an array.
makeheap(n); // Construct the
initial heap.
for(i=n; i>=2; i--) {
swap(s[1],s[i]);
heapsize--;
keepheap(i);
}
}
makeheap(n) {
heapsize=n;
for(i=n/2; i>=1; i--)
keepheap(i);
}
keepheap(i) {
l = 2*i;
r = 2*i + 1;
p = s[l];
q = s[r];
t = s[i];
Answer
Answer
if(l<=heapsize && p->value > t->value)
largest = l;
else
largest = i;
m = s[largest];
if(r<=heapsize && q->value > m->value)
largest = r;
if(largest != i) {
swap(s[i], s[largest]);
keepheap(largest);
}
}
Implement the bubble sort algorithm. How can it
be improved? Write the code for selection sort,
quick sort, insertion sort.
Question No : 44
Bubble sort algorithm
Answer
void bubble_sort(int a[], int n)
{
int i, j, temp;
for(j = 1; j < n; j++)
{
for(i = 0; i < (n - j); i++)
{
if(a[i] >= a[i + 1])
{
//Swap a[i], a[i+1]
}
}
}
}
void bubble_sort(int a[], int n)
{
int i, j, temp;
int flag;
for(j = 1; j < n; j++) {
flag = 0;
for(i = 0; i < (n - j); i++) {
if(a[i] >= a[i + 1])
{
//Swap a[i], a[i+1]
flag = 1;
}
}
if(flag==0)break;
}
}
To improvise this basic algorithm, keep track of whether a
particular pass results in any swap or not.
If not, you can break out without wasting more cycles.
Answer
Selection Sort Algorithm
void selection_sort(int a[],
int n) {
int i, j, small, pos, temp;
for(i = 0; i < (n - 1); i++)
{
small = a[i];
pos = i;
for(j = i + 1; j < n; j++)
{
if(a[j] < small)
{
small = a[j];
pos = j;
}
}
temp = a[pos];
a[pos] = a[i];
a[i] = temp;
}
}
Answer
Quick Sort Algorithm
int partition(int a[], int low, int high)
{
int i, j, temp, key;
key = a[low];
i = low + 1;
j = high;
while(1) {
while(i < high && key >= a[i])i++;
while(key < a[j])j--;
if(i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else {
temp = a[low];
a[low] = a[j];
a[j] = temp;
return(j);
}
}
}
Answer
Answer
void quicksort(int a[], int low, int high)
{
int j;
if(low < high) {
j = partition(a, low, high);
quicksort(a, low, j - 1);
quicksort(a, j + 1, high);
}
}
int main() {
// Populate the array a
quicksort(a, 0, n - 1);
}
Insertion Sort Algorithm
Answer
void insertion_sort(int a[], int n)
{
int i, j, item;
for(i = 0; i < n; i++)
{
item = a[i];
j = i - 1;
while(j >=0 && item < a[j])
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = item;
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

Data structure-questions
Data structure-questionsData structure-questions
Data structure-questions
Shekhar Chander
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 
Lecture 4 variables data types and operators
Lecture 4  variables data types and operatorsLecture 4  variables data types and operators
Lecture 4 variables data types and operators
alvin567
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
Aakash deep Singhal
 

Was ist angesagt? (20)

Data structure-questions
Data structure-questionsData structure-questions
Data structure-questions
 
Fuzzy System and fuzzy logic -MCQ
Fuzzy System and fuzzy logic -MCQFuzzy System and fuzzy logic -MCQ
Fuzzy System and fuzzy logic -MCQ
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Introduction to fuzzy logic
Introduction to fuzzy logicIntroduction to fuzzy logic
Introduction to fuzzy logic
 
Learn from LL(1) to PEG parser the hard way
Learn from LL(1) to PEG parser the hard wayLearn from LL(1) to PEG parser the hard way
Learn from LL(1) to PEG parser the hard way
 
Data structures
Data structuresData structures
Data structures
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Lecture 4 variables data types and operators
Lecture 4  variables data types and operatorsLecture 4  variables data types and operators
Lecture 4 variables data types and operators
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
1
11
1
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
C faqs interview questions placement paper 2013
C faqs interview questions placement paper 2013C faqs interview questions placement paper 2013
C faqs interview questions placement paper 2013
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary trees
Binary treesBinary trees
Binary trees
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 

Ähnlich wie Ds 111011055724-phpapp01

Page 3SECTION 1. Algorithm Analysis [1 pt per prompt = 12 poi.docx
Page 3SECTION 1.  Algorithm Analysis [1 pt per prompt = 12 poi.docxPage 3SECTION 1.  Algorithm Analysis [1 pt per prompt = 12 poi.docx
Page 3SECTION 1. Algorithm Analysis [1 pt per prompt = 12 poi.docx
bunyansaturnina
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
alisha230390
 
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
maxinesmith73660
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
IT Eagers
 
Sample prac exam2013
Sample prac exam2013Sample prac exam2013
Sample prac exam2013
hccit
 

Ähnlich wie Ds 111011055724-phpapp01 (20)

Data Structure
Data StructureData Structure
Data Structure
 
Ds qb 2021 rma
Ds qb 2021 rmaDs qb 2021 rma
Ds qb 2021 rma
 
Technical aptitude questions_e_book1
Technical aptitude questions_e_book1Technical aptitude questions_e_book1
Technical aptitude questions_e_book1
 
Page 3SECTION 1. Algorithm Analysis [1 pt per prompt = 12 poi.docx
Page 3SECTION 1.  Algorithm Analysis [1 pt per prompt = 12 poi.docxPage 3SECTION 1.  Algorithm Analysis [1 pt per prompt = 12 poi.docx
Page 3SECTION 1. Algorithm Analysis [1 pt per prompt = 12 poi.docx
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
 
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
 
Data structure
Data structureData structure
Data structure
 
Q
QQ
Q
 
Ee693 sept2014quiz1
Ee693 sept2014quiz1Ee693 sept2014quiz1
Ee693 sept2014quiz1
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
Ee693 sept2014quizgt1
Ee693 sept2014quizgt1Ee693 sept2014quizgt1
Ee693 sept2014quizgt1
 
Lab12 dsa bsee20075
Lab12 dsa bsee20075Lab12 dsa bsee20075
Lab12 dsa bsee20075
 
Sample prac exam2013
Sample prac exam2013Sample prac exam2013
Sample prac exam2013
 
50 must know coding interview questions
50 must know coding interview questions50 must know coding interview questions
50 must know coding interview questions
 
Data Structures
Data StructuresData Structures
Data Structures
 
Data structures
Data structuresData structures
Data structures
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
611+tutorial
611+tutorial611+tutorial
611+tutorial
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 

Mehr von Getachew Ganfur

Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
Getachew Ganfur
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
Getachew Ganfur
 
Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01
Getachew Ganfur
 
Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02
Getachew Ganfur
 
Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02
Getachew Ganfur
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
Getachew Ganfur
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
Getachew Ganfur
 
Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02
Getachew Ganfur
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01
Getachew Ganfur
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 

Mehr von Getachew Ganfur (13)

Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 
Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01
 
Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02
 
Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Document(1)
Document(1)Document(1)
Document(1)
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
 
Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
1to5 4th
1to5 4th1to5 4th
1to5 4th
 

Ds 111011055724-phpapp01

  • 1.
  • 2. Which of the following is faster? A binary search of an ordered set of elements in an array or a sequential search of the elements? Question No : 1
  • 3. The binary search is faster than the sequential search. The complexity of binary search is 'log n' whereas the complexity of a sequential search is 'n'. In a binary search, each time we proceed, we have to deal with only half of the elements of the array compared to the previous one. So the search is faster. Answer
  • 4. List out the areas in which data structures are applied extensively? Question No : 2
  • 6. Which data structure is used to perform recursion? Question No : 3
  • 7. Stack. Because of its LIFO (Last In First Out) property it remembers its caller and hence knows where to return to when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used. Answer
  • 8. Tree can have duplicate values : True (or) False? Question No : 4
  • 9. True Tree defines the structure of an acyclic graph but does not disallow duplicates. Answer
  • 10. The size of a Tree is the number of nodes in the Tree : True (or) False? Question No : 5
  • 11. True The size denotes the number of nodes, height denotes the longest path from leaf node to root node. Answer
  • 12. Ram is told to sort a set of Data using Data structure. He has been told to use one of the following Methods a. Insertion b. Selection c. Exchange d. Linear Now Ram says a Method from the above can not be used to sort. Which is the method? Question No : 6
  • 13. d. Linear Using insertion we can perform insertion sort, using selection we can perform selection sort, and using exchange we can perform bubble sort. But no sorting method is possible using linear method; Linear is a searching method Answer
  • 14. Ashok is told to manipulate an Arithmetic Expression. What is the data structure he will use? a. Linked List b. Tree c. Graph d. Stack Question No : 7
  • 15. d. Stack Stacks are used to evaluate the algebraic or arithmetic expressions using prefix or postfix notations Answer
  • 16. There are 8,15,13,14 nodes in 4 different trees. Which of them could form a full binary tree? a. 8 b. 15 c. 13 d. 14 Question No : 8
  • 17. In general, there are 2n – 1 nodes in a full binary tree. By the method of elimination: Full binary tree contains odd number of nodes. So there cannot be a full binary tree with 8 or 14 nodes. With 13 nodes, you can form a complete binary tree but not a full binary tree. Full and complete binary trees are different All full binary trees are complete binary trees but not vice versa Answer
  • 18. Full binary Tree: A binary tree is a full binary tree if and only if: Each non leaf node has exactly two child nodes All leaf nodes have identical path length It is called full since all possible node slots are occupied A B C D E F G Answer
  • 19. Complete binary Tree: A complete binary tree (of height h) satisfies the following conditions: Level 0 to h-1 represent a full binary tree of height h-1 One or more nodes in level h-1 may have 0, or 1 child nodes Answer A B C D E F G H I J K
  • 20. How many null branches are there in a binary tree with 20 nodes? Question No : 9
  • 21. 21 (null branches) Let’s consider a tree with 5 nodes So the total number of null nodes in a binary tree of n nodes is n+1 Answer Null branches
  • 22. Write an algorithm to detect loop in a linked list. You are presented with a linked list, which may have a "loop" in it. That is, an element of the linked list may incorrectly point to a previously encountered element, which can cause an infinite loop when traversing the list. Devise an algorithm to detect whether a loop exists in a linked list. How does your answer change if you cannot change the structure of the list elements? Question No : 10
  • 23. One possible answer is to add a flag to each element of the list. You could then traverse the list, starting at the head and tagging each element as you encounter it. If you ever encountered an element that was already tagged, you would know that you had already visited it and that there existed a loop in the linked list. What if you are not allowed to alter the structure of the elements of the linked list? Answer
  • 24. The following algorithm will find the loop: a) Start with two pointers ptr1 and ptr2. b) Set ptr1 and ptr2 to the head of the linked list. c) Traverse the linked list with ptr1 moving twice as fast as ptr2 (for every two elements that ptr1 advances within the list, advance ptr2 by one element). d) Stop when ptr1 reaches the end of the list, or when ptr1 = ptr2. e) If ptr1 and ptr2 are ever equal, then there must be a loop in the linked list. If the linked list has no loops, ptr1 should reach the end of the linked list ahead of ptr2 Answer
  • 25. The Operation that is not allowed in a binary search tree is a. Location Change b. Search c. Deletion d. Insertion Question No : 11
  • 27. Array is a type of ________________ data structure. a. Non Homogenous b. Non Linear c. Homogenous but not Linear d. Both Homogenous and Linear Question No : 12
  • 28. d. Both Homogenous and Linear. Answer
  • 29. The address of a node in a data structure is called a. Pointer b. Referencer c. Link d. All the above Question No : 13
  • 30. d. All the above Answer
  • 31. The minimum number of edges in a connected cycle graph on n vertices is ________ a. n b. n + 1 c. n – 1 d. 2n Question No : 14
  • 33. The total number of passes required in a selection sort is a. n + 1 b. n – 1 c. n d. n * n Question No : 15
  • 34. b. n – 1 Answer
  • 35. The node that does not have any sub trees is called ___________ a. Null Node b. Zero Node c. Leaf Node d. Empty Node Question No : 16
  • 37. Linked List is a a. Static Data Structure b. Primitive Data Structure c. Dynamic Data Structure d. None of the above Question No : 17
  • 38. c. Dynamic Data Structure Answer
  • 39. Which data structure is needed to convert infix notation to postfix notation. a. Tree b. Linear Linked List c. Stack d. Queue Question No : 18
  • 41. If every node u in Graph (G) is adjacent to every other node v in G, it is called as _____ graph. a. Directed Graph b. Complete Graph c. Connected Graph d. Multi Graph Question No : 19
  • 43. Bubble sort is an example of a. Selection sort technique b. Exchange sort technique c. Quick sort technique d. None of the options Question No : 20
  • 44. b. Exchange sort technique Answer
  • 45. How do you chose the best algorithm among available algorithms to solve a problem a. Based on space complexity b. Based on programming requirements c. Based on time complexity d. All the above Question No : 21
  • 46. d. All the above Answer
  • 47. Which of the following are called descendants? a. All the leaf nodes b. Parents, grandparents c. Root node d. Children, grandchildren Question No : 22
  • 49. Choose the limitation of an array from the below options. a. Memory Management is very poor b. Searching is slower c. Insertion and deletion are costlier d. Insertion and Deletion is not possible Question No : 23
  • 50. c. Insertion and deletion are costlier (It involves shifting rest of the elements) Answer
  • 51. Areas where stacks are popularly used are. a. Subroutines b. Expression Handling c. Recursion d. All the above Question No : 24
  • 52. d. All the above Answer
  • 53. How would you implement queue using stack(s)? Question No : 25
  • 54. Use a temp stack Data In into queue Push the element into the original stack Data Out from queue Pop all the elements from stack into a temp stack pop out the first element from the temp stack Answer
  • 55. Write a C program to compare two linked lists. Question No : 26
  • 56. int compare_linked_lists(struct node *q, struct node *r){ static int flag; if((q==NULL ) && (r==NULL)){ flag=1; } else{ if(q==NULL || r==NULL){ flag=0; } if(q->data!=r->data){ flag=0; } else{ compare_linked_lists(q- >link,r->link); } } return(flag); } Answer
  • 57. Write a C program to return the nth node from the end of a linked list. Question No : 27
  • 58. Suppose one needs to get to the 6th node from the end in the LL. First, just keep on incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in this case) STEP 1 : 1(ptr1,ptr2) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 STEP 2 : 1(ptr2) -> 2 -> 3 -> 4 -> 5 -> 6(ptr1) -> 7 -> 8 -> 9 -> 10 Now, start the second pointer (ptr2) and keep on incrementing it till the first pointer (ptr1) reaches the end of the LL. STEP 3 : 1 -> 2 -> 3 -> 4(ptr2) -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 (ptr1) So here you have the 6th node from the end pointed to by ptr2! Answer
  • 59. struct node { int data; struct node *next; }mynode; mynode * nthNode(mynode *head, int n /*pass 0 for last node*/) { mynode *ptr1,*ptr2; int count; if(!head) { return(NULL); } ptr1 = head; ptr2 = head; count = 0; Answer
  • 60. while(count < n) { count++; if((ptr1=ptr1->next)==NULL) { //Length of the linked list less than n. Error. return(NULL); } } while((ptr1=ptr1->next)!=NULL) { ptr2=ptr2->next; } return(ptr2); } Answer
  • 61. Write a C program to insert nodes into a linked list in a sorted fashion? Question No : 28
  • 62. Answer // Special case code for the head end void linkedListInsertSorted(struct node** headReference, struct node* newNode) { // Special case for the head end if (*headReference == NULL || (*headReference)->data >= newNode->data) { newNode->next = *headReference; The solution is to iterate down the list looking for the correct place to insert the new node. That could be the end of the list, or a point just before a node which is larger than the new node. Let us assume the memory for the new node has already been allocated and a pointer to that memory is being passed to this function.
  • 63. Answer *headReference = newNode; } else { // Locate the node before which the insertion is to happen! struct node* current = *headReference; while (current->next!=NULL && current->next->data < newNode->data) { current = current->next; } newNode->next = current->next; current->next = newNode; } }
  • 64. Write a C program to remove duplicates from a sorted linked list? Question No : 29
  • 65. Answer // Remove duplicates from a sorted list void RemoveDuplicates(struct node* head) { struct node* current = head; if (current == NULL) return; // do nothing if the list is empty // Compare current node with next node while(current->next!=NULL) { As the linked list is sorted, we can start from the beginning of the list and compare adjacent nodes. When adjacent nodes are the same, remove the second one. There's a tricky case where the node after the next node needs to be noted before the deletion.
  • 66. Answer if (current->data == current->next->data) { struct node* nextNext = current->next->next; free(current->next); current->next = nextNext; } else { current = current->next; // only advance if no deletion } } }
  • 67. Write a C program to find the depth or height of a tree. Question No : 30
  • 68. Answer #define max(x,y) ((x)>(y)?(x):(y)) struct Bintree { int element; struct Bintree *left; struct Bintree *right; }; typedef struct Bintree* Tree; int height(Tree T) { if(!T) return -1; else return (1 + max(height(T->left), height(T->right))) }
  • 69. Write C code to determine if two trees are identical Question No : 31
  • 70. Answer struct Bintree { int element; struct Bintree *left; struct Bintree *right; }; typedef struct Bintree* Tree; int CheckIdentical( Tree T1, Tree T2 ) { if(!T1 && !T2) // If both tree are NULL then return true return 1;
  • 71. Answer else if((!T1 && T2) || (T1 && !T2)) //If either of one is NULL, return false return 0; else return ((T1->element == T2->element) && CheckIdentical(T1->left, T2-i>left) && CheckIdentical(T1->right, T2->right)); // if element of both tree are same and left and right tree is also same then both trees are same }
  • 72. Write a C code to create a copy of a Tree Question No : 32
  • 73. Answer mynode *copy(mynode *root) { mynode *temp; if(root==NULL)return(NULL); temp = (mynode *) malloc(sizeof(mynode)); temp->value = root->value; temp->left = copy(root->left); temp->right = copy(root->right); return(temp); }
  • 74. Which of the following are called siblings a. Children of the same parent b. All nodes in the given path upto leaf node c. All nodes in a sub tree d. Children, Grand Children Question No : 33
  • 75. a. Children of the same parent Answer
  • 76. Linked List can grow and shrink in size dynamically at __________ . a. Runtime b. Compile time Question No : 34
  • 78. The postfix of A+(B*C) is a. ABC*+ b. AB+C* c. ABC+* d. +A*BC Question No : 35
  • 80. Data structure using sequential allocation is called a. Linear Data Structure b. Non-Linear Data Structure c. Non-primitive Data Structure d. Sequence Data Structure Question No : 36
  • 81. a. Linear Data Structure Answer
  • 82. A linear list in which elements can be added or removed at either end but not in the middle is known as a. Tree b. Queue c. Dequeue d. Stack Question No : 37
  • 84. The average number of key comparisons done in a successful sequential search in list of length n is a. n+1/2 b. n-1/2 c. n/2 d. log n Question No : 38
  • 86. A full binary tree with n leaves contains __________ a. nlog2n nodes b. 2^n nodes c. (2n-1) nodes d. n nodes Question No : 39
  • 88. If a node has positive outdegree and zero indegree, it is called a __________. a. Source b. Sink c. outdegree node d. indegree node Question No : 40
  • 90. The postfix notation for ((A+B)^C-(D-E)^(F+G)) is a. AB + C*DE—FG+^ b. ^-*+ABC –DE + FG c. ^+AB*C—DE^+FG d. ABC + CDE *-- FG +^ Question No : 41
  • 91. a. AB + C*DE—FG+^ Answer
  • 92. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? Question No : 42
  • 93. The heterogeneous linked list contains different data types in its nodes and we need a pointer to connect them. It is not possible to use ordinary pointers for this. So we use void pointer. Void pointer is capable of storing pointer to any type of data (eg., integer or character) as it is a generic pointer type. Answer
  • 94. What is heap sort? Question No : 43
  • 95. A Heap is an almost complete binary tree. In this tree, if the maximum level is i, then, upto the (i-1)th level should be complete. At level i, the number of nodes can be less than or equal to 2^i. If the number of nodes is less than 2^i, then the nodes in that level should be completely filled, only from left to right The property of an ascending heap is that, the root is the lowest and given any other node i, that node should be less than its left child and its right child. In a descending heap, the root should be the highest and given any other node i, that node should be greater than its left child and right child. Answer
  • 96. To sort the elements, one should create the heap first. Once the heap is created, the root has the highest value. Now we need to sort the elements in ascending order. The root can not be exchanged with the nth element so that the item in the nth position is sorted. Now, sort the remaining (n-1) elements. This can be achieved by reconstructing the heap for (n-1) elements. Answer
  • 97. heapsort() { n = array(); // Convert the tree into an array. makeheap(n); // Construct the initial heap. for(i=n; i>=2; i--) { swap(s[1],s[i]); heapsize--; keepheap(i); } } makeheap(n) { heapsize=n; for(i=n/2; i>=1; i--) keepheap(i); } keepheap(i) { l = 2*i; r = 2*i + 1; p = s[l]; q = s[r]; t = s[i]; Answer
  • 98. Answer if(l<=heapsize && p->value > t->value) largest = l; else largest = i; m = s[largest]; if(r<=heapsize && q->value > m->value) largest = r; if(largest != i) { swap(s[i], s[largest]); keepheap(largest); } }
  • 99. Implement the bubble sort algorithm. How can it be improved? Write the code for selection sort, quick sort, insertion sort. Question No : 44
  • 100. Bubble sort algorithm Answer void bubble_sort(int a[], int n) { int i, j, temp; for(j = 1; j < n; j++) { for(i = 0; i < (n - j); i++) { if(a[i] >= a[i + 1]) { //Swap a[i], a[i+1] } } } }
  • 101. void bubble_sort(int a[], int n) { int i, j, temp; int flag; for(j = 1; j < n; j++) { flag = 0; for(i = 0; i < (n - j); i++) { if(a[i] >= a[i + 1]) { //Swap a[i], a[i+1] flag = 1; } } if(flag==0)break; } } To improvise this basic algorithm, keep track of whether a particular pass results in any swap or not. If not, you can break out without wasting more cycles. Answer
  • 102. Selection Sort Algorithm void selection_sort(int a[], int n) { int i, j, small, pos, temp; for(i = 0; i < (n - 1); i++) { small = a[i]; pos = i; for(j = i + 1; j < n; j++) { if(a[j] < small) { small = a[j]; pos = j; } } temp = a[pos]; a[pos] = a[i]; a[i] = temp; } } Answer
  • 103. Quick Sort Algorithm int partition(int a[], int low, int high) { int i, j, temp, key; key = a[low]; i = low + 1; j = high; while(1) { while(i < high && key >= a[i])i++; while(key < a[j])j--; if(i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } else { temp = a[low]; a[low] = a[j]; a[j] = temp; return(j); } } } Answer
  • 104. Answer void quicksort(int a[], int low, int high) { int j; if(low < high) { j = partition(a, low, high); quicksort(a, low, j - 1); quicksort(a, j + 1, high); } } int main() { // Populate the array a quicksort(a, 0, n - 1); }
  • 105. Insertion Sort Algorithm Answer void insertion_sort(int a[], int n) { int i, j, item; for(i = 0; i < n; i++) { item = a[i]; j = i - 1; while(j >=0 && item < a[j]) { a[j + 1] = a[j]; j--; } a[j + 1] = item; } }