0% found this document useful (0 votes)
3 views

Explain Different Types of Data Structures and Their Applications…

The document provides an overview of various data structures, including linear (arrays, linked lists, stacks, queues), non-linear (trees, graphs), hash-based structures (hash tables), and specialized structures (tries, heaps, B-Trees), along with their applications. It also compares arrays and linked lists, explains stack operations, tree traversal techniques, and sorting algorithms with their time complexities. Additionally, it covers concepts like recursion, graphs, hashing, dynamic programming, Dijkstra's algorithm, and AVL trees, including their properties and rotation techniques.

Uploaded by

Sayan Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Explain Different Types of Data Structures and Their Applications…

The document provides an overview of various data structures, including linear (arrays, linked lists, stacks, queues), non-linear (trees, graphs), hash-based structures (hash tables), and specialized structures (tries, heaps, B-Trees), along with their applications. It also compares arrays and linked lists, explains stack operations, tree traversal techniques, and sorting algorithms with their time complexities. Additionally, it covers concepts like recursion, graphs, hashing, dynamic programming, Dijkstra's algorithm, and AVL trees, including their properties and rotation techniques.

Uploaded by

Sayan Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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.

2. Compare and contrast arrays and linked lists.


Answer:
Feature Arrays Linked Lists
Structure Contiguous memory Non-contiguous memory
allocation allocation
Insertion/Deletion Costly due to shifting of Efficient, especially in
elements the middle
Access Time O(1) for direct access via O(n) for sequential
index access
Memory Efficiency Fixed size; may waste Dynamic size; no
memory memory wastage
Applications Best for static data Suitable for dynamic
datasets
3. Explain stack and its applications. Implement a stack using an array.
Answer:
Stack is a linear data structure that follows the Last In, First Out (LIFO) principle. Operations:
●Push: Add an element to the top.
● Pop: Remove an element from the top.
● Peek: View the top element.
Applications:
Expression evaluation (infix to postfix conversion, evaluation).

● Function call stack in programming languages.
● Undo/redo in text editors.
Implementation of Stack using Array:

#include <stdio.h>
#define MAX 100

int stack[MAX];
int top = -1;

void push(int value) {


if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = value;
}
}

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;
}

4. Describe tree traversal techniques with examples.


Answer:
Tree traversal refers to visiting all nodes in a tree systematically. Common techniques include:
a) Depth-First Traversals (DFT):
. Inorder (Left, Root, Right): Example: For a tree:

1
/\
2 3

Preorder (Root, Left, Right): Output: 1, 2, 3.


Postorder (Left, Right, Root): Output: 2, 3, 1.

b) Breadth-First Traversal (BFT):


● Also known as Level Order Traversal.
● Visits nodes level by level.
● Output: 1, 2, 3.

5. What are the differences between BFS and DFS?

Feature BFS (Breadth-First DFS (Depth-First


Search) Search)
Traversal Level by level Depth of a branch first
Data Structure Used Queue Stack (or recursion)
Space Complexity O(V) (queue storage) O(V) (recursion stack)
Applications Shortest path in Detecting cycles in
unweighted graphs graphs

Explain sorting algorithms with time complexity.


Answer:
Sorting Time Space Stable? Comments
Algorithm Complexity Complexity
(Best, Avg,
Worst)
Bubble Sort O(n), O(n²), O(1) Yes Simple but
O(n²) inefficient
Insertion Sort O(n), O(n²), O(1) Yes Good for
O(n²) small
datasets
Selection O(n²), O(n²), O(1) No Inefficient,
Sort O(n²) rarely used
Merge Sort O(n log n), O(n) Yes Divide and
O(n log n), conquer
O(n log n)
Quick Sort O(n log n), O(log n) No Best for large
O(n log n), datasets
O(n²)
Heap Sort O(n log n), O(1) No Based on
O(n log n), heaps
O(n log n)

Write a program to implement binary search.


Answer:
Binary Search is an efficient algorithm with a time complexity of O(log n).

#include <stdio.h>

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}

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;
}

What is a graph? Explain its representations and applications.


Answer:
A graph is a non-linear data structure consisting of vertices (nodes) and edges (connections). A
graph is denoted as
G
(
V
,
E
)
G(V,E), where
V
V is a set of vertices and
E
E is a set of edges.
Graph Representations:
. Adjacency Matrix:
○ A 2D array where A
[
i
]
[
j
]
=
1

A[i][j]=1 if there is an edge from vertex i

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.

Explain the concept of recursion. How does it differ from iteration?


Answer:
Recursion is a process where a function calls itself directly or indirectly. It is used to solve
problems by breaking them into smaller sub-problems.
Example (Factorial using recursion):
c
Copy code

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

Write a program to implement a queue using a linked list.


#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* front = NULL;


struct Node* rear = NULL;
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}

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

After Left Rotation:


20
/ \
10 30

You might also like