Question Bank With Solution
Question Bank With Solution
Ans:
Examples: Linked List Concatenation
As shown in above fig. We can perform further operations on previous
versions of linked lists after the concatenation.
characters, compute the address of element of stored at 4th row and 3rd
column. (Say if the array is alpha [5][5], then find the address of alpha [4][3]).
i) Data
Answer:
Data Type: The data type is the form of a variable to which a value can be
assigned. It defines that the particular variable will assign the values of the
given data type only. It can hold value but not data. Therefore, it is dataless.
In the case of data types, the value of data is not stored because it only
represents the type of data that can be stored. Data type examples are int, float,
double, etc.
Q.4 ) Write different types of data structures. Give one example of each type.
Arrays:
An array is a linear data structure and it is a collection of items stored
at contiguous memory locations. The idea is to store multiple items of
the same type together in one place. It allows the processing of a large
amount of data in a relatively short period. The first element of the
array is indexed by a subscript of 0. There are different operations
possible in an array, like Searching, Sorting, Inserting, Traversing,
Reversing, and Deleting.
inked list:
A linked list is a linear data structure in which elements are not stored at
contiguous memory locations. The elements in a linked list are linked
using pointers as shown in the below image:
● Singly-linked list
Stack:
Stack is a linear data structure that follows a particular order in which the
operations are performed. The order is LIFO(Last in first out). Entering and
retrieving data is possible from only one end. The entering and retrieving
of data is also called push and pop operation in a stack. There are different
operations possible in a stack like reversing a stack using recursion,
Sorting, Deleting the middle element of a stack, etc.
Queue:
Queue is a linear data structure that follows a particular order in which the
operations are performed. The order is First In First Out(FIFO) i.e. the data
item stored first will be accessed first. In this, entering and retrieving data
is not done from only one end. An example of a queue is any queue of
consumers for a resource where the consumer that came first is served
first. Different operations are performed on a Queue like Reversing a
Queue (with or without using recursion), Reversing the first K elements of
a Queue, etc. A few basic operations performed In Queue are enqueue,
dequeue, front, rear, etc.
Tree:
A tree is a non-linear and hierarchical data structure where the elements
are arranged in a tree-like structure. In a tree, the topmost node is called
the root node. Each node contains some data, and data can be of any type.
It consists of a central node, structural nodes, and sub-nodes which are
connected via edges. Different tree data structures allow quicker and
easier access to the data as it is a non-linear data structure. A tree has
various terminologies like Node, Root, Edge, Height of a tree, Degree of a
tree, etc.
Graph:
A graph is a non-linear data structure that consists of vertices (or nodes)
and edges. It consists of a finite set of vertices and set of edges that
connect a pair of nodes. The graph is used to solve the most challenging
and complex programming problems. It has different terminologies which
are Path, Degree, Adjacent vertices, Connected components, etc.
To analyze the time complexity of an algorithm that finds the union of two sets of
length m and n, we need to consider a common approach and its complexity. Here,
I'll outline a typical approach and analyze its complexity:
Suppose A is set of m elements and B is set of n elements and C is set where union
on A and B elements are stored.
for(i=0;i<m;i++)
{
c[i]=a[i];
for(j=0;j<n;j++)
c[i]=b[j];
i++;
Linked lists and arrays are both fundamental data structures with distinct
advantages and use cases. Here are some key advantages of linked lists over
arrays:
1. Dynamic Size
2. Efficient Insertions/Deletions
3. Memory Utilization
● Linked List: Allocates memory dynamically as needed for each node,
which can be more efficient in scenarios with varying amounts of data.
● Array: Allocates a contiguous block of memory, which can lead to
wasted space if the array is not fully utilized, or may require expensive
resizing operations if the array needs to grow beyond its initial capacity.
Linear search and binary search are both algorithms used to find a target
value in a collection of elements. They differ significantly in their approach,
efficiency, and use cases. Here’s an explanation of each with examples to
illustrate the differences:
Linear Search
Steps:
Time Complexity: O(n) in the worst case, where n is the number of elements.
This is because you might need to check each element in the list.
Binary Search
Steps:
1. Start: Begin with the entire collection and find the middle element.
2. Compare: Compare the middle element with the target value.
3. Narrow Down:
○ If the target is equal to the middle element, return the index.
○ If the target is less than the middle element, narrow the search to
the lower half.
○ If the target is greater than the middle element, narrow the search
to the upper half.
4. Repeat: Continue this process until you either find the target or the
interval is empty.
1. Efficiency:
○ Linear Search: Less efficient with a time complexity of O(n). It can
be used on unsorted data.
○ Binary Search: More efficient with a time complexity of O(log n),
but requires sorted data.
2. Use Cases:
○ Linear Search: Suitable for small or unsorted lists, or when
sorting the list is impractical.
○ Binary Search: Suitable for large, sorted lists where quick search
performance is necessary.
3. Preconditions:
○ Linear Search: No special preconditions (works on unsorted
data).
○ Binary Search: Requires that the data be sorted before the search.
Q. 2) Sort following list using bubble sort, show the output of each pass.
10, 5, 4, 18, 17, 1, 2.
Answer:
Bubble sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order.
The pass through the list is repeated until the list is sorted.
Let's sort the list [10, 5, 4, 18, 17, 1, 2] using bubble sort and show
the output of each pass.
Initial List
[10, 5, 4, 18, 17, 1, 2]
Pass 1:
Pass 3:
Pass 4:
Pass 5:
Pass 6:
The list is fully sorted after 6 passes, which corresponds to the length of the
list minus one.
Answer:
i) Insertion Sort
Insertion Sort is a simple and intuitive sorting algorithm that builds the final
sorted array one item at a time. It is efficient for small data sets or nearly
sorted data but can be less efficient for larger lists.
Real-World Applications
Q. 5) Sort the following list using merge sort 4 38, 27, 43, 3, 9, 82, 11, 10
Answer: following shows how the merge sort and merge function
call gets called:
Step 1: Divide
First, we divide the list into two halves until we have individual
elements:
Step 2: Conquer
else:
// Target found
return i