0% found this document useful (0 votes)
4 views6 pages

DSA Important Questions

DSA question answers

Uploaded by

dasdytyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

DSA Important Questions

DSA question answers

Uploaded by

dasdytyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

### Important Topics in Data Structures and Algorithms ###

**7. Graphs**

**Q: Explain the representations of graphs (Adjacency Matrix and Adjacency List).**

1. **Adjacency Matrix:**

- A 2D array of size `V x V` (where `V` is the number of vertices).

- If theres an edge between vertex `i` and `j`, `matrix[i][j] = 1`, otherwise `matrix[i][j] = 0`.

- **Pros:** Easy to implement, fast edge lookup.

- **Cons:** Consumes `O(V)` space even for sparse graphs.

2. **Adjacency List:**

- An array of lists where each list contains the adjacent vertices of a vertex.

- **Pros:** Space-efficient for sparse graphs.

- **Cons:** Slower edge lookup compared to adjacency matrix.

**Q: Write algorithms for DFS and BFS.**

1. **Depth-First Search (DFS):**

- Uses recursion or a stack to explore the graph deeply before backtracking.

- **Algorithm:** Recursively visit each unvisited neighbor.

- **Code (C):**

```c

void dfs(int vertex, int adj[10][10], int visited[], int n) {

printf("%d ", vertex);

visited[vertex] = 1;
for (int i = 0; i < n; i++) {

if (adj[vertex][i] == 1 && !visited[i]) {

dfs(i, adj, visited, n);

```

2. **Breadth-First Search (BFS):**

- Uses a queue to explore all neighbors of a vertex before moving to the next level.

- **Algorithm:** Use a queue to process each nodes neighbors.

- **Code (C):**

```c

void bfs(int start, int adj[10][10], int visited[], int n) {

int queue[10], front = 0, rear = 0;

visited[start] = 1;

queue[rear++] = start;

while (front < rear) {

int curr = queue[front++];

printf("%d ", curr);

for (int i = 0; i < n; i++) {

if (adj[curr][i] == 1 && !visited[i]) {

visited[i] = 1;

queue[rear++] = i;

}
}

```

**8. Sorting**

**Q: Write algorithms and analyze time complexity for Bubble Sort, Quick Sort, and Merge Sort.**

1. **Bubble Sort:**

- Repeatedly compare adjacent elements and swap them if in the wrong order.

- **Code:**

```c

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

```

- **Time Complexity:** `O(n)` worst/average case, `O(n)` best case.

2. **Quick Sort:**
- Partition the array around a pivot and recursively sort the partitions.

- **Code:**

```c

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = low - 1;

for (int j = low; j < high; j++) {

if (arr[j] < pivot) {

i++;

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

return i + 1;

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}
```

**10. Hashing**

**Q: What is Hashing? Explain its applications.**

- **Hashing:** A technique to map keys to values using a hash function.

- **Applications:** Fast lookups in databases, caching, and compilers.

**Q: Write a program for Chaining (collision handling).**

- **Code:**

```c

struct Node {

int data;

struct Node* next;

};

struct Node* hashTable[10];

int hashFunction(int key) {

return key % 10;

void insert(int key) {

int index = hashFunction(key);

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = key;
newNode->next = hashTable[index];

hashTable[index] = newNode;

void display() {

for (int i = 0; i < 10; i++) {

printf("Index %d: ", i);

struct Node* temp = hashTable[i];

while (temp) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL

");

```

You might also like