Define Data Structure 2023 Question Paper With Solution
Define Data Structure 2023 Question Paper With Solution
1. Insertion: This operation involves adding a new element to the data structure at a specific position.
Insertion can be performed in different data structures like arrays, linked lists, trees, and hash tables.
2. Deletion: This operation involves removing an existing element from the data structure. Deletion
can also be performed in various data structures such as arrays, linked lists, trees, and hash tables.
The process of deletion may involve reorganizing the remaining elements to maintain the structure's
integrity.
Array ADT:
1. Data:
- A collection of elements, where each element is stored at a specific index.
- The size of the array, which represents the total number of elements it can store.
- The length of the array, which is the current number of elements in the array.
2. Operations:
- Create(size): Create a new array with the specified size.
- Get(index): Retrieve the element at the specified index in the array.
- Set(index, value): Update the element at the specified index with the given value.
- Insert(index, value): Insert a new element with the given value at the specified index. This
may involve shifting elements to make room for the new value.
- Delete(index): Remove the element at the specified index from the array. This may involve
shifting elements to fill the gap created by the deletion.
- Length(): Return the current length of the array (i.e., the number of elements in the
array).
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
- Resize(newSize): Change the size of the array to the specified newSize, which can be
larger or smaller than the current size. This may involve allocating new memory or
deallocating unused memory.
- Traverse(): Perform an operation on each element in the array, such as printing or
processing the elements.
- Search(value): Find the index of the first occurrence of a given value in the array, or return
-1 if the value is not found.
Note that the specific implementation of these operations may vary depending on the
programming language or data structure library being used.
Diagram:
A simple queue has two pointers, front and rear, to indicate the beginning and end of the
queue. Elements are enqueued at the rear and dequeued from the front. Once the front
reaches the end of the queue's capacity, even if there are empty spaces in the front, it
cannot insert more elements unless the entire queue is shifted.
2. Circular Queue:
Diagram:
A circular queue is an improvement over a simple queue. It treats the front and rear as
adjacent, allowing the rear pointer to wrap around to the beginning when it reaches the end
of the queue's capacity. This eliminates the need for shifting elements and makes better use
of available space.
There are also variations of these basic queue types, such as:
1. Priority Queue:
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
Elements in a priority queue are assigned a priority, and elements with higher priority are
dequeued before elements with lower priority.
3. Multilevel Queue:
A multilevel queue consists of multiple simple or circular queues, each with its own priority
or purpose. Elements are enqueued and dequeued based on the priority of their respective
queues.
Depth-First Traversal:
4. Level-Order Traversal:
In level-order traversal, nodes are visited level by level, starting from the root. Nodes in the
same level are visited from left to right. This traversal method uses a queue to keep track of
the nodes at each level.
These traversal methods are mainly used for searching, inserting, or deleting nodes in a tree,
as well as for other operations like printing the tree nodes in a specific order. Each traversal
method has its own use case and can be applied depending on the specific requirements of a
problem or algorithm.
A B-Tree (short for Balanced Tree) is a self-balancing tree data structure that
maintains a sorted order of its elements and allows for efficient insertion, deletion,
and search operations. B-Trees are commonly used in database systems and file
systems to organize and manage large amounts of data.
1. Each node in the B-Tree contains a certain number of keys (elements) and child
pointers, with the keys sorted in ascending order.
2. All the leaves (nodes with no children) appear at the same level, ensuring that the
tree remains balanced.
3. A B-Tree has a minimum degree 't' (t ≥ 2), which determines the minimum and
maximum number of keys and children a node can have:
- Every node, except the root, must have at least (t - 1) keys and t children.
- Every node can have at most (2t - 1) keys and 2t children.
- The root node can have a minimum of 1 key and a maximum of (2t - 1) keys.
Operations on a B-Tree:
1. Search: Searches for a specific key in the B-Tree by traversing the tree from the
root to the appropriate leaf node based on the key's value. The search operation
takes O(log n) time complexity.
2. Insert: Inserts a new key into the B-Tree while maintaining the tree's properties. If
the node where the key should be inserted becomes full (has 2t - 1 keys), the node is
split, and the middle key is moved up to the parent node. This process may be
repeated up to the root node. The insert operation takes O(log n) time complexity.
3. Delete: Removes a key from the B-Tree while maintaining the tree's properties.
Depending on the key's location, the delete operation may involve redistributing keys
or merging nodes to ensure that all nodes (except the root) have at least (t - 1) keys.
The delete operation takes O(log n) time complexity.
4. Traverse: Visits all the keys in the B-Tree in a sorted order, typically using an
inorder traversal method.
B-Trees are particularly useful in situations where the data is too large to fit in main
memory and needs to be stored on secondary storage devices (e.g., hard disks). Their
balanced structure and efficient operations help minimize disk I/O operations and
provide fast access to the stored data.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
In separate chaining, each slot in the hash table contains a linked list (or
another data structure) to store the keys that collide with each other.
When a collision occurs, the new key is simply added to the linked list at
the appropriate slot.
To search for a key in the hash table, the hash function is applied to the
key to determine the slot, and then the linked list at that slot is traversed
to find the desired key.
Pros:
- It can handle an arbitrary number of collisions per slot.
- The load factor can exceed 1, meaning the number of keys can be
greater than the size of the hash table.
Cons:
- The performance can degrade if the linked lists become too long, as
searching through the list takes linear time.
- It requires additional memory to store the linked lists.
In open addressing, all keys are stored directly in the hash table itself,
and collisions are resolved by finding another available slot in the table.
When a collision occurs, a probing sequence is used to determine the
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
next available slot. There are several probing techniques, such as linear
probing, quadratic probing, and double hashing.
Pros:
- It does not require additional memory to store linked lists or other data
structures.
- It can provide better cache performance since all keys are stored in the
hash table itself.
Cons:
- The load factor should be kept below 1 to ensure that an empty slot
can always be found.
- The performance can degrade if the hash table becomes too full, as the
probing sequence may need to search through many slots to find an
available slot or the desired key.
Both separate chaining and open addressing have their advantages and
disadvantages, and the choice of collision resolution technique depends
on the specific requirements of the application and the expected
distribution of keys.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
SECTION _ B
Answer any four questions. Each question carries 5 marks.
Time Complexity:
Time complexity is often expressed using big O notation, which describes the
upper bound of an algorithm's growth rate. For example, O(n) represents linear
time complexity, meaning the algorithm's running time increases linearly with
the input size, while O(n^2) represents quadratic time complexity, meaning the
running time increases quadratically with the input size.
Space Complexity:
efficiency of different algorithms for the same problem or task, with lower
space complexity generally indicating a more memory-efficient algorithm.
Space complexity is also often expressed using big O notation, which describes
the upper bound of an algorithm's memory usage. For example, O(1)
represents constant space complexity, meaning the algorithm uses a fixed
amount of memory regardless of the input size, while O(n) represents linear
space complexity, meaning the algorithm's memory usage increases linearly
with the input size.
So, the prefix notation for the given infix expression is: Q/+AB*CD
1. Search: Searching in an AVL tree is the same as searching in a regular binary search
tree. Starting from the root, the search compares the target value with the current
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
node's value and moves left or right based on the comparison. The search operation
has a time complexity of O(log n) due to the balanced nature of the AVL tree.
2. Insertion: When inserting a new node into an AVL tree, the process starts by
inserting the node as in a regular binary search tree. After the insertion, the tree's
balance may be violated, so the tree needs to be rebalanced. This rebalancing is
done using rotations:
- Single rotation (left or right): If the balance is disturbed in one direction (left or
right), a single rotation can be used to restore balance.
- Double rotation (left-right or right-left): If the balance is disturbed in a zigzag
pattern, a double rotation is required to restore balance.
The insertion operation has a time complexity of O(log n), which includes both the
initial insertion and the rebalancing process.
3. Deletion: Deleting a node from an AVL tree involves finding the node, removing it
as in a regular binary search tree, and then rebalancing the tree if necessary. Similar
to insertion, the rebalancing process may involve single or double rotations. The
deletion operation has a time complexity of O(log n).
4. Traversal: AVL trees can be traversed using various tree traversal techniques, such
as inorder, preorder, and postorder traversal. The traversal operation has a time
complexity of O(n) as it visits all n nodes in the tree.
The depth-first search or DFS algorithm traverses or explores data structures, such
as trees and graphs. The algorithm starts at the root node (in the case of a graph, you can use
any random node as the root node) and examines each branch as far as possible before
backtracking.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
When a dead-end occurs in any iteration, the Depth First Search (DFS) method traverses a
network in a deathward motion and uses a stack data structure to remember to acquire the
next vertex to start a search.
The outcome of a DFS traversal of a graph is a spanning tree. A spanning tree is a graph that
is devoid of loops. To implement DFS traversal, you need to utilize a stack data structure
with a maximum size equal to the total number of vertices in the graph.
Step 1: Create a stack with the total number of vertices in the graph as the size.
Step 2: Choose any vertex as the traversal's beginning point. Push a visit to that vertex and
add it to the stack.
Step 3 - Push any non-visited adjacent vertices of a vertex at the top of the stack to the top of
the stack.
Step 4 - Repeat steps 3 and 4 until there are no more vertices to visit from the vertex at the
top of the stack.
Step 5 - If there are no new vertices to visit, go back and pop one from the stack using
backtracking.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
Step 7 - When the stack is entirely unoccupied, create the final spanning tree by deleting the
graph's unused edges.
Consider the following graph as an example of how to use the dfs algorithm.
Step 3: From vertex C and D, visit any adjacent unvisited vertices of vertex B. Imagine you
have chosen vertex C, and you want to make C a visited vertex.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
Step 4: You can visit any nearby unvisited vertices of vertex C, you need to select vertex D
and designate it as a visited vertex.
Step 5: Vertex E is the lone unvisited adjacent vertex of vertex D, thus marking it as visited.
Step 6: Vertex E's nearby vertices, namely vertex C and D have been visited, pop vertex E
from the stack.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
Step 7: Now that all of vertex D's nearby vertices, namely vertex B and C, have been visited,
pop vertex D from the stack.
Step 8: Similarly, vertex C's adjacent vertices have already been visited; therefore, pop it
from the stack.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
Step 9: There is no more unvisited adjacent vertex of b, thus pop it from the stack.
Step 10: All of the nearby vertices of Vertex A, B, and C, have already been visited, so pop
vertex A from the stack as well.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
The logic is simple, we start from the leftmost element and keep track of
the index of smaller (or equal) elements as i. While traversing, if we find
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
1. Big O notation (O): This notation represents the upper bound of a function,
which means it describes the maximum growth rate of the function. It is used to give
an upper limit on the running time of an algorithm in the worst case, thus providing
a guarantee that the algorithm will not perform worse than the given big O
notation. Example: The time complexity of a linear search algorithm is O(n), which
means that the time it takes to search for an element in a list grows linearly with the
size of the list.
2. Big Omega notation (Ω): This notation represents the lower bound of a function,
which means it describes the minimum growth rate of the function. It is used to give
a lower limit on the running time of an algorithm in the best case, thus providing a
guarantee that the algorithm will not perform better than the given big Omega
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
notation. Example: The time complexity of a binary search algorithm is Ω(log n),
which means that the time it takes to search for an element in a sorted list grows
logarithmically with the size of the list.
3. Big Theta notation (Θ): This notation represents the tight bound of a function,
which means it describes the exact growth rate of the function. It is used when the
algorithm's growth rate is both upper and lower bounded by the same function, thus
providing an accurate estimate of the algorithm's efficiency. Example: The time
complexity of the merge sort algorithm is Θ(n log n), which means that the time it
takes to sort a list grows linearithmically (linearly and logarithmically) with the
size of the list.
In summary, asymptotic notation helps us describe and compare the growth rates of
functions, which is especially useful for analyzing the efficiency of algorithms in
terms of their running time or space complexity.
s
A sparse matrix is a matrix in which the majority of its elements are zero. Here's a
C program to display a sparse matrix and its transpose:
#include <stdio.h>
int main() {
int m, n, count = 0;
int matrix[m][n];
scanf("%d", &matrix[i][j]);
if (matrix[i][j] != 0) {
count++;
}
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
int sparse[count][3], k = 0;
if (matrix[i][j] != 0) {
sparse[k][0] = i;
sparse[k][1] = j;
sparse[k][2] = matrix[i][j];
k++;
printf("Sparse Matrix:\n");
printf("\n");
int transpose[count][3];
transpose[i][0] = sparse[i][1];
transpose[i][1] = sparse[i][0];
transpose[i][2] = sparse[i][2];
printf("\n");
return 0;
```
This program first reads the dimensions and elements of a matrix, then converts it
into a sparse matrix representation (using a 2D array with three columns: row,
column, and value). It then calculates the transpose of the sparse matrix by
swapping the row and column values and displays both the sparse matrix and its
transpose.
a.solution.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
2. Index: A unique identifier associated with each element in the array. In most
programming languages, indices start from 0, meaning that the first element has
an index of 0, the second element has an index of 1, and so on.
3. Length or size: The total number of elements in the array. The length is a fixed
property of the array and cannot be changed once the array is created.
Arrays can be classified based on their dimensions and the type of elements they
store:
1. One-dimensional arrays (1D arrays): These are simple, linear arrays that
contain a single row or column of elements. Each element in a 1D array can be
accessed using a single index.
2. Multidimensional arrays: These are arrays that contain more than one row or
column of elements. They can be thought of as arrays of arrays. The most
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
3. Homogeneous arrays: These are arrays that store elements of the same data
type. All elements in a homogeneous array must be of the same type, such as
integers, floating-point numbers, or characters.
4. Heterogeneous arrays: These are arrays that store elements of different data
types. Heterogeneous arrays are typically implemented using structures or classes
in programming languages that support them.
In summary, arrays are fundamental data structures used to store and organize
collections of elements. They can be classified based on their dimensions (1D or
multidimensional) and the type of elements they store (homogeneous or
heterogeneous). Arrays are characterized by their elements, indices, length or
size, and memory allocation.
B.solution.
Here's an algorithm to insert an element (2A) at position 2 in
the given array A = {10, 30, 40, 50}:
1. Initialize the given array A with elements {10, 30, 40, 50}.
2. Set the element to insert, element = 2 * A[1] (since position 2
has an index of 1).
3. Set the position to insert, position = 2.
4. Determine the length of the array, length = 4 (the number of
elements in the array A).
5. Create a new array B with a length equal to length + 1.
6. Set index = 0.
7. For each element in array A, do the following:
a. If index is less than the position, copy the element from
array A to array B at the same index.
b. If index is equal to the position, insert the element at this
index in array B.
c. If index is greater than the position, copy the element from
array A to array B at index + 1.
d. Increment the index.
8. Array B now contains the original elements with the new
element inserted at the specified position.
1. Push operation: This operation adds an element to the top of the stack. If the stack has a
fixed size, the push operation first checks whether the stack is full. If the stack is full, it
returns an error or a message indicating that the stack is full. If the stack is not full, the new
element is added to the top of the stack, and the top pointer is updated.
Example: Let's say we have a stack with elements [10, 20, 30] and we want to push the
element 40 onto the stack. After performing the push operation, the stack will become [10,
20, 30, 40].
2. Pop operation: This operation removes the top element from the stack and returns its
value. If the stack has a fixed size, the pop operation first checks whether the stack is empty.
If the stack is empty, it returns an error or a message indicating that the stack is empty. If the
stack is not empty, the top element is removed from the stack, and the top pointer is
updated.
Example: Let's say we have a stack with elements [10, 20, 30, 40] and we want to perform
the pop operation. After performing the pop operation, the stack will become [10, 20, 30],
and the removed element (40) will be returned.
In summary, a stack is a LIFO data structure with two main operations: push and pop. The
push operation adds an element to the top of the stack, while the pop operation removes
the top element from the stack and returns its value. Stacks can be implemented using arrays
or linked lists and are commonly used for various applications, such as parsing expressions,
managing function calls, and implementing undo/redo functionality in software.
```
function bubbleSort(list):
n = length of list
repeat
swapped = false
for i = 1 to n-1
if list[i-1] > list[i]
swap(list[i-1], list[i])
swapped = true
until not swapped
```
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
In this algorithm, the outer loop (repeat-until) iterates until no more swaps are
needed, and the inner loop (for) iterates through the list comparing and swapping
adjacent elements. The bubble sort algorithm has a worst-case time complexity of
O(n^2), where n is the number of elements in the list, but it can perform better
(O(n)) on already sorted or nearly sorted lists.
B.solution.
Here's a step-by-step process of sorting the given elements using bubble sort:
1. Pass 1:
- Compare 38 and 47, no swap needed.
- Compare 47 and 24, swap them: [38, 24, 47, 42, 1]
- Compare 47 and 42, swap them: [38, 24, 42, 47, 1]
- Compare 47 and 1, swap them: [38, 24, 42, 1, 47]
2. Pass 2:
- Compare 38 and 24, swap them: [24, 38, 42, 1, 47]
- Compare 38 and 42, no swap needed.
- Compare 42 and 1, swap them: [24, 38, 1, 42, 47]
3. Pass 3:
- Compare 24 and 38, no swap needed.
- Compare 38 and 1, swap them: [24, 1, 38, 42, 47]
4. Pass 4:
- Compare 24 and 1, swap them: [1, 24, 38, 42, 47]
After the 4th pass, no more swaps are needed, so the algorithm terminates. The
sorted list is: [1, 24, 38, 42, 47].
a.solution
BST stands for Binary Search Tree. It is a binary tree data structure in which each node has at
most two child nodes, arranged in such a way that:
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
1. The value of the node to the left is less than or equal to the value of the parent node.
2. The value of the node to the right is greater than or equal to the value of the parent node.
This property holds for each node in the Binary Search Tree, making it an ordered data
structure. BSTs are commonly used for searching, sorting, and managing ordered data. They
allow for efficient insertion, deletion, and lookup of elements, with average-case time
complexities of O(log n), where n is the number of nodes in the tree. However, in the worst
case (when the tree becomes completely unbalanced), the time complexity can degrade to
O(n).
Some common operations performed on Binary Search Trees include insertion, deletion,
searching for a specific value, finding the minimum and maximum values, and traversing the
tree in different orders (in-order, pre-order, and post-order traversals).
b.Solution
A Binary Search Tree (BST) is a binary tree data structure where each node has at most two child
nodes, arranged in a way such that the value of the node to the left is less than or equal to the value
of the parent node, and the value of the node to the right is greater than or equal to the value of the
parent node.
```
56
```
```
56
/
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
38
```
3. Insert 10 (10 < 56 and 10 < 38, so it becomes the left child of 38).
```
56
38
10
```
```
56
/ \
38 65
10
```
5. Insert 72 (72 > 56 and 72 > 65, so it becomes the right child of 65).
```
56
/ \
38 65
/ \
10 72
```
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
6. Insert 44 (44 < 56 and 44 > 38, so it becomes the right child of 38).
```
56
/ \
38 65
/ \
10 72
44
```
7. Insert 50 (50 < 56, 50 > 38, and 50 > 44, so it becomes the right child of 44).
```
56
/ \
38 65
/ \
10 72
44
50
```
The final Binary Search Tree for the given elements is:
```
56
/ \
38 65
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
/ \
10 72
44
50
A Hash Table is a data structure that allows efficient storage and retrieval of data using the
concept of hashing. It stores data in an associative manner, using key-value pairs, where
each key is associated with a unique value. The hash table uses a hash function to calculate
an index for each key, which determines the position of the corresponding value in the table.
This allows for fast access to the desired data using the key.
A Hash Function is a mathematical function that takes an input (or a key) and returns a fixed-
size output, which is used as the index in the hash table. The primary goal of a hash function
is to distribute the keys uniformly across the hash table to minimize collisions (situations
where two or more keys have the same hash value). A good hash function should have the
following properties:
1. Deterministic: The same input (key) should always produce the same output (hash value).
2. Uniform distribution: The hash function should distribute the keys uniformly across the
hash table, minimizing the chances of collisions.
3. Fast computation: The hash function should be computationally efficient, allowing for fast
calculations of hash values.
4. Minimal collisions: Although it's impossible to avoid collisions entirely, a good hash
function should minimize the number of collisions.
Assistant professor. 3rd Semester B.C.A Degree Examination, January-2024
Brijesh C G Data Structure (NEP Scheme)
2023 question paper and Solution
In summary, hashing is a technique that converts input data into fixed-size output using a
mathematical algorithm. A hash table is a data structure that stores data in an associative
manner using key-value pairs and a hash function to calculate the index for each key. A hash
function is a mathematical function that generates a fixed-size output (hash value) from a
given input (key), with the aim of distributing the keys uniformly across the hash table,
minimizing collisions, and allowing for fast computation.
b.solution
Here's a simple C program to perform a linear search on an array of integers:
```c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int target;
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found in the array.\n");
}
return 0;
}
```
In this program, the `linearSearch` function takes an array, the number of elements in the
array (n), and the target value to search for. It iterates through the array and compares each
element with the target. If the target is found, the function returns the index of the target
element. If the target is not found, the function returns -1.
The `main` function initializes an array of integers, reads the target value from the user, and
calls the `linearSearch` function. It then prints the result based on the returned index value.