DSA Solved Ans
DSA Solved Ans
(2023)
Q] Define Data Object?
--> A data object is a region of storage that contains a value or group of values.
Each value can be accessed using its identifier or a more complex expression that
refers to the object. In addition, each object has a unique data type.
Q] Define stable sorting ?
--> A sorting algorithm is said to be stable if two objects with equal keys appear in
the same order in sorted output as they appear in the input data set.
Q] List Linear search variations ?
--> Sequential search algorithm are:
(1) the sentinel search, (2) the probability search, and (3) the ordered list search.
Q] What is time complexity of merge sort ?
--> Merge Sort is a recursive algorithm and time complexity can be expressed as
following recurrence relation. T(n) = 2T(n/2) + O(n)
Q] Define term Null list?
--> List reference can be null in a special-cased zero value for reference types.
Q] Applications of linked list ?
--> Application of Linked List
1.Implementing Stacks.
2.Queues using Linked List.
3.Implementation of Graphs.
4.Implementing Hash Tables.
5.Portray a Polynomial with a Linked List.
6.Large-Number-Arithmetic.
7.Linked allocation of files.
8.Memory Management with Linked List.
--> Linked List is a linear data structure, in which the elements are not stored at
contiguous memory locations. The elements in a linked list are linked using
pointers. It is implemented on the heap memory rather than the stack memory.
Q] Write node structure of doubly linked list?
--> Representation of Doubly Linked List
1.For node one: next stores the address of two and prev stores null (there is no
node before it)
2.For node two: next stores the address of three and prev stores the address of one.
3.For node three: next stores null (there is no node after it) and prev stores the
address of two .
Q] What is Top of the stack ?
--> The "top" element of the stack is the element that was last pushed and will be
the first to be popped.
Q] Define recursion ?
--> Recursion in the data structure can be defined as a method through which
problems are broken down into smaller sub-problems to find a solution. This is
done by replicating the function on a smaller scale, making it call itself and then
combining them together to solve problems.
Q] What is circular quene?
--> A Circular Queue is an extended version of a normal queue where the last
element of the queue is connected to the first element of the queue forming a
circle. The operations are performed based on FIFO (First In First Out) principle.
Q] Describe the term ADT ?
--> Abstract data type (ADT) is a mathematical model for data types, defined by its behavior from the
point of view of a user of the data, specifically in terms of possible values, possible operations on data of
this type, and the behavior of these operations.ADTs provide a way to encapsulate data and operations
into a single unit, making it easier to manage and modify the data structure.ADTs allow users to work
with data structures without having to know the implementation details, which can simplify programming
and reduce errors.ADTs can protect the integrity of data by controlling access and preventing
unauthorized modifications.
Q] What is the best case and worst case efficiency quick sort ?
--> Best case occurs generally when the list is in completely random manner.
Worst case occurs when the array is already sorted either in ascending or
descending order.
Q] What is divide and conquer stratergy?
--> Divide-and-conquer solves a large problem by recursively breaking it down
into smaller subproblems until they can be solved directly. Divide-and-conquer
works in three steps: divide, conquer, and combine. It is more efficient than brute
force approaches. It is prone to stack overflow error due to the use of recursion.
Q] Define Stack overflow?
--> Stack overflow is a type of buffer overflow error that occurs when a computer program tries
to use more memory space in the call stack than has been allocated to that stack.
int main() {
int n, key;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
Q] Write a C function to delete a node from singly circular linked list at any ?
--> #include <stdio.h>
#include <stdlib.h>
// Function to insert a node at the end of the singly circular linked list
struct Node* insertEnd(struct Node* last, int data) {
if (last == NULL) {
// If the list is empty, create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode; // Point to itself since it's the only node
return newNode;
} else {
// If the list is not empty, create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = last->next; // Make the new node point to the first node
last->next = newNode; // Make the last node point to the new node
return newNode;
}
}
// Function to delete a node from a given position in the singly circular linked
list
struct Node* deleteNode(struct Node* last, int position) {
if (last == NULL) {
printf("List is empty. Cannot delete from an empty list.\n");
return NULL;
}
Scan "P":
Scan "*":
Scan "Q":
Scan "+":
Pop "*" from the stack (higher precedence than "+") and add it to the output.
Push "+" onto the stack.
Stack: +
Output: P Q *
Scan "R":
Scan "/":
Pop "+" from the stack (same precedence as "/") and add it to the output.
Push "/" onto the stack.
Stack: /
Output: P Q * R +
Scan "S":
Pop "/" from the stack (lower precedence than "-") and add it to the output.
Push "-" onto the stack.
Stack: -
Output: P Q * R + S /
Scan "T":
Final Result: P Q * R + S / T -
Q] Define Deque. List types of Deque and explain any two operations
performed on Deque.
--> Deque, the data can be inserted and deleted from both front and rear ends.
Unlike queue, deque in data structure doesn't follow the FIFO rule (First in first
out). There are two types of deque in data structure Input restricted queue and
Output restricted queue.
Q]Define
1] Data structure?
--> A data structure is a specialized format for organizing, processing, retrieving
and storing data.
2] Omega notation?
--> Omega notation represents the lower bound of the running time of an
algorithm.Thus, it provides the best case complexity of an algorithm. Omega gives
the lower bound of a function Ω(g(n)) = { f(n): there exist positive constants c and
n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
3] Time complexity?
--> Time complexity is a type of computational complexity that describes the time
required to execute an algorithm. The time complexity of an algorithm is the
amount of time it takes for each statement to complete. As a result, it is highly
dependent on the size of the processed data.
4] Big Oh?
--> Big O Notation is a tool used to describe the time complexity of algorithms. It
calculates the time taken to run an algorithm as the input grows. In other words, it
calculates the worst-case time complexity of an algorithm.
5] Theta notation?
--> Theta notation encloses the function from above and below. Since it represents
the upper and the lower bound of the running time of an algorithm.
6] Space complexity ?
--> Space complexity refers to the total amount of memory space used by an
algorithm/program, including the space of input values for execution.
Q] Write a short note on priority queue?
--> Priority queue in a data structure is an extension of a linear queue that
possesses the following properties: Every element has a certain priority assigned to
it. Every element of this queue must be comparable. It will delete the element with
higher priority before the element with lower priority.
Step 2:
Process each symbol in the expression.
Push operands directly to the output.
Push operators onto the stack based on their precedence and associativity.
Symbol: ( (Open parenthesis has the highest precedence)
Stack: [ ( ]
Output: []
Step 3:
Process the next symbol.
Symbol: A
Stack: [ ( ]
Output: [ A ]
Step 4:
Process the next symbol.
Symbol: -
Stack: [ ( ]
Output: [ A ]
Step 5:
Process the next symbol.
Symbol: B
Stack: [ ( ]
Output: [ A, B ]
Step 6:
Process the next symbol.
Symbol: /
Stack: [ (, / ]
Output: [ A, B ]
Step 7:
Process the next symbol.
Symbol: C
Stack: [ (, / ]
Output: [ A, B, C ]
Step 8:
Process the next symbol.
Symbol: )
Stack: [ ( ]
Output: [ A, B, C, / ]
Step 9:
Process the next symbol.
Symbol: *
Stack: [ (, * ]
Output: [ A, B, C, / ]
Step 10:
Process the next symbol.
Symbol: (
Stack: [ (, * ]
Output: [ A, B, C, / ]
Step 11:
Process the next symbol.
Symbol: D
Stack: [ (, * ]
Output: [ A, B, C, /, D ]
Step 12:
Process the next symbol.
Symbol: /
Stack: [ (, *, / ]
Output: [ A, B, C, /, D ]
Step 13:
Process the next symbol.
Symbol: E
Stack: [ (, *, / ]
Output: [ A, B, C, /, D, E ]
Step 14:
Process the next symbol.
Symbol: -
Stack: [ (, *, /, - ]
Output: [ A, B, C, /, D, E ]
Step 15:
Process the next symbol.
Symbol: F
Stack: [ (, *, /, - ]
Output: [ A, B, C, /, D, E, F ]
Step 16:
Process the next symbol.
Symbol: )
Stack: [ (, *, / ]
Output: [ A, B, C, /, D, E, F, - ]
Step 17:
Process the next symbol.
Symbol: )
Stack: [ (, * ]
Output: [ A, B, C, /, D, E, F, -, / ]
Step 18:
Process the next symbol.
Symbol: (No more symbols)
Stack: [ ( ]
Output: [ A, B, C, /, D, E, F, -, /, * ]
Step 19:
Pop any remaining operators from the stack and append to the output.
Stack: []
Output: [ A, B, C, /, D, E, F, -, /, *, - ]
So, the postfix expression is "AB-C/DE/F-*".
Stacks Queues
Stacks are based on the LIFO principle, i.e., Queues are based on the FIFO principle, i.e.,
the element inserted at the last, is the first the element inserted at the first, is the first
element to come out of the list. element to come out of the list.
Stacks are often used for tasks that require Queues are often used for tasks that involve
backtracking, such as parsing expressions or processing elements in a specific order, such
implementing undo functionality. as handling requests or scheduling tasks.
Types Description
Q] Null Pointer ?
--> The Null Pointer is the pointer that does not point to any location but NULL.
Syntax :-
type pointer_name = NULL;
type pointer_name = 0;
Q] External pointer ?
--> External pointer is a pointer that points to the very first node in the linked list,
it enables us to access the entire linked list.
Q] Write a C function to insert a node in circular doubly linked lists ?
--> #include <stdio.h>
#include <stdlib.h>
if (last == NULL) {
// If the list is empty, make the new node point to itself
newNode->next = newNode;
newNode->prev = newNode;
return newNode;
} else {
// Insert the new node at the end of the list
newNode->next = last->next;
last->next->prev = newNode;
newNode->prev = last;
last->next = newNode;
return last;
}
}
do {
printf("%d <-> ", temp->data);
temp = temp->next;
} while (temp != last->next);
printf("(head)\n");
}
int main() {
struct Node* la
Q] Write a algorithm for tranversing a linked list and also give its function?
• --> Start from the head of the linked list.
• While the current node is not NULL:
• Perform the desired operation on the data of the current node.
• Move to the next node by updating the current node to the next node.
• End.
return head;
}
printf("\n
// Swap arr[i+1] and arr[high] to place the pivot in the correct position
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
int main() {
int arr[] = {108, 3, 97, 65, 71, 23, 57, 93, 100};
int arrSize = sizeof(arr) / sizeof(arr[0]);
return 0;
}
2] Searching:
• Linear Search:
• Simple search method that sequentially checks each element in a collection until a
match is found.
• Works well for small datasets or unordered lists.
• Time complexity is O(n) in the worst case.
• Binary Search:
• Applicable to sorted arrays or lists.
• Divides the dataset in half by comparing the target value with the middle element.
• Repeats this process until the element is found or the search space is empty.
• Time complexity is O(log n) in the worst case.