DS Question Bank
DS Question Bank
3RD SEMESTER
QUESTION BANK WITH SOLUTION
DATA STRUCTURES
03606201
1
DATA STRUCTURE
2
DATA STRUCTURE
3
DATA STRUCTURE
It helps arrange data elements in computer memory in a way that they could be
retrieved faster, easier, and more efficiently.
Ans:-
Ans:- Primitive Data structures are Basic data structures and are directly operated by
Machine instruction.
Non-primitive data structures are more complicated data structures and are derived
from primitive data structures. They emphasize the grouping of the same or different
data items with the relationship between each item.
o Ex:- Array, list, and Files.
Ans:-
4
DATA STRUCTURE
A data structure is said to be linear if its elements are connected in a linear fashion by
means of logically or in sequence memory locations.
Ex:- Stack and Queue
In Non – Linear Data Structures, the data items are not in sequence.
Ex:- Tree and Graph
Key Features:-
○ Unambiguous
○ Input
○ Output
○ Finiteness
○ Feasibility
○ Independent
Ans:-
The Time Complexity of an algorithm represents the amount of time required by the
algorithm to run to completion.
Ans:-
Worst-Case Analysis
The maximum time required for program execution.
Average-Case Analysis
Average time required for program execution.
Best-Case Analysis
Minimum time required for program execution.
5
DATA STRUCTURE
Q-8. What is Stack? List out the basic operations of Stack. [4 Marks]
Basic Operations
○ PUSH() – pushing(storing) an element on the stack.
○ POP() – Removing(accessing) an element from the stack.
○ Peek() – get the top data element of the stack, without removing it.
○ isFull() – Check if stack is full.
○ isEmpty() – Check if stack is empty.
Q-9. Write down the algorithm of peek(), isFull(), and isEmpty().[4 Marks]
Ans:-
peek()
Algorithm
begin procedure peek
return stack[top]
end procedure
Implementation of peek() function in C programming language −
Example
int peek() {
return stack[top];
}
isfull()
Algorithm
begin procedure isfull
end procedure
Implementation of isfull() function in C programming language −
Example
6
DATA STRUCTURE
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
isempty()
Algorithm
begin procedure isempty
end procedure
Implementation of isempty() function in C programming language is slightly different. We
initialize top at -1, as the index in array starts from 0. So we check if the top is below zero or
-1 to determine if the stack is empty. Here's the code −
Example
bool isempty() {
if(top == -1)
return true;
else
return false;
}
Ans:-
push()
begin procedure push: stack, data
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
7
DATA STRUCTURE
pop()
begin procedure pop: stack
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Ans:-
Ans:-
Infix
○ We write the expression in infix notation, e.g. a-b+c, where operators are
used in-between operands.
Prefix
○ In this expression, the operator is prefixed to operands, i.e., the operator is
written ahead of operands. For example, +ab. This is equivalent to a+b infix
notation.
Postfix
○ In this, the operator is post fixed to the operands i.e. the operator is written
after the operands. For example, ab+. This is equivalent to a+b infix notation.
Ans:- As we have discussed, it is not a very efficient way to design an algorithm or program
to parse infix notations. Instead, these infix notations are first converted into either postfix or
prefix notations and then Computed. To parse any arithmetic expression, we need to take
care of operator precedence and associativity also.
8
DATA STRUCTURE
Ans:-
Ans:- Asymptotic Notations are the expressions that are used to represent the complexity of an
algorithm.
9
DATA STRUCTURE
Ans:- Queue is an Abstract Data Structure similar to Stack. The queue is open at both ends.
One end is always used to insert data and another end is always used to remove data. It
follows FIFO(First In First Out).
Ans:-
Basic Operations
○ Enqueue() – add(store) items in the queue.
○ Dequeue() – remove(access) an item from the queue..
○ Peek() – gets the element at the front of the queue without
○ removing it.
○ Isfull() – checks if the queue isfull.
○ Isempty() – checks if the queue isempty.
Ans:-
Ans:-
Enqueue
○ procedure enqueue(data)
if queue is full
return overflow
Endif
end procedure
Dequeue
10
DATA STRUCTURE
○ procedure dequeue
if queue is empty
return underflow
end if
end procedure
Ans:- A linear queue is a linear data structure that serves the request first, which has been
arrived first. It consists of data elements that are connected in a linear fashion. It has two
pointers, i.e., front and rear, where the insertion takes place from the front end, and deletion
occurs from the front end.
In a linear queue, the traversal through the queue is possible only once ,i.e., once an element
is deleted, we cannot insert another element in its position. This disadvantage of a linear
queue is overcome by a circular queue, thus saving memory.
A 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 behaves like a circular data structure.
Ans:-
1) When are source is shared among multiple consumers. Examples include CPU
scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at the same
rateassent)between two processes. Examples include IOBuffers, pipes, file IO, etc.
3) In Operating systems:
a) Semaphores
b) FCFS ( first come first serve) scheduling, for example, FIFO queue
c) Spooling in printers
d) Buffer for devices like keyboard
4) In Networks:
11
DATA STRUCTURE
Q-24:- Difference between a linear queue and a circular queue? [3/4 Marks]
Ans:-
Insertion and The new element is added from Insertion and deletion can
deletion the rear end and removed from be done at anyplace.
the front.
12
DATA STRUCTURE
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as
a pointer. Take a look at some of the valid pointer declarations –
Int *ip; /* pointer to an integer */
Double *dp; /* pointer to a double */
Float *fp; /* pointer to a float */
Char *ch; /* pointer to a character */
member definition;
member definition;
...
member definition;
} [one or more structure variables];
o malloc()
o calloc()
o realloc()
o free()
Malloc() function
o The malloc() function allocates single block of requested memory.
o It doesn't initialize memory at execution time, so it has garbage value
initially.
o It returns NULL if memory is not sufficient.
o The syntax of malloc() function is given below:
ptr=(cast-type*)malloc(byte-size)
Calloc() function
o The calloc() function allocates multiple block of requested memory.
13
DATA STRUCTURE
Realloc() function
o If memory is not sufficient for malloc() or calloc(), you can reallocate the
memory by realloc() function. In short, it changes the memory size.
o Let's see the syntax of realloc() function.
ptr=realloc(ptr, new-size)
Free() function
o The memory occupied by malloc() or calloc() functions must be released
by calling free() function. Otherwise, it will consume memory until
program exit.
o Let's see the syntax of free() function.
free(ptr)
14
DATA STRUCTURE
newNode->data = 4;
newNode->next = head;
head = newNode;
newNode->data = 4;
newNode->next = NULL;
while(temp->next != NULL){
15
DATA STRUCTURE
temp = temp->next;
temp->next = newNode;
new Node->data = 4;
if(temp->next != NULL) {
temp = temp->next;
Ans:- We can delete either from beginning, end or from a particular position.
Delete from beginning
Point head to the second node
head = head->next;
Delete from end
Traverse to second last element.
Change its next pointer to null.
16
DATA STRUCTURE
while(temp->next->next!=NULL){
temp = temp->next;
temp->next = NULL;
Delete from Middle
if(temp->next!=NULL) {
temp = temp->next;
temp->next = temp->next->next;
Ans:- Circular linked list is a linked list where all nodes are connected to form a circle. There
is no NULL at the end. A circular linked list can be a singly circular linked list or doubly
circular linked list.
Any node can be a starting point. We can traverse the whole list by starting from
any point. We just need to stop when the first visited node is visited again.
Useful for implementation of queue. Unlike this implementation, we don’t need to
maintain two pointers for front and rear if we use circular linked list. We can
maintain a pointer to the last inserted node and front can always be obtained as next
of last.
Circular lists are useful in applications to repeatedly go around the list. For
example, when multiple applications are running on a PC, it is common for the
operating system to put the running applications on a list and then to cycle through
them, giving each of them a slice of time to execute, and then making them wait
17
DATA STRUCTURE
while the CPU is given to another application. It is convenient for the operating
system to use a circular list so that when it reaches the end of the list it can cycle
around to the front of the list.
Circular Doubly Linked Lists are used for implementation of advanced data
structures like Fibonacci Heap.
Sorting is nothing but arranging data in ascending or descending order. The term
sorting came into picture, as humans realised the importance of searching quickly.
18
DATA STRUCTURE
There are so many things in our life that we need to search for, like a particular record
in database, roll numbers in a merit list, a particular page in a book, a particular
telephone number in a dictionary, etc.
All these would have been a mess if the data was kept unordered and unsorted, but
fortunately the concept of sorting came into existence, making it easier for everyone
to arrange data in the order, hence making it easier to search.
Sorting arranged data in sequence which makes searching easier.
There are so many methods for sorting data elements which are as listed below:
o Bubble Sort
o Selection Sort
o Quick Sort
o Insertion Sort
o Merge Sort
o Radix sort
Q-33. Explain bubble sort with example and write an algorithm of bubble sort. [4
Marks]
o Bubble sort starts with very first two elements, comparing them to check which
one is greater.
o We find that 27 is smaller than 33 and these two values must be swapped.
o Next we compare 33 and 35. We find that both are in already sorted positions.
19
DATA STRUCTURE
o We know then that 10 is smaller 35. Hence they are not sorted.
o We swap these values. We find that we have reached the end of the array. After
one iteration, the array should look like this –
o To be precise, we are now showing how an array should look like after each
iteration. After the second iteration, it should look like this
o Notice that after each iteration, at least one value moves at the end.
o And when there's no swap required, bubble sorts learns that an array is
completely sorted.
Algorithm
We assume list is an array of n elements. We further assume that swap function swaps
the values of given array elements.
begin Bubble Sort(list)
return list
end BubbleSort
Q-34. Explain selection sort with example and write an algorithm of selection sort. [4
Marks]
20
DATA STRUCTURE
In selection sort algorithm, list is divided into two parts, the sorted part at the left end
and unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the list and swapped with the leftmost element,
and that element becomes part of the sorted array. This process continue moving
unsorted array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case
complexities are of Ο(n2), where n is the number of items.
Working of selection sort
o For the first position in the sorted list, the whole list is scanned
sequentially. The first position where 14 is stored presently, we search
the whole list and find that 10 is the lowest value.
o For the second position, where 33 is residing, we start scanning the rest
of the list in a linear manner.
o We find that 14 is the second lowest value in the list and it should
appear at the second place. We swap these values.
o After two iterations, two least values are positioned at the beginning in
a sorted manner.
21
DATA STRUCTURE
o The same process is applied to the rest of the items in the array.
Algorithm:
Q-35. Explain quick sort with example and write an algorithm of quick sort. [4 Marks]
22
DATA STRUCTURE
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array
of data into smaller arrays.
A large array is partitioned into two arrays one of which holds values smaller than
the specified value, say pivot, based on which the partition is made and another array
holds values greater than the pivot value.
Quick sort partitions an array and then calls itself recursively twice to sort the two
resulting sub arrays.
This algorithm is quite efficient for large-sized data sets as its average and worst-case
complexity are O(nLogn) and image.png(n2), respectively.
Let's consider an array with values {9, 7, 5, 11, 12, 2, 14, 3, 10, 6}
Below, we have a pictorial representation of how quick sort will sort the given array.
23
DATA STRUCTURE
After selecting an element as pivot, which is the last index of the array in our case,
we divide the array for the first time.
In quick sort, we call this partitioning. It is not simple breaking down of array into
2 subarrays, but in case of partitioning, the array elements are so positioned that all
the elements smaller than the pivot will be on the left side of the pivot and all the
24
DATA STRUCTURE
elements greater than the pivot will be on the right side of it. And the pivot element
will be at its final sorted position.
Algorithm
Quick Sort Pivot Algorithm
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
Merge sort is a sorting technique based on divide and conquer technique. With worst-
case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a
sorted manner.
Working of Merge Sort
25
DATA STRUCTURE
o We know that merge sort first divides the whole array iteratively into equal
halves unless the atomic values are achieved. We see here that an array of 8
items is divided into two arrays of size 4.
o This does not change the sequence of appearance of items in the original.
Now we divide these two arrays into halves.
o We further divide these arrays and we achieve atomic value which can no
more be divided.
o Now, we combine them in exactly the same manner as they were broken
down. Please note the color codes given to these lists.
o We first compare the element for each list and then combine them into
another list in a sorted manner. We see that 14 and 33 are in sorted positions.
We compare 27 and 10 and in the target list of 2 values we put 10 first,
followed by 27. We change the order of 19 and 35 whereas 42 and 44 are
placed sequentially.
o In the next iteration of the combining phase, we compare lists of two data
values, and merge them into a list of found data values placing all in a sorted
order.
o After the final merging, the list should look like this –
Algorithm:
26
DATA STRUCTURE
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Q-37. Explain insertion sort with example and write an algorithm of insertion sort. [4
Marks]
In this, a sub-list is maintained which is always sorted. For example, the lower part of
an array is maintained to be sorted.
An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate
place and then it has to be inserted there. Hence the name, insertion sort.
The array is searched sequentially and unsorted items are moved and inserted into the
sorted sub-list (in the same array).
This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2), where n is the number of items.
Working of Insertion sort
o We take an unsorted array for our example.
o It finds that both 14 and 33 are already in ascending order. For now, 14 is
in sorted sub-list.
o It swaps 33 with 27. It also checks with all the elements of sorted sub-list.
Here we see that the sorted sub-list has only one element 14, and 27 is
greater than 14. Hence, the sorted sub-list remains sorted after swapping.
27
DATA STRUCTURE
o So we swap them.
o We swap them again. By the end of third iteration, we have a sorted sub-
list of 4 items.
o This process goes on until all the unsorted values are covered in a sorted
sub-list. Now we shall see some programming aspects of insertion sort.
Algorithm:
28
DATA STRUCTURE
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
29
DATA STRUCTURE
Ans:- Data structures where data elements are not arranged sequentially or linearly are
called non-linear data structures. In a non-linear data structure, a single level is not involved.
It utilizes computer memory efficiently in comparison to a linear data structure. Its examples
are trees and graphs.
○ GeneralTree
○ BinaryTree
○ Binary SearchTree
○ AVLTree
○ BTree
○ B+Tree
A binary search tree can be defined as a class of binary trees, in which the nodes are
arranged in a specific order.
This is also called an ordered binary tree.
In a binary search tree, the value of all the nodes in the left-sub-tree is less than the
value of the root.
Similarly, the value of all the nodes in the right sub-tree is greater than or equal to the
value of the root.
30
DATA STRUCTURE
Inorder Traversal:- For binary search trees (BST), Inorder Traversal specifies the nodes in
non-descending order. In order to obtain nodes from BST in non-increasing order, a
variation of inorder traversal may be used where inorder traversal is reversed.
Preorder Traversal:- Preorder traversal will create a copy of the tree. Preorder Traversal is
also used to get the prefix expression of an expression.
Post order Traversal:- Post order traversal is used to get the postfix expression of an
expression given.
Step 1: root node of the general tree becomes the root node of a binary tree.
Step 2: Now Root Node (A) has three child Nodes (B, H, E) in the general tree. Theleftmost
node (B) of the root node (A) in the general tree becomes the leftmost node of the root node
(A) in a binary tree.
Step 3: Now Node H becomes the right node of B and Node E becomes the right node of H.
Step 4: Now Node B has only one left child node, which is C in the general tree. So Node C
becomes the left child of Node B in the binary tree.
31
DATA STRUCTURE
Step 5: Now Node E has two child nodes (F, G). The leftmost node (F) of the node
(E) in the general tree becomes the leftmost node of node E in the binary tree.
Step 6: Now Node G becomes the right node of Node F in the binary tree.
32