Ronak Data Structures Paper 2022-23 QnA 01
Ronak Data Structures Paper 2022-23 QnA 01
Part A
### (a) What are the features of an efficient algorithm?
1. **Correctness**: The algorithm should produce the correct output for all possible valid
inputs.
2. **Time Complexity**: The amount of me taken by the algorithm to complete should be
as low as possible.
3. **Space Complexity**: The amount of memory space required by the algorithm should
be minimized.
4. **Readability and Simplicity**: The algorithm should be easy to understand and
implement.
5. **Scalability**: The algorithm should perform well even as the size of the input data
increases.
6. **Robustness**: The algorithm should handle edge cases and unexpected inputs
gracefully.
Primi ve data structures are the most basic forms of data structures directly supported by
the system or programming language. These include:
- **Integer**: Whole numbers
- **Float**: Floa ng-point numbers or real numbers
- **Character**: Single characters or le ers
- **Boolean**: True or false values
Asympto c nota ons are mathema cal tools to represent the me complexity and space
complexity of algorithms. They describe the behavior of an algorithm in terms of input size
as the input size grows. Common asympto c nota ons include:
- **Big O Nota on (O)**: Upper bound, worst-case complexity.
- **Omega Nota on (Ω)**: Lower bound, best-case complexity.
### (d) List the various opera ons that can be performed on data structure.
### (e) What are the pi alls encountered in Singly linked list?
1. **Traversal Time**: Traversing a singly linked list can take more me compared to arrays
since you must follow the links.
2. **Memory Usage**: Each element requires addi onal memory for storing a
pointer/reference to the next element.
3. **Inser on/Dele on at End**: Inser on and dele on at the end of the list require
traversal from the head to the last node.
4. **No Backward Traversal**: Only forward traversal is possible, making some opera ons
more complex or inefficient.
- **Arrays**:
- Fixed size.
- Homogeneous elements (same data type).
- Con nuous memory alloca on.
- Faster access (O(1) me complexity for indexing).
To test for an empty queue, you typically check if the front pointer or the size of the queue is
zero. For example:
- If using an array-based queue: `if (front == -1 || front > rear)`.
- If using a linked list-based queue: `if (head == NULL)`.
Searching techniques are methods to find an element within a data structure. Common
searching techniques include:
- **Linear Search**: Sequen ally checking each element un l the target is found.
- **Binary Search**: Repeatedly dividing the search interval in half, used on sorted arrays.
**Binary Search** is generally faster than linear search for large, sorted datasets, with a
me complexity of O(log n) compared to O(n) for linear search.
PART - B
## UNIT - I
### 2. (a) What are Data Structures? Explain the classifica on of data structures with neat
diagram.
**Data Structures** are organized ways to store, manage, and manipulate data in a
computer system, enabling efficient access and modifica on. They are essen al for various
compu ng tasks and algorithms.
- Integer
- Float
- Character
- Boolean
**Diagram:**
```plaintext
Data Structures
|
|-- Primi ve
| |-- Integer
| |-- Float
| |-- Character
| |-- Boolean
|
|-- Non-Primi ve
|
|-- Linear
| |-- Array
| |-- Linked List
| |-- Stack
| |-- Queue
|
|-- Non-Linear
|-- Tree
|-- Graph
|-- Heap
|-- Hash Table
```
### (b) Define Prefix and Pos ix Expression. Write a program to convert infix to pos ix
expression.
class Program
{
sta c int Precedence(char op)
{
switch (op)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
}
else if (c == '(')
{
stack.Push(c);
}
else if (c == ')')
{
while (stack.Count > 0 && stack.Peek() != '(')
{
result += stack.Pop();
}
stack.Pop();
}
else
{
while (stack.Count > 0 && Precedence(stack.Peek()) >= Precedence(c))
{
result += stack.Pop();
}
stack.Push(c);
}
}
## OR
### 2. (a) What is Analysis of Algorithm? Explain the Asympto c Nota on (Big O, Ω, Θ)
used while analyzing an algorithm.
1. **Big O Nota on (O):** Describes the upper bound of the me complexity. It gives the
worst-case scenario.
- Example: O(n^2) indicates the algorithm's me complexity grows at most propor onal to
the square of the input size.
2. **Omega Nota on (Ω):** Describes the lower bound of the me complexity. It gives the
best-case scenario.
- Example: Ω(n) indicates the algorithm's me complexity grows at least propor onal to
the input size.
3. **Theta Nota on (Θ):** Describes the exact bound of the me complexity. It provides
both the upper and lower bounds.
- Example: Θ(n log n) indicates the algorithm's me complexity grows exactly propor onal
to n log n.
**Pseudo code:** A high-level descrip on of an algorithm using the structural conven ons
of programming languages but in a simplified form. It focuses on the logic rather than syntax
and helps in understanding the algorithm without ge ng into programming details.
Example:
```plaintext
Algorithm: Find the maximum number in a list
Input: List of numbers
Output: Maximum number
Procedure findMax(list):
max = list[0]
for each number in list:
## UNIT - II
### 3. (a) What is linked-list? Give the data structure and write algorithm to-
(i) Delete an element
(ii) Count the number of elements.
**Linked List:** A data structure consis ng of nodes, where each node contains a data value
and a reference (or link) to the next node in the sequence.
```csharp
public class Node
{
public Node(int d)
{
data = d;
next = null;
}
}
```
```csharp
public Node DeleteNode(Node head, int key)
{
Node temp = head, prev = null;
prev.next = temp.next;
return head;
}
```
**Algorithm to Count Number of Elements in C#:**
```csharp
public int CountNodes(Node head)
{
int count = 0;
Node current = head;
return count;
}
```
### (b) When doubly linked list can be represented as Circular Linked List?
A doubly linked list can be represented as a circular linked list when the `next` pointer of the
last node points to the first node, and the `prev` pointer of the first node points to the last
node, forming a circular chain.
### (c) Write the rou ne for inser on opera on of singly linked list.
```csharp
public Node InsertNode(Node head, int data)
{
Node newNode = new Node(data);
if (head == null)
{
head = newNode;
return head;
}
last.next = newNode;
return head;
}
```
## OR
### 3. (a) List out the steps involved in dele ng a node from a Binary Search Tree.
1. **Find the Node to be Deleted**: Traverse the tree to locate the node.
2. **Node has No Children (Leaf Node)**: Simply remove the node.
3. **Node has One Child**: Remove the node and replace it with its child.
4. **Node has Two Children**: Find the in-order successor (smallest node in the right
subtree) or in-order predecessor (largest node in the le subtree), replace the node's value
with it, and delete the successor or predecessor node.
### (b) Define tree-traversal and men on the types of traversals.
**Tree Traversal:** The process of visi ng all the nodes in a tree data structure in a specific
order.
**Types of Traversals:**
1. **In-Order Traversal (Le , Root, Right):** Visits the le subtree, root, and then the right
subtree.
2. **Pre-Order Traversal (Root, Le , Right):** Visits the root, le subtree, and then the right
subtree.
3. **Post-Order Traversal (Le , Right, Root):** Visits the le subtree, right subtree, and then
the root.
### (c) List out the applica ons of Tree Data Structure.
## UNIT - III
1. **Adjacency Matrix:**
- A 2D array where each cell `[i][j]` is `1` if there is an edge between vertex `i` and vertex `j`,
otherwise `0`.
**Example:**
```plaintext
010
101
010
```
2. **Adjacency List:**
- An array of lists. The index represents the vertex, and the list at each index contains the
adjacent ver ces.
**Example:**
```plaintext
0: 1
1: 0, 2
2: 1
```
3. **Incidence Matrix:**
- A 2D array where rows represent ver ces and columns represent edges. The entry `(i, j)`
is `1` if vertex `i` is connected by edge `j`.
**Example:**
```plaintext
e1 e2 e3
1 1 0
1 0 1
0 1 1
```
## OR
### 5. (a) What do you mean by Internal and External Sor ng?
- **Internal Sor ng:** Sor ng algorithms that assume the en re dataset fits into the main
memory. Examples: Quick sort, Merge sort.
- **External Sor ng:** Sor ng algorithms designed for datasets that are too large to fit into
main memory and must be stored on external storage. Examples: External merge sort, Mul -
way merge sort.
### (b) List out the different types of Hashing func ons.
### (c) Write an algorithm to implement bubble sort with suitable example.
```csharp
using System;
class Program
{
sta c void BubbleSort(int[] arr)
{
int n = arr.Length;
**Hashing:** A technique to convert a given key into a unique hash value (index) in an array
or table. It provides a way to quickly access data based on the generated index.
**Extendible Hashing:** A type of dynamic hashing that allows the hash table to expand
and contract as keys are added and removed. It uses a directory of pointers to buckets and
allows the table to grow by spli ng buckets when they become full.
- **Quick Sort:**
- Average and Best Case Time Complexity: O(n log n)
- Worst Case Time Complexity: O(n^2) (can be improved with randomiza on)
- In-place sor ng (no extra space required)
- Faster in prac ce due to good cache performance
- **Merge Sort:**
- Time Complexity: O(n log n) for all cases
- Requires addi onal space for merging (not in-place)
- Stable sor ng algorithm
- Preferred for sor ng linked lists and external sor ng
### (d) How does inser on sort algorithm work?
```csharp
using System;
class Program
{
sta c void Inser onSort(int[] arr)
{
int n = arr.Length;
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
2023
Here are the answers to the ques ons from the extracted content of the "Ronak Data
Structures Paper 2023":
---
### PART - A:
### PART - B:
2. **Explain the concept of a stack and provide an example of its opera on.**
- **Answer:** A stack is a linear data structure that follows the Last In, First Out (LIFO)
principle. Elements are added (pushed) and removed (popped) from the top of the stack. For
example, if we push elements `1, 2, 3` onto the stack, and then pop an element, we get `3`.
OR
**What is the concept of a priority queue? How is it different from a regular queue?**
- **Answer:** A priority queue is an abstract data type similar to a regular queue or stack
data structure in which each element addi onally has a priority associated with it. In a
priority queue, an element with high priority is served before an element with low priority.
This is different from a regular queue where elements are served in the order they were
added (FIFO).
3. **Write an algorithm to insert a node between two nodes in a doubly linked list.**
- **Answer:**
```python
def insert_between(node_before, node_a er, new_node):
new_node.prev = node_before
new_node.next = node_a er
node_before.next = new_node
node_a er.prev = new_node
```
OR
**Describe the process of tree traversal. What are the different types of tree traversal
algorithms?**
- **Answer:** Tree traversal is the process of visi ng all the nodes of a tree in a specific
order. The different types of tree traversal algorithms are:
- **Preorder Traversal:** Visit the root node, then recursively visit the le subtree, and
finally the right subtree.
- **Inorder Traversal:** Recursively visit the le subtree, visit the root node, and then the
right subtree.
- **Postorder Traversal:** Recursively visit the le subtree, then the right subtree, and
finally the root node.
- **Level Order Traversal:** Visit the nodes level by level star ng from the root.
4. **What is the difference between Breadth-First Search (BFS) and Depth-First Search
(DFS)? When would you use each algorithm?**
- **Answer:**
- **BFS:** BFS explores the neighbor nodes level by level star ng from the root. It uses a
queue to keep track of the next node to visit.
- **DFS:** DFS explores as far down a branch as possible before backtracking. It uses a
stack (or recursion) to keep track of nodes to visit.
- **Use cases:** BFS is used when the shortest path is sought, like in finding the shortest
path in an unweighted graph. DFS is used for traversing or searching tree or graph data
structures, o en in scenarios like topological sor ng or detec ng cycles.
OR
**Describe the concept of graph traversal. What are the different graph traversal
algorithms?**
- **Answer:** Graph traversal is the process of visi ng all the ver ces and edges of a
graph. The different graph traversal algorithms are:
- **Breadth-First Search (BFS):** Traverses the graph level by level.
- **Depth-First Search (DFS):** Traverses the graph by exploring as deep as possible along
each branch before backtracking.
5. **What is the difference between a linear search and a binary search algorithm? In
what scenarios would you choose one over the other?**
- **Answer:**
- **Linear Search:** This algorithm checks each element of the list sequen ally un l the
desired element is found or the list ends. It is simple and works on both sorted and unsorted
lists.
- **Binary Search:** This algorithm finds the posi on of a target value within a sorted
array. It compares the target value to the middle element and eliminates half of the
remaining elements at each step.
- **Use cases:** Use linear search for small or unsorted lists and binary search for large,
sorted lists for faster search opera ons.
OR
**Explain the concept of a hash func on and its role in hash tables.**
- **Answer:** A hash func on is a func on that takes input (or 'key') and returns a fixed-
size string of bytes. The output is typically a 'hash code' used to index a hash table. The role
of a hash func on in hash tables is to efficiently distribute keys into buckets to allow for fast
data retrieval.
---
If you need further elabora on or have any specific ques ons on these topics, feel free to
ask!