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

DSA_Detailed_Questions_Answers

DSA question answers

Uploaded by

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

DSA_Detailed_Questions_Answers

DSA question answers

Uploaded by

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

### Detailed Questions and Answers for Data Structures and Algorithms ###

**1. Arrays**

**Q: Write algorithms for searching an element in an array (Linear Search and Binary Search).**

- **Linear Search:**

- **Algorithm:**

1. Start from the first element of the array.

2. Compare the target element with each array element sequentially.

3. If a match is found, return the index of the element.

4. If the target is not found, return -1.

- **Code:**

```c

int linearSearch(int arr[], int n, int target) {

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

if (arr[i] == target) return i; // Return index if found

return -1; // Return -1 if not found

```

- **Binary Search (only works on sorted arrays):**

- **Algorithm:**

1. Initialize `low = 0`, `high = n - 1`.


2. Calculate `mid = (low + high) / 2`.

3. If `arr[mid] == target`, return `mid`.

4. If `arr[mid] < target`, search in the right half (`low = mid + 1`).

5. If `arr[mid] > target`, search in the left half (`high = mid - 1`).

6. If the range is invalid (`low > high`), return -1.

- **Code:**

```c

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

int low = 0, high = n - 1;

while (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == target) return mid; // Found target

else if (arr[mid] < target) low = mid + 1;

else high = mid - 1;

return -1; // Not found

```

**2. Linked Lists**

**Q: Explain the types of linked lists (Singly, Doubly, Circular).**

- **Singly Linked List:** Each node contains data and a pointer to the next node. Traversal is unidirectional.

**Example:** `10 -> 20 -> 30 -> NULL`


- **Doubly Linked List:** Each node contains data, a pointer to the next node, and a pointer to the previous

**Example:** `NULL <- 10 <-> 20 <-> 30 -> NULL`

- **Circular Linked List:** The last node points to the first node, forming a circular structure.

**Example:** `10 -> 20 -> 30 -> 10`

**3. Stack**

**Q: Write code for checking balanced parentheses using a stack.**

- **Code:**

```c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define SIZE 100

char stack[SIZE];

int top = -1;

void push(char c) { stack[++top] = c; }

char pop() { return stack[top--]; }

char peek() { return stack[top]; }

int isBalanced(char* expr) {

for (int i = 0; i < strlen(expr); i++) {


char c = expr[i];

if (c == '(' || c == '{' || c == '[') push(c);

else if (c == ')' || c == '}' || c == ']') {

if (top == -1) return 0;

char topChar = peek();

if ((c == ')' && topChar == '(') || (c == '}' && topChar == '{') || (c == ']' && topChar == '[')) {

pop();

} else {

return 0;

return top == -1;

int main() {

char expr[] = "{[()]}";

printf(isBalanced(expr) ? "Balanced

" : "Not Balanced

");

return 0;

```

**4. Queue**

**Q: Write code for implementing a queue using arrays.**


- **Code:**

```c

#include <stdio.h>

#define SIZE 100

int queue[SIZE];

int front = -1, rear = -1;

void enqueue(int value) {

if (rear == SIZE - 1) {

printf("Queue Overflow

");

return;

if (front == -1) front = 0;

queue[++rear] = value;

int dequeue() {

if (front == -1 || front > rear) {

printf("Queue Underflow

");

return -1;

return queue[front++];

}
int main() {

enqueue(10);

enqueue(20);

printf("Dequeued: %d

", dequeue());

return 0;

```

**7. Graphs**

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

1. **Adjacency Matrix:** A 2D array where element `matrix[i][j] = 1` indicates an edge from `i` to `j`.

2. **Adjacency List:** Each vertex has a list of adjacent vertices.

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

- **DFS 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);


}

```

- **BFS 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;

```

**10. Hashing**
**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