0% found this document useful (0 votes)
14 views11 pages

Solution of DSA Question Bank

The document provides solutions to various data structure and algorithm questions, including the differences between linear and binary search, explanations of asymptotic notations, time complexity, and operations of stack and queue ADTs. It also covers dynamic memory management, linked lists, and the significance of priority queues. Additionally, it discusses the applications of graphs, stacks, and queues, along with algorithms for deleting elements from a doubly linked list.

Uploaded by

pallavprem2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

Solution of DSA Question Bank

The document provides solutions to various data structure and algorithm questions, including the differences between linear and binary search, explanations of asymptotic notations, time complexity, and operations of stack and queue ADTs. It also covers dynamic memory management, linked lists, and the significance of priority queues. Additionally, it discusses the applications of graphs, stacks, and queues, along with algorithms for deleting elements from a doubly linked list.

Uploaded by

pallavprem2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Solution of DSA Question Bank

1 Differentiate between linear search and binary search.


Ans Linear search is a search that finds an element in the list by searching the
element sequentially until the element is found in the list. On the other hand, a
binary search is a search that finds the middle element in the list recursively until
the middle element is matched with a searched element.

The main difference between linear search and binary search is that a binary search
(also known as a half-interval search or logarithmic search) is more efficient and takes
minimum time to search an element than a linear search (or sequential search).

2 Explain asymptotic notations in detail.


Ans Asymptotic notations are the mathematical notations used to describe the running
time of an algorithm when the input tends towards a particular value or a limiting
value. For example: In bubble sort, when the input array is already sorted, the time
taken by the algorithm is linear i.e. the best case.

3 What is meant by time complexity? Define different time complexity notations.

There are two such methods used, time complexity and space complexity which are
discussed below:
Time Complexity: The time complexity of an algorithm quantifies the amount of
time taken by an algorithm to run as a function of the length of the input. Note that
the time to run is a function of the length of the input and not the actual execution
time of the machine on which the algorithm is running on.
Space Complexity: The space complexity of an algorithm quantifies the amount of
space taken by an algorithm to run as a function of the length of the input. Consider
an example: Suppose a problem to find the frequency of array elements.
In order to calculate time complexity on an algorithm, it is assumed
Asymptotic Notation is used to describe the running time of an algorithm - how much
time an algorithm takes with a given input, n. There are three different notations: big
O, big Theta (Θ), and big Omega (Ω).
4 Differentiate between LIFO & FIFO.

Ans LIFO, is a form of inventory management wherein the product or material


received last, is consumed first and thus the stock in hand, consist of earliest
consignment. On the other hand, FIFO is another method of inventory management, in
which the material received first is consumed first, i.e. the issue of goods is done from
the earliest lot and the stock in hand comprise of the latest lot.

5 Explain all operations of STACK ADT.


Ans he abstract datatype is special kind of datatype, whose behavior is defined by a
set of values and set of operations. The keyword “Abstract” is used as we can use
these datatypes, we can perform different operations. But how those operations are
working that is totally hidden from the user. The ADT is made of with primitive
datatypes, but operation logics are hidden.
Here we will see the stack ADT. These are few operations or functions of the Stack
ADT.

 isFull(), This is used to check whether stack is full or not


 isEmpry(), This is used to check whether stack is empty or not
 push(x), This is used to push x into the stack
 pop(), This is used to delete one element from top of the stack
 peek(), This is used to get the top most element of the stack
 size(), this function is used to get number of elements present into the stack

6 Explain all operations that can be performed on Queue ADT

Ans Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a
queue is open at both its ends. One end is always used to insert data (enqueue) and the
other is used to remove data (dequeue). Queue follows First-In-First-Out methodology,
i.e., the data item stored first will be accessed first.
A real-world example of queue can be a single-lane one-way road, where the vehicle
enters first, exits first. More real-world examples can be seen as queues at the ticket
windows and bus-stops.
Queue Representation

As we now understand that in queue, we access both ends for different reasons. The
following diagram given below tries to explain queue representation as data structure −

As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and
Structures. For the sake of simplicity, we shall implement queues using one-
dimensional array.

Basic Operations

Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic
operations associated with queues −
 enqueue() − add (store) an item to the queue.
 dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation
efficient. These are −
 peek() − Gets the element at the front of the queue without removing it.
 isfull() − Checks if the queue is full.
 isempty() − Checks if the queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while
enqueing (or storing) data in the queue we take help of rear pointer.
Let's first learn about supportive functions of a queue −
peek()
This function helps to see the data at the front of the queue. The algorithm of peek()
function is as follows −
Algorithm
begin procedure peek
return queue[front]
end procedure
Implementation of peek() function in C programming language −
Example
int peek() {
return queue[front];
}
isfull()
As we are using single dimension array to implement queue, we just check for the rear
pointer to reach at MAXSIZE to determine that the queue is full. In case we maintain
the queue in a circular linked-list, the algorithm will differ. Algorithm of isfull()
function −
Algorithm
begin procedure isfull

if rear equals to MAXSIZE


return true
else
return false
endif

end procedure
Implementation of isfull() function in C programming language −
Example
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
Algorithm
begin procedure isempty
if front is less than MIN OR front is greater than rear
return true
else
return false
endif

end procedure
If the value of front is less than MIN or 0, it tells that the queue is not yet initialized,
hence empty.
Here's the C programming code −
Example
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}

Enqueue Operation

Queues maintain two data pointers, front and rear. Therefore, its operations are
comparatively difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
 Step 1 − Check if the queue is full.
 Step 2 − If the queue is full, produce overflow error and exit.
 Step 3 − If the queue is not full, increment rear pointer to point the next empty
space.
 Step 4 − Add data element to the queue location, where the rear is pointing.
 Step 5 − return success.

Sometimes, we also check to see if a queue is initialized or not, to handle any


unforeseen situations.
Algorithm for enqueue operation
procedure enqueue(data)

if queue is full
return overflow
endif

rear ← rear + 1
queue[rear] ← data
return true

end procedure
Implementation of enqueue() in C programming language −
Example
int enqueue(int data)
if(isfull())
return 0;

rear = rear + 1;
queue[rear] = data;

return 1;
end procedure

Dequeue Operation

Accessing data from the queue is a process of two tasks − access the data
where front is pointing and remove the data after access. The following steps are taken
to perform dequeue operation −
 Step 1 − Check if the queue is empty.
 Step 2 − If the queue is empty, produce underflow error and exit.
 Step 3 − If the queue is not empty, access the data where front is pointing.
 Step 4 − Increment front pointer to point to the next available data element.
 Step 5 − Return success.
Algorithm for dequeue operation
procedure dequeue

if queue is empty
return underflow
end if

data = queue[front]
front ← front + 1
return true

end procedure
Implementation of dequeue() in C programming language −
Example
int dequeue() {
if(isempty())
return 0;

int data = queue[front];


front = front + 1;

return data;
}

7 What is the significance of priority queue?

Ans Priority queues are very important to systems that juggle multiple programs and
their execution (programs are chosen to run based on their priority). They are also very
important to networking systems, like the internet, because they can help prioritize
important data to make sure it gets through faste
8 Classify the applications of Graph, Stack and Queue.

Ans : Applications of Queue


 Applied as waiting lists for a single shared resource like CPU, Disk, Printer.
 Applied as buffers on MP3 players and portable CD players.
 Applied on Operating system to handle interruption.
 Applied to add song at the end or to play from the front.
 Applied on WhatsApp when we send messages to our friends and they don’t
have an internet connection then these messages is queued on the server of
WhatsApp.
 A Stack can be used for evaluating expressions consisting of operands and
operators.
 Stacks can be used for Backtracking, i.e., to check parenthesis matching in an
expression.
 It can also be used to convert one form of expression to another form.
 It can be used for systematic Memory Management.
 Graphs are used to define the flow of computation. Graphs are used to
represent networks of communication. Graphs are used to represent data
organization. Graph transformation systems work on rule-based in-memory
manipulation of graphs.

9 Which function is used to create Dynamic Memory.


Ans : Dynamic memory management involves the use of pointers and four standard
library functions, namely, malloc, calloc, realloc and free. The first three functions
are used to allocate memory, whereas the last function is used to return memory to
the system (also called freeing/deallocating memory). Representation of these
functions are malloc() , calloc() , realloc() and free() are used. These functions are
defined in the <stdlib.

10 Define the concept of Rear and Front.


Ans : The rear is the back end of something, like the rear of a school bus or the
members of the marching band who stand at the rear of the parade. At the other end of
something's front is its rear, whether it's a car or a line of people standing outside a
theater.

11 Differentiate between Single linked list and circular linked list


Ans Introduction to Singly linked list : A singly linked list is a set of nodes where each
node has two fields ‘data’ and ‘link’. The ‘data’ field stores actual piece of information
and ‘link’ field is used to point to next node. Basically the ‘link’ field stores the
address of the next node.

linkedlist

Introduction to Doubly linked list : A Doubly Linked List (DLL) contains an extra
pointer, typically called previous pointer, together with next pointer and data which are
there in singly linked list.

Singly linked list vs Doubly linked list


Singly linked list (SLL) Doubly linked list (DLL)
SLL nodes contains 2 field -data field and next link field. DLL nodes contains 3 fields
-data field, a previous link field and a next link field.
linkedlist dll
In SLL, the traversal can be done using the next node link only. Thus traversal is
possible in one direction only. In DLL, the traversal can be done using the previous
node link or the next node link. Thus traversal is possible in both directions (forward
and backward).
The SLL occupies less memory than DLL as it has only 2 fields.
The DLL occupies more memory than SLL as it has 3 fields.
Complexity of insertion and deletion at a given position is O(n). Complexity of
insertion and deletion at a given position is O(n / 2) = O(n) because traversal can be
made from start or from the end.
Complexity of deletion with a given node is O(n), because the previous node needs to
be known, and traversal takes O(n) Complexity of deletion with a given node is
O(1) because the previous node can be accessed easily
We mostly prefer to use singly linked list for the execution of stacks. We can use a
doubly linked list to execute heaps and stacks, binary trees.
When we do not need to perform any searching operation and we want to save
memory, we prefer a singly linked list. In case of better implementation, while
searching, we prefer to use doubly linked list.
A singly linked list consumes less memory as compared to the doubly linked list.The
doubly linked list consumes more memory as compared to the singly linked list.
12 Explain algorithm to delete an element from the beginning, from a specified
position & from the end of a doubly linked list.
Ans Deletion in doubly linked list at the beginning is the simplest operation. We just
need to copy the head pointer to pointer ptr and shift the head pointer to its next.
1. Ptr = head;
2. head = head → next
now make the prev of this new head node point to NULL. This will be done by using
the following statements.

1. head → prev = NULL


Now free the pointer ptr by using the free function.

1. free(ptr)

Algorithm

o STEP 1: IF HEAD = NULL

WRITEUNDERFLOW
GOTO STEP 6

o STEP 2: SET PTR = HEAD


o STEP 3: SET HEAD = HEAD → NEXT
o STEP 4: SET HEAD → PREV = NULL
o STEP 5: FREE PTR
o STEP 6: EXIT

You might also like