mcs21 - Notes
mcs21 - Notes
tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: A data structure is an arrangement of data in a computer’s memory or even disk storage. It has a different
way of storing and organizing data in a computer so that it can used efficiently. Different kind of Data structure
is used in different application. For example, B-tree is widely used in implementation of databases.
Ans: Data structure where data elements are arranged sequentially or linearly where the elements are attached
to its previous and next adjacent in what is called a linear data structure. In linear data structure, single level is
involved. Therefore, we can traverse all the elements in single run only. Linear data structures are easy to
implement because computer memory is arranged in a linear way. Its examples are array, stack, queue, linked
list, etc.
Ans: Non Linear Data Structures are constructed by attaching a data element to several other data elements in
such a way that it reflects a specific relationship among them For Example: – N-dimensional Array, Trees and
Graphs
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Arrays
Q.1 What is an Array?
Ans: An Array can be defined as a data structure having fixed size sequenced collection of elements of same
data type and which can be referenced through same variable name. An Array is a name given to a group of
similar quantities.
Declaring an array
Type variable_name [length of array];
For example:-
double height[10];
float width[20];
int min[9];
int myArray[5] = {1, 2, 3, 4, 5}; //declare and initialize the array in one statement
In C language array starts at position 0. C language treats array name as the pointer to the first element and an
item in the array can be accessed through its index.
Ans:
#include<stdio.h>
void main ()
{
int array[10], n;
// Reading an array
printf(“Enter size of an array”);
scanf ("%d", &n);
printf(“Enter array elements”);
for (i=0;i<n;i++)
scanf ("%d", &array[i]);
// writing an array
printf(“array elements are”);
for (i=0;i<n;i++)
printf ("%d", array[i]);
}
Ans: To insert a value into an array, you need to shift every value after the location you want to insert. The
logic is very simple: start from the end and copy the previous item to the current element until you hit the
location you want to insert.
Ans:
// Reading an array
printf (“Enter size of an array”);
scanf ("%d", &n);
printf (“Enter array elements”);
for (i=0;i<n;i++)
scanf ("%d", &array[i]);
// writing an array
printf (“array elements are”);
for (i=0;i<n;i++)
printf ("%d", array[i]);
}
Ans: To delete a value from an array, you need to shift every value after the location you want to delete. The
logic is very simple: start from the position you want to delete and copy the previous item to the current element
until end of the array.
Q.6 Write code to delete data from an array from a particular position?
Ans:
// Reading an array
printf(“Enter size of an array”);
scanf ("%d", &n);
printf(“Enter array elements”);
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
for (i=0;i<n;i++)
scanf ("%d", &array[i]);
// writing an array
printf(“array elements are”);
for (i=0;i<n;i++)
printf ("%d", array[i]);
}
Ans: Assume, that both arrays are sorted in ascending order and we want resulting array to maintain the same
order. Algorithm to merge two arrays A[0..m-1] and B[0..n-1] into an array C[0..m+n-1] is as following:
• Introduce read-indices i , j to traverse arrays A and B, accordingly. Introduce write-index k to store position of
the first free cell in the resulting array. By default i = j = k = 0.
• At each step: if both indices are in range ( i < m and j < n), choose minimum of (A[ i ], B[ j ]) and write it to C[ k ].
Otherwise go to step 4.
• Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.
• Copy the rest values from the array, which index is still in range, to the resulting array.
Ans: Two-dimensional arrays, the most common multidimensional arrays, are used to store information that we
normally represent in table form. Two-dimensional arrays, like one-dimensional arrays, are homogeneous. This
means that all of the data in a two-dimensional array is of the same type. The two-dimensional array may also
be visualized as a one-dimensional array of arrays. Examples of applications involving two-dimensional arrays
include:
//declaration
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Searching
Q .1 How to do Linear Search in an Array?
Ans: Linear search or Sequential is the simplest search algorithm for finding a particular value in an array that
consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.
Ans:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: A Binary Search or Half-interval search algorithm finds the position of a specified value (the input “key”)
within a sorted array. In each step, the algorithm compares the input key value with the key value of the middle
element of the array. If the keys match, then a matching element has been found so its index, or position, is
returned. Otherwise, if the sought key is less than the middle element’s key, then the algorithm repeats its action
on the sub-array to the left of the middle element or, if the input key is greater, on the sub-array to the right. If
the remaining array to be searched is reduced to zero, then the key cannot be found in the array and a special
“Not found” indication is returned.
Ans:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Stack
Q1. What is Stack ?
Ans: Stack is a linear data structure which follows a particular order in which the operations are performed. The
order may be LIFO(Last In First Out) or FILO(First In Last Out).
Ans:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: Stack follows LIFO. Thus the item that is first entered would be the last removed. In array the items can
be entered or removed in any order. Basically each member access is done using index and no strict order is to
be followed here to remove a particular element .Array may be multi-dimensional or one dimensional but stack
should be one-dimensional. Size of array is fixed, while stack can be grow or shrink. We can say stack is
dynamic data structure.
3. Towers of Hanoi
4. Function Calls
5. 8 Queen Problem.
Queue
Q1. What is Queue?
Ans: A Queue is a linear structure which follows a particular order in which the operations are performed. The
order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where
the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack
we remove the item the most recently added; in a queue, we remove the item the least recently added.
Ans:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans. A circular queue is one in which the insertion of new element is done at the very first location of the queue
if the last location of the queue is full. Suppose if we have a Queue of n elements then after adding the element
at the last index i.e. (n-1) th , as queue is starting with 0 index, the next element will be inserted at the very first
location of the queue which was not possible in the simple linear queue. That’s why linear queue leads to
wastage of the memory, and this flaw of linear queue is overcome by circular queue. So, in all we can say that
the circular queue is a queue in which first element come right after the last element that means a circular queue
has a starting point but no end point.
Ans: A Priority Queue is a collection of elements such that each element has been assigned a priority and such
that the order in which each elements are deleted and processed comes from the following rules:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
This algorithm adds an item with priority number M to a priority Queue maintained by a 2-dimensional array
Queue.
This algorithm deleted and processes the first element in a priority queue maintained by a 2-dimensional array
Queue
Sorting
Q.1 What is Sorting?
Ans: Sorting refers to ordering data in an increasing or decreasing fashion according to some linear relationship
among the data items.
Ans:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: Bubble sort , often incorrectly referred to as sinking sort , is a simple sorting algorithm that works by
repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if
they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that
the list is sorted. The algorithm gets its name from the way smaller elements “bubble” to the top of the list.
Because it only uses comparisons to operate on elements, it is a comparison sort.
For Example: Let us take the array of numbers “5 1 4 2 8”, and sort the array from lowest number to greatest
number using bubble sort algorithm. In each step, elements written in bold are being compared. Three passes
will be required.
First Pass:
( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap
them.
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Second Pass:
(14258)→(14258)
( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2
(12458)→(12458)
( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) Now, the array is already sorted, but our algorithm does not know if it is completed.
The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
Ans:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans:
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:
1. Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value, which is
in range of sorted values, even if it doesn’t present in the array.
2. Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part
of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot
can stay in any part of the array. Notice, that array may be divided in non-equal parts.
3. Sort both parts. Apply quicksort algorithm recursively to the left and the right parts.
Partition algorithm in detail: There are two indices i and j and at the very beginning of the partition algorithm
i points to the first element in the array and j points to the last one. Then algorithm moves i forward, until an
element with value greater or equal to the pivot is found. Index j is moved backward, until an element with
value lesser or equal to the pivot is found. If i ≤ j then they are swapped and i steps to the next position ( i + 1 ),
j steps to the previous one (j – 1) . Algorithm stops, when i becomes greater than j . After partition, all values
before i-th element are less or equal than the pivot and all values after j-th element are greater or equal to the
pivot.
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Linked List
Q.1 Write an algorithm to search an element in a linear linked list?
Ans: Here START is a pointer variable which contains the address of first node. ITEM is the value to be
searched.
Trees
Q.1 Explain binary search tree?
Ans: A tree is a finite set of one or more nodes such that:-There is a specially designated node called the root.
The remaining nodes are partitioned into n>=0 disjoint sets T1, …, Tn, where each of these sets is a tree. We
call T1, …, Tn the subtrees of the root.
Binary Trees
1. A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called
the left subtree and the right subtree.
2. Any tree can be transformed into binary tree.by left child-right.
3. The left subtree and the right subtree are distinguished.
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: A data structure is a way of organizing data that considers not only the items stored, but also their
relationship to each other. Advance knowledge about the relationship between data items allows designing of
efficient algorithms for the manipulation of data.
Q.2 List out the areas in which data structures are applied extensively?
Ans: Compiler Design, Operating System, Database Management System, Statistical analysis package,
Numerical Analysis, Graphics, Artificial Intelligence, Simulation
Q.3 What are the major data structures used in the following areas: RDBMS, Network data model &
Hierarchical data model.
Ans:
Q.4 If you are using C language to implement the heterogeneous linked list, what pointer type will you
use?
Ans: The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to
connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is
capable of storing pointer to any type as it is a generic pointer type.
Ans: Two. One queue is used for actual storing of data and another for storing priorities.
Ans: Stack. Because of its LIFO (Last In First Out) property it remembers its “caller” so knows whom to return
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.
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: Straight merging, Natural merging, Polyphase sort, Distribution of Initial runs.
Ans:
Q.9 List out few of the applications that make use of Multilinked Structures?
Q.11 What is the type of the algorithm used in solving the 8 Queens problem?
Ans: Backtracking
Ans: If the “pivotal value” (or the “Height factor”) is greater than 1 or less than –1.
Q.13 What is the bucket size, when the overlapping and collision occur at same time?
Ans: One. If there is only one entry possible in the bucket, when the collision occurs, there is no way to
accommodate the colliding value. This results in the overlapping of values.
Q.14 There are 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could have formed a full
binary tree?
Ans: 15.
In general: There are 2n -1 nodes in a full binary tree.
By the method of elimination: Full binary trees contain odd number of nodes. So there cannot be full binary
trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete binary tree but not a full binary
tree. So the correct answer is 15.
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Note Full and Complete binary trees are different. All full binary trees are complete binary trees but not vice
versa.
Q.15 Classify the Hashing Functions based on the various methods by which the key value is found.
Ans:
• Direct method
• Subtraction method
• Modulo-Division method
• Digit-Extraction method
• Mid-Square method
• Folding method,
• Pseudo-random method
Q.16 What are the types of Collision Resolution Techniques and the methods used in each of the type?
Ans:
Q.17 In RDBMS, what is the efficient data structure used in the internal storage representation?
Ans: B+ tree. Because in B+ tree, all the data is stored only in leaf nodes, that makes searching easier. This
corresponds to the records that shall be stored in leaf nodes.
Q.18 Of the following tree structure, which is, efficient considering space and time complexities?
(a) Incomplete Binary Tree
(b) Complete Binary Tree
(c) Full Binary Tree
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
read and refer the official study material provided by the university.
• Facebook-https://www.facebook.com/dalal.tech
• telegram - https://t.me/DalalTechnologies
• YouTube-https://www.youtube.com/channel/UCilEr1rW-SIrJlJ5_ioKQfw
Ans: A spanning tree is a tree associated with a network. All the nodes of the graph appear on the tree once. A
minimum spanning tree is a spanning tree organized so that the total edge weight between nodes is minimized.
Q.20 Does the minimum spanning tree of a graph give the shortest distance between any 2 specified
nodes?
Ans: No.Minimal spanning tree assures that the total weight of the tree is kept at its minimum. But it doesn’t
mean that the distance between any two nodes involved in the minimum-spanning tree is minimum.
Disclaimer/Note
These are just the sample of the answers/solution to some of the questions given in the assignments. Student should
Best PDF Encryption Reviews read and refer the official study material provided by the university.