Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views18 pages

DSA Questions

The document covers various data structures and algorithms, including arrays, linked lists, stacks, queues, binary search, linear search, and sorting algorithms. It explains key concepts, time complexities, and provides code implementations for reversing linked lists, implementing stacks and queues, and searching algorithms. Additionally, it compares different sorting algorithms and discusses their stability and complexities.

Uploaded by

muntahaj09
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)
15 views18 pages

DSA Questions

The document covers various data structures and algorithms, including arrays, linked lists, stacks, queues, binary search, linear search, and sorting algorithms. It explains key concepts, time complexities, and provides code implementations for reversing linked lists, implementing stacks and queues, and searching algorithms. Additionally, it compares different sorting algorithms and discusses their stability and complexities.

Uploaded by

muntahaj09
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/ 18

Logical questions (DSA)

1. Arrays:
o What is an array, and how is it different from a linked list?
o Explain the time complexity for accessing an element in an array.
o How can you find the maximum and minimum elements in an array efficiently?

ANS:

1.1 An array is a collection of elements stored in contiguous memory locations, while


a linked list is a collection of nodes where each node contains a data element and a
reference to the next node.

1.2 Time complexity for accessing an element in an array is O(1) if the index is known.

1.3 To find the maximum and minimum elements in an array efficiently, you can
traverse the array once, keeping track of the maximum and minimum elements
encountered so far.

2. Linked Lists:
o Describe the concept of a singly linked list.
o How do you reverse a linked list?
o What is the time complexity for inserting an element at the beginning of a linked list?
3. Explain the concept of a singly linked list and a doubly linked list.
4. Write code to reverse a linked list.

ANS:

2.1 A singly linked list is a data structure where each element, called a node, contains a data
field and a reference to the next node in the sequence.

2.2 To reverse a linked list, you traverse the list, changing the next pointer of each node to
point to the previous node.

2.3 Time complexity for inserting an element at the beginning of a linked list is O(1).

Singly linked list vs Doubly linked list:

3.1 In a singly linked list, each node points to the next node in the sequence. In a doubly
linked list, each node contains references to both the next and previous nodes.

4.Reversing a linked list:


o struct Node {
o int data;
o Node* next;
o Node(int val) : data(val), next(nullptr) {}
o };
o
o Node* reverse_linked_list(Node* head) {
o Node* prev = nullptr;
o Node* current = head;
o while (current) {
o Node* next_node = current->next;
o current->next = prev;
o prev = current;
o current = next_node;
o }
o return prev;
o }
5. Stacks and Queues:

What is the LIFO property of a stack?

Ans: LIFO property of a stack means that the last element added to the stack will be the first one to
be removed.

How can you implement a queue using two stacks?

You can implement a queue using two stacks by maintaining one stack for enqueue operations and
another for dequeue operations.

HOW CAN A STACK BE USED IN FUNCTION CALL?

Ans: A stack is used in function call management to store information about the function calls,
including parameters, return addresses, and local variables.

6. Implement a stack using an array or linked list.

#include <iostream>

using namespace std;

#define MAX_SIZE 100


class Stack {

private:

int top;

int arr[MAX_SIZE];

public:

Stack() {

top = -1;

bool isEmpty() {

return top == -1;

bool isFull() {

return top == MAX_SIZE - 1;

void push(int val) {

if (isFull()) {

cout << "Stack overflow!" << endl;

return;

}
arr[++top] = val;

int pop() {

if (isEmpty()) {

cout << "Stack underflow!" << endl;

return -1;

return arr[top--];

int peek() {

if (isEmpty()) {

cout << "Stack is empty!" << endl;

return -1;

return arr[top];

};

int main() {

Stack myStack;

myStack.push(1);
myStack.push(2);

myStack.push(3);

cout << "Top element: " << myStack.peek() << endl;

cout << "Popping elements: ";

while (!myStack.isEmpty()) {

cout << myStack.pop() << " ";

cout << endl;

return 0;

#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

Node(int val) : data(val), next(nullptr) {}

};
class Stack {

private:

Node* top;

public:

Stack() {

top = nullptr;

bool isEmpty() {

return top == nullptr;

void push(int val) {

Node* newNode = new Node(val);

newNode->next = top;

top = newNode;

int pop() {

if (isEmpty()) {

cout << "Stack underflow!" << endl;

return -1;

}
int val = top->data;

Node* temp = top;

top = top->next;

delete temp;

return val;

int peek() {

if (isEmpty()) {

cout << "Stack is empty!" << endl;

return -1;

return top->data;

};

int main() {

Stack myStack;

myStack.push(1);

myStack.push(2);

myStack.push(3);

cout << "Top element: " << myStack.peek() << endl;


cout << "Popping elements: ";

while (!myStack.isEmpty()) {

cout << myStack.pop() << " ";

cout << endl;

return 0;

7. Evaluate a postfix expression using a stack.


8. Check if parentheses in an expression are balanced using a stack.

Balancing parentheses using a stack:

Push opening parentheses onto the stack and pop when a closing parenthesis is encountered. At
the end, if the stack is empty, parentheses are balanced.

9. Explain the FIFO (First-In-First-Out) property of queues.

FIFO property of queues:


It means that the first element added to the queue will be the first one to be removed.

10. Implement a queue using an array or linked list.

#include <iostream>

using namespace std;

#define MAX_SIZE 100


class Queue {

private:

int front, rear;

int arr[MAX_SIZE];

public:

Queue() {

front = rear = -1;

bool isEmpty() {

return front == -1 && rear == -1;

bool isFull() {

return rear == MAX_SIZE - 1;

void enqueue(int val) {

if (isFull()) {

cout << "Queue overflow!" << endl;

return;

if (isEmpty()) {
front = rear = 0;

} else {

rear++;

arr[rear] = val;

int dequeue() {

if (isEmpty()) {

cout << "Queue underflow!" << endl;

return -1;

int val = arr[front];

if (front == rear) {

front = rear = -1;

} else {

front++;

return val;

int peek() {

if (isEmpty()) {
cout << "Queue is empty!" << endl;

return -1;

return arr[front];

};

int main() {

Queue myQueue;

myQueue.enqueue(1);

myQueue.enqueue(2);

myQueue.enqueue(3);

cout << "Front element: " << myQueue.peek() << endl;

cout << "Dequeueing elements: ";

while (!myQueue.isEmpty()) {

cout << myQueue.dequeue() << " ";

cout << endl;

return 0;

}
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

Node(int val) : data(val), next(nullptr) {}

};

class Queue {

private:

Node* front;

Node* rear;

public:

Queue() {

front = rear = nullptr;

bool isEmpty() {

return front == nullptr && rear == nullptr;

}
void enqueue(int val) {

Node* newNode = new Node(val);

if (isEmpty()) {

front = rear = newNode;

} else {

rear->next = newNode;

rear = newNode;

int dequeue() {

if (isEmpty()) {

cout << "Queue underflow!" << endl;

return -1;

int val = front->data;

Node* temp = front;

if (front == rear) {

front = rear = nullptr;

} else {

front = front->next;

delete temp;
return val;

int peek() {

if (isEmpty()) {

cout << "Queue is empty!" << endl;

return -1;

return front->data;

};

int main() {

Queue myQueue;

myQueue.enqueue(1);

myQueue.enqueue(2);

myQueue.enqueue(3);

cout << "Front element: " << myQueue.peek() << endl;

cout << "Dequeueing elements: ";

while (!myQueue.isEmpty()) {

cout << myQueue.dequeue() << " ";


}

cout << endl;

return 0;

11. Binary Search:


o Describe the binary search algorithm.
o What is the time complexity of binary search?
o When is binary search most efficient?

14.2 Binary search is an algorithm to find an element in a sorted array by repeatedly


dividing the search interval in half.

o 14.2 Time complexity of binary search is O(log n).


o 14.3 Binary search is most efficient when the elements are sorted.
o Efficient scenarios for binary search: Binary search is efficient when working with large
sorted datasets.

Linear Search:

o Describe the linear search algorithm.


o Compare linear search with binary search.
o Write code to perform linear search on an array.

#include <iostream>

using namespace std;

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

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

if (arr[i] == target) {

return i; // return index if target is found

}
return -1; // return -1 if target is not found

int main() {

int arr[] = {3, 7, 2, 9, 4, 1, 6};

int n = sizeof(arr) / sizeof(arr[0]);

int target = 4;

int index = linear_search(arr, n, target);

if (index != -1) {

cout << "Element found at index " << index << endl;

} else {

cout << "Element not found" << endl;

return 0;

o Discuss the time complexity of linear search.


o Linear Search: 19.1 Linear search is a simple search algorithm that sequentially checks
each element in a list until the desired element is found or the end of the list is
reached.
o 19.2 Compared to binary search, linear search has a time complexity of O(n).

12. Sorting Algorithms:


o Compare bubble sort, insertion sort, and selection sort.
o Which sorting algorithm is stable?

Stability means that the relative order of equal elements is preserved after sorting.
Insertion sort is stable.

13. Compare their time complexities and space complexities.

Bubble Sort:

Time Complexity:

Worst Case: O(n^2)

Average Case: O(n^2)

Best Case (with optimizations): O(n)

Space Complexity: O(1)

Bubble sort compares adjacent elements and swaps them if they are in the wrong order,
iterating through the array until no more swaps are needed.

Insertion Sort:

Time Complexity:

Worst Case: O(n^2)

Average Case: O(n^2)

Best Case: O(n)

Space Complexity: O(1)

Insertion sort works by iterating through the array, taking one element at a time and
inserting it into its correct position among the already sorted elements.

Selection Sort:

Time Complexity:
Worst Case: O(n^2)

Average Case: O(n^2)

Best Case: O(n^2)

Space Complexity: O(1)

Selection sort repeatedly finds the minimum element from the unsorted part of the array and
swaps it with the first unsorted element.

Comparing their time complexities, bubble sort, insertion sort, and selection sort all have
quadratic time complexities in the worst and average cases. However, in terms of best-case
time complexity, insertion sort performs better than the other two, as it can achieve linear
time complexity when the array is already sorted.

You might also like