Professional Documents
Culture Documents
Question Collection DSA
Question Collection DSA
Group A:
Circular queue operations in array implementation include enqueue (adding an element to the
queue), dequeue (removing an element from the queue), front (getting the front element of the
queue), and rear (getting the rear element of the queue). The circular queue implementation
uses a fixed-size array, and the front and rear pointers move in a circular fashion to maintain the
structure of the queue.The operations are performed based on FIFO (First In First Out) principle
and the last position is connected back to the first position to make a circle.
● PUSH is used when you want to add more entries to a stack while POP is used to
remove entries from it.
● Push increases the stack size by one, POP decreases the stack size by one.
LIFO stands for "Last In, First Out," which means that the last item added to a data structure is
the first one to be removed. It follows the principle of stack operations, where the most recently
added item is the first one to be accessed or removed.
FIFO stands for "First In, First Out," which means that the first item added to a data structure is
the first one to be removed. It follows the principle of queue operations, where the oldest item in
the queue is the first one to be accessed or removed.
Data Structures and Algorithms
Stack overflow occurs when a program tries to store more data on the stack than its allocated
size, which can result in a program crash or other undefined behaviour. It's important to limit the
stack size and ensure proper exit conditions in recursive functions to prevent stack overflow.
A priority queue is a type of queue that arranges elements based on their priority values.
Elements with higher priority values are typically retrieved before elements with lower priority
values.
9. Write about tree traversal.
Data Structures and Algorithms
Tree traversal refers to the process of visiting each node of a tree data structure exactly once in
a systematic way.
the right subtree, then the node). This method uses a stack to keep track of the nodes to
be visited.
● Simple queue: In a simple queue, the first element that is added to the queue is the first
one to be removed.
● Circular queue: In a circular queue, the last element is connected to the first element,
forming a circular structure. This allows for efficient use of memory and faster
performance.
● Priority queue: In a priority queue, each element has a priority assigned to it. Elements
with higher priority are removed first.
● Deque (Double-ended queue): A deque is a queue where elements can be added and
removed from both ends.
● Blocking queue: A blocking queue is a type of queue where the addition of elements is
blocked when the queue is full, or the removal of elements is blocked when the queue is
empty.
● Concurrent queue: A concurrent queue is a type of queue that can be accessed by
multiple threads simultaneously.
These are some of the common types of queues used in computer programming and software
development.
Data Structures and Algorithms
1. Inorder traversal: This traversal visits the left subtree, then the root node, and finally the right
subtree. This traversal visits the nodes in ascending order if the binary tree is a search tree.
2. Preorder traversal: This traversal visits the root node, then the left subtree, and finally the
right subtree.
3. Postorder traversal: This traversal visits the left subtree, then the right subtree, and finally the
root node.
Balanced trees are binary trees where the height of the left and right subtrees of any node
differs by no more than one. In other words, a balanced tree is a tree where the difference
between the heights of the left and right subtrees of every node is either zero or one.
Balanced trees are desirable because they allow for efficient searching, insertion, and deletion
operations. Common examples of balanced trees include AVL trees, red-black trees, and
B-trees.
In a directed graph, adjacent nodes can be either a successor node (outgoing edge) or a
predecessor node (incoming edge) of a given node. In an undirected graph, adjacent nodes are
simply nodes that are connected by an edge, regardless of the direction of the edge.
1. Kruskal's algorithm
2. Prim's algorithm
Non-linear data structures are those in which the data elements are not arranged in a sequential
order, and each element may have multiple predecessors and successors, forming complex
relationships among the elements. Examples of non-linear data structures include trees, graphs,
and heaps.
22. What are the various operations that can be performed on different
data Structure?
The various operations that can be performed on different data structures are:
(1) Traversing: Accessing each record exactly once so that certain items in the record
may be processed.
(2) Searching: Finding the location of a particular record with a given key value, or
finding the location of all records which satisfy one or more conditions.
(3) Inserting: Adding a new record to the structure.
(4) Deleting: Removing the record from the structure.
(5) Sorting: Managing the data or record in some logical order(Ascending or descending
order).
(6) Merging: Combining the record in two different sorted files into a single sorted file.
Linked lists are preferred over arrays in certain situations because they allow for dynamic
memory allocation, efficient insertion and deletion operations, and can handle a varying number
of elements without requiring resizing. Additionally, linked lists allow for easy implementation of
more complex data structures such as stacks, queues, and trees. However, arrays can be faster
for random access to elements and have better cache locality.
In a queue data structure, the "front" and "rear" refer to the two ends of the queue.
The "front" of the queue is the end from which elements are dequeued or removed. When an
element is enqueued or added to the queue, it is added to the "rear" end of the queue.
For example, if we have a queue containing the elements A, B, C, and D in that order, with A at
the front and D at the rear, then:
- If we enqueue another new element F, it is added to the rear end of the queue after E,
becoming the new rear element.
FIFO stands for "First In, First Out", which means that the first element added to a queue will be
the first one to be removed.
LIFO stands for "Last In, First Out", which means that the last element added to a stack will be
the first one to be removed.
Group B:
Ans-: Quick sort is a popular sorting algorithm that follows the divide-and-conquer
strategy. It works by selecting a pivot element from the array and partitioning the other
elements into two sub-arrays, according to whether they are less than or greater than
the pivot. The sub-arrays are then recursively sorted using the same process.
Data Structures and Algorithms
Data Structures and Algorithms
class Stack {
// Creating a stack
Stack(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}
// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
}
}
Data Structures and Algorithms
Features of an algorithm:
● selection
● insertion
● bubble
● quick
In Insertion Sort, the array is divided into two parts: sorted and unsorted. The first
element of the array is considered as a sorted part of the array and the rest of the
elements are considered as an unsorted part of the array. In each iteration, the first
element of the unsorted part is picked up and compared with each element of the sorted
part of the array. The picked element is then inserted at the correct position in the sorted
part of the array.
[5, 2, 4, 6, 1, 3]
- In the first iteration, we take 2 from the unsorted part of the array and insert it in the
sorted part of the array to get [2, 5, 4, 6, 1, 3].
- In the second iteration, we take 4 from the unsorted part of the array and insert it in the
sorted part of the array to get [2, 4, 5, 6, 1, 3].
- In the third iteration, we take 6 from the unsorted part of the array and insert it in the
sorted part of the array to get [2, 4, 5, 6, 1, 3].
- In the fourth iteration, we take 1 from the unsorted part of the array and insert it in the
sorted part of the array to get [1, 2, 4, 5, 6, 3].
- In the fifth iteration, we take 3 from the unsorted part of the array and insert it in the
sorted part of the array to get [1, 2, 3, 4, 5, 6].
After the completion of all iterations, we get the sorted array as [1, 2, 3, 4, 5, 6].
Data Structures and Algorithms
The Tower of Hanoi is a classic mathematical puzzle which involves three rods and a series of
discs of varying sizes which can slide onto any rod. The puzzle starts with the discs in a neat
stack in ascending order of size on one rod, the smallest at the top, thus forming a conical
shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following
simple rules:
1. Only one disc can be moved at a time.
2. Each move consists of taking the upper disc from one of the stacks and placing it on top of
another stack or on an empty rod.
3. No larger disc may be placed on top of a smaller disc.
Here, n denotes the number of disks, start denotes the starting rod, end denotes the ending rod,
and aux denotes the auxiliary rod.
The algorithm recursively calls itself by moving n-1 disks from the start pole to the auxiliary pole,
then moves the nth disk from the start pole to the end pole, and then again recursively moves
the n-1 disks from the auxiliary pole to the end pole. This process continues until all the disks
are moved from the start pole to the end pole.
12. What do you mean by a double linked list? Explain with examples.
Data Structures and Algorithms
Group C
Postfix notation, also known as Reverse Polish Notation (RPN), is a way of writing
mathematical expressions in which the operator follows the operands. In other words,
the operator comes after the two operands that it operates on. For example, the infix
expression "2 + 3" would be written in postfix notation as "2 3 +".
Data Structures and Algorithms
2. Differentiate between singly linked list and doubly linked list. How do
you insert and delete a node from doubly linked list.
class Node {
int data;
Node prev;
Node next;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
Node head;
currNode = currNode.next;
}
if (currNode == null) {
return;
}
if (currNode.prev != null) {
currNode.prev.next = currNode.next;
} else {
head = currNode.next;
}
if (currNode.next != null) {
currNode.next.prev = currNode.prev;
}
}
}
Data Structures and Algorithms
//Using Recursion
public class FibonacciCalc{
public static int fibonacciRecursion(int n){
if(n == 0){
return 0;
}
if(n == 1 || n == 2){
return 1;
}
return fibonacciRecursion(n-2) +
fibonacciRecursion(n-1);
}
public static void main(String args[]) {
int maxNumber = 10;
System.out.print("Fibonacci Series of
"+maxNumber+" numbers: ");
for(int i = 0; i < maxNumber; i++){
System.out.print(fibonacciRecursion(i) +"
");
}
}
}
Pre Order - F A E K C D H G B
Inorder – E A C K F H D B G
A 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 as shown in the below image:
12. Discuss depth first and breadth first traversal of a graph with
suitable example.
Data Structures and Algorithms
Group D