Explain Different Types of Data Structures and Their Applications…
Explain Different Types of Data Structures and Their Applications…
Answer:
Data structures are ways of organizing and storing data for efficient access and modification.
They can be broadly categorized into the following types:
a) Linear Data Structures:
● Definition: Elements are arranged sequentially.
● Examples: Arrays, Linked Lists, Stacks, Queues.
● Applications:
○ Arrays: Used for static data storage like a list of items, matrices, etc.
○ Linked Lists: Dynamic memory allocation, undo functionality in editors.
○ Stacks: Backtracking algorithms like DFS, parsing expressions.
○ Queues: Scheduling (CPU scheduling, I/O Buffers).
b) Non-linear Data Structures:
● Definition: Elements are not sequentially connected.
● Examples: Trees, Graphs.
● Applications:
○ Trees: Represent hierarchical data (e.g., file systems, databases).
○ Graphs: Represent networks like social networks, shortest path algorithms.
c) Hash-based Data Structures:
● Definition: Data is stored in key-value pairs.
● Example: Hash Tables.
● Applications: Databases, caches, associative arrays.
d) Specialized Data Structures:
● Examples: Tries, Heaps, B-Trees.
● Applications:
○ Tries: Prefix-based search like in auto-suggestions.
○ Heaps: Priority Queues, heap sort.
○ B-Trees: Indexing in databases.
#include <stdio.h>
#define MAX 100
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
int peek() {
if (top == -1) {
printf("Stack is empty\n");
return -1;
} else {
return stack[top];
}
}
int main() {
push(10);
push(20);
printf("Top element: %d\n", peek());
printf("Popped element: %d\n", pop());
printf("Popped element: %d\n", pop());
return 0;
}
1
/\
2 3
#include <stdio.h>
int main() {
int arr[] = {2, 4, 6, 8, 10, 12};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 8;
int result = binarySearch(arr, n, key);
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
i to vertex j
j; otherwise 0
0.
○ Space Complexity: O(V
2
V2).
. Adjacency List:
○ An array of lists where each vertex points to a list of its adjacent vertices.
○ Space Complexity: O(V
+
E
V+E).
Applications:
● Computer Networks: Represent routers and connections.
● Social Networks: Represent users and relationships.
● Pathfinding Algorithms: Dijkstra's and Floyd-Warshall algorithms.
● Web Crawlers: Represent hyperlinks between web pages.
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
Key Differences:
Feature Recursion Iteration
Definition Function calls itself Uses loops (for, while)
Memory Usage Requires stack memory No extra memory
required
Termination Needs a base condition Terminates with loop
condition
Complexity May lead to stack More efficient in terms
overflow of sp
struct Node {
int data;
struct Node* next;
};
int dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return -1;
}
int value = front->data;
struct Node* temp = front;
front = front->next;
if (front == NULL) {
rear = NULL;
}
free(temp);
return value;
}
void display() {
struct Node* temp = front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
printf("Queue: ");
display();
printf("Dequeued: %d\n", dequeue());
printf("Queue after dequeue: ");
display();
return 0;
}
Explain hashing and its applications. What are collision resolution techniques?
Answer:
Hashing is a technique to map data to a fixed-size table (hash table) using a hash function.
Applications:
● Password storage.
● Database indexing.
● Caches in web browsers.
● Associative arrays.
Collision Resolution Techniques:
. Chaining: Use a linked list to store all elements with the same hash.
. Open Addressing:
○ Linear Probing: Check the next slot sequentially.
○ Quadratic Probing: Use a quadratic formula to find the next slot.
○ Double Hashing: Use a second hash function for probing.
12. What are the advantages and disadvantages of different sorting algorithms?
Answer:
Algorithm Advantages Disadvantages
Bubble Sort Simple, easy to Inefficient for large
implement datasets
Insertion Sort Efficient for small Poor performance on
datasets large datasets
Selection Sort Reduces swapping Inefficient, even for
sorted arrays
Merge Sort Always O(n log n); stable Requires extra memory
Quick Sort Efficient for average Worst-case O(n²);
cases unstable
Heap Sort In-place, good for large Not stable
datasets
13. Explain the concept of dynamic programming. How is it different from divide-and-
conquer?
Answer:
Dynamic Programming (DP) is a method of solving problems by breaking them into smaller sub-
problems, solving each sub-problem once, and storing their results.
Key Features:
Overlapping sub-problems.
●
Optimal substructure.
●
Example: Fibonacci Sequence using DP:
c
Copy code
int fibonacci(int n) {
int dp[n + 1];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
Difference Between DP and Divide-and-Conquer:
Feature Dynamic Programming Divide-and-Conquer
Approach Bottom-up Top-down
Sub-problems Overlapping Independent
Storage Stores sub-problem Does not store solutions
solutions
Explain Dijkstra's algorithm with an example.
Answer:
Dijkstra's Algorithm is used to find the shortest path from a source vertex to all other vertices in
a weighted graph.
Steps:
. Initialize distances of all vertices as infinite except the source (distance = 0).
. Use a priority queue to pick the vertex with the smallest distance.
. Update distances of its adjacent vertices.
. Repeat until all vertices are processed.
Example: Graph:
(A)
1/ \4
(B)---(C)
2
Source: A
● Shortest Path to B: A → B (Distance: 1).
● Shortest Path to C: A → B → C (Distance: 3).
What are AVL trees? Explain rotations in AVL trees.
Answer:
An AVL tree is a self-balancing binary search tree where the difference in height of left and right
subtrees (balance factor) is at most 1.
Rotations in AVL Trees:
. Left Rotation: Applied when the right subtree is unbalanced.
. Right Rotation: Applied when the left subtree is unbalanced.
. Left-Right Rotation: Combination of left and right rotations.
. Right-Left Rotation: Combination of right and left rotations.
10
\
20
\
30