DSA Questions
DSA Questions
#include <iostream>
using namespace std;
if (arr[mid] == key)
return mid; // Element found at mid
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = binarySearchRecursive(arr, 0, n - 1,
key);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result <<
endl;
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = binarySearchIterative(arr, n,
key);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result
<< endl;
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = linearSearchIterative(arr, n,
key);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result
<< endl;
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = linearSearchRecursive(arr, n, key,
0);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result
<< endl;
return 0;
}
int main() {
int arr[] = {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}
class Stack {
private:
int arr[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
int pop() {
if (top < 0) {
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}
int peek() {
if (top < 0) {
cout << "Stack is empty\n";
return -1;
}
return arr[top];
}
bool isEmpty() {
return (top < 0);
}
};
int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "Top element: " << s.peek() << endl;
cout << "Popped element: " << s.pop() << endl;
cout << "Top element: " << s.peek() << endl;
return 0;
}
8. Q2) Stack using List
#include <iostream>
#include <list>
using namespace std;
class Stack {
private:
list<int> data;
public:
void push(int value) {
data.push_back(value);
}
int pop() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
int value = data.back();
data.pop_back();
return value;
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
return data.back();
}
bool isEmpty() {
return data.empty();
}
};
int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "Top element: " << s.peek() << endl;
cout << "Popped element: " << s.pop() << endl;
cout << "Top element: " << s.peek() << endl;
return 0;
}
9. Q3) Stack using Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class Stack {
private:
Node* top;
public:
Stack() {
top = nullptr;
}
int pop() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
int value = top->data;
Node* temp = top;
top = top->next;
delete temp;
return value;
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
return top->data;
}
bool isEmpty() {
return (top == nullptr);
}
};
int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "Top element: " << s.peek() << endl;
cout << "Popped element: " << s.pop() << endl;
cout << "Top element: " << s.peek() << endl;
return 0;
}
10. Q4) Queue implementation using Array
#include <iostream>
using namespace std;
class Queue {
private:
int arr[MAX_SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
int peek() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
return arr[front];
}
bool isEmpty() {
return (front == -1);
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << "Front element: " << q.peek() << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
cout << "Front element: " << q.peek() << endl;
return 0;
}
11. Q5) Queue using List
#include <iostream>
#include <list>
using namespace std;
class Queue {
private:
list<int> data;
public:
void enqueue(int value) {
data.push_back(value);
}
int dequeue() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
int value = data.front();
data.pop_front();
return value;
}
int peek() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
return data.front();
}
bool isEmpty() {
return data.empty();
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << "Front element: " << q.peek() << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
cout << "Front element: " << q.peek() << endl;
return 0;
}
12. Q6) Queue using Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = nullptr;
rear = nullptr;
}
int dequeue() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
int value = front->data;
Node* temp = front;
if (front == rear)
front = rear = nullptr;
else
front = front->next;
delete temp;
return value;
}
int peek() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
return front->data;
}
bool isEmpty() {
return (front == nullptr);
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << "Front element: " << q.peek() << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
cout << "Front element: " << q.peek() << endl;
return 0;
}
Practical Class-3
13. Q1) Write a C++ program to separate 0s and 1s from a given
array of values 0 and 1.
#include <iostream>
using namespace std;
int main() {
int arr[] = {0, 1, 0, 1, 1, 0, 0, 1};
int n = sizeof(arr) / sizeof(arr[0]);
separateZerosOnes(arr, n);
cout << "Array after separating 0s and 1s: ";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}
14. Q2) Function to count occurrences of a specific number 'x' in
an array 'arr' of size 'n'
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 2, 2, 5, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 2;
cout << "Number of occurrences of " << x << ": " <<
countOccurrences(arr, n, x) << endl;
return 0;
}
15. Q3) Indirect recursion
#include <iostream>
using namespace std;
void funA(int n) {
if (n > 0) {
cout << n << " ";
funB(n - 1);
}
}
void funB(int n) {
if (n > 1) {
cout << n << " ";
funA(n / 2);
}
}
int main() {
funA(20);
return 0;
}
Practical Class-4
16. Q1) Circular Linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class CircularLinkedList {
private:
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}
void display() {
if (head == nullptr) {
cout << "List is empty\n";
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList list;
list.insert(1);
list.insert(2);
list.insert(3);
list.display();
return 0;
}
17. Q2) Singly Linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class SinglyLinkedList {
private:
Node* head;
public:
SinglyLinkedList() {
head = nullptr;
}
int main() {
SinglyLinkedList list;
list.insert(1);
list.insert(2);
list.insert(3);
list.display();
return 0;
}
18. Q3) Doubly Linked List using Class
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int value) {
data = value;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
void display() {
if (head == nullptr) {
cout << "List is empty\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
DoublyLinkedList list;
list.insert(1);
list.insert(2);
list.insert(3);
list.display();
return 0;
}
Practical Class-5
19. Q1) Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
int main() {
Node* head = nullptr;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
display(head);
return 0;
}
20. Q2) Tree Traversal
#include <iostream>
using namespace std;
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
return 0;
}
Practical Class-6
21. Q1) Binary Search Tree operations
#include <iostream>
using namespace std;
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};
class BST {
private:
TreeNode* root;
public:
BST() {
root = nullptr;
}
int main() {
BST bst;
bst.insert(4);
bst.insert(2);
bst.insert(6);
bst.insert(1);
bst.insert(3);
return 0;
}
22. Q2) Tree Traversal using C style
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
int main() {
struct TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return 0;
}
23. Q3) AVL tree
24. #include <iostream>
25. using namespace std;
26.
27. struct TreeNode {
28. int data;
29. TreeNode* left;
30. TreeNode* right;
31. int height;
32. };
33.
34. int max(int a, int b) {
35. return (a > b) ? a : b;
36. }
37.
38. int getHeight(TreeNode* node) {
39. if (node == nullptr)
40. return 0;
41. return node->height;
42. }
43.
44. int getBalance(TreeNode* node) {
45. if (node == nullptr)
46. return 0;
47. return getHeight(node->left) -
getHeight(node->right);
48. }
49.
50. TreeNode* rightRotate(TreeNode* y) {
51. TreeNode* x = y->left;
52. TreeNode* T2 = x->right;
53.
54. x->right = y;
55. y->left = T2;
56.
57. y->height = 1 + max(getHeight(y->left),
getHeight(y->right));
58. x->height = 1 + max(getHeight(x->left),
getHeight(x->right));
59.
60. return x;
61. }
62.
63. TreeNode* leftRotate(TreeNode* x) {
64. TreeNode* y = x->right;
65. TreeNode* T2 = y->left;
66.
67. y->left = x;
68. x->right = T2;
69.
70. x->height = 1 + max(getHeight(x->left),
getHeight(x->right));
71. y->height = 1 + max(getHeight(y->left),
getHeight(y->right));
72.
73. return y;
74. }
75.
76. TreeNode* insert(TreeNode* node, int value) {
77. if (node == nullptr)
78. return new TreeNode{value, nullptr,
nullptr, 1};
79.
80. if (value < node->data)
81. node->left = insert(node->left, value);
82. else if (value > node->data)
83. node->right = insert(node->right,
value);
84. else
85. return node;
86.
87. node->height = 1 + max(getHeight(node-
>left), getHeight(node->right));
88.
89. int balance = getBalance(node);
90.
if (balance > 1 && value < node->left->data)
return rightRotate(node);
91. if (balance < -1 && value > node->right-
>data)
return leftRotate(node);
if (balance > 1 && value > node->left->data) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && value < node->right->data)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
int main() {
TreeNode* root = nullptr;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
return 0;
}
Practical Class-7
24. Q1) Max Heap
25. #include <iostream>
26. using namespace std;
27.
28. class MaxHeap {
29. private:
30. int* heap;
31. int capacity;
32. int size;
33.
34. int parent(int i) {
35. return (i - 1) / 2;
36. }
37.
38. int leftChild(int i) {
39. return 2 * i + 1;
40. }
41.
42. int rightChild(int i) {
43. return 2 * i + 2;
44. }
45.
46. void heapifyUp(int i) {
47. while (i > 0 && heap[i] >
heap[parent(i)]) {
48. swap(heap[i], heap[parent(i)]);
49. i = parent(i);
50. }
51. }
52.
53. void heapifyDown(int i) {
54. int maxIndex = i;
55. int left = leftChild(i);
56. int right = rightChild(i);
57.
58. if (left < size && heap[left] >
heap[maxIndex])
59. maxIndex = left;
60.
61. if (right < size && heap[right] >
heap[maxIndex])
62. maxIndex = right;
63.
64. if (i != maxIndex) {
65. swap(heap[i], heap[maxIndex]);
66. heapifyDown(maxIndex);
67. }
68. }
69.
70. public:
71. MaxHeap(int capacity) {
72. this->capacity = capacity;
73. heap = new int[capacity];
74. size = 0;
75. }
76.
77. ~MaxHeap() {
78. delete[] heap;
79. }
80.
81. void insert(int value) {
82. if (size >= capacity) {
83. cout << "Heap overflow\n";
84. return;
85. }
86. heap[size++] = value;
87. heapifyUp(size - 1);
}
88.
int extractMax() {
if (size <= 0) {
cout << "Heap underflow\n";
return -1;
}
int max = heap[0];
heap[0] = heap[--size];
heapifyDown(0);
return max;
}
void printHeap() {
for (int i = 0; i < size; ++i)
cout << heap[i] << " ";
cout << endl;
}
};
int main() {
MaxHeap maxHeap(10);
maxHeap.insert(10);
maxHeap.insert(20);
maxHeap.insert(15);
maxHeap.insert(30);
maxHeap.insert(25);
return 0;
}
Practical Class-8
25. Q1) Heap Sort
#include <iostream>
using namespace std;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
heapSort(arr, n);
return 0;
}