You are on page 1of 34

Data Structures and Algorithms

Group A:

1. Describe circular Queue operations in array implementation.

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.

2. What is the difference between a PUSH and a POP?

● 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.

3. What are the different types of linked lists?

● Singly linked list


● Doubly linked list
● Circular linked list
● Doubly circular linked list
● Sorted linked list
● Unsorted linked list

4. What is the LIFO and FIFO principle?

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

5. State the difference between stack and queue.

6. List out the basic operations that can be performed on a stack .

The basic operations that can be performed on a stack are:

● Push: Adds an item to the top of the stack.


● Pop: Removes the item at the top of the stack.
● Peek or Top: Returns the item at the top of the stack without removing it.
● isEmpty: Returns true if the stack is empty, false otherwise.
● isFull: Returns true if the stack is full, false otherwise (applicable for fixed-size stack
implementations).
● Size: Returns the number of items currently in the stack.

7. What is the meaning of the stack overflow condition?

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.

8. What is a priority queue?

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.

10. What do you mean by BFS and DFS?

● Breadth-First Search (BFS) is a method of traversing a tree in a level-by-level order.


Starting from the root node, we visit all nodes on the current level before moving on to
the nodes on the next level. This method uses a queue to keep track of the nodes to be
visited.

● Depth-First Search (DFS) is a method of traversing a tree in a depth-first order. Starting


from the root node, we visit the leftmost branch of the tree until we reach the leaf node,
and then backtrack to visit the other branches of the tree. There are three types of DFS:
pre-order (visit the node, then its left subtree, then its right subtree), in-order (visit the left
subtree, then the node, then the right subtree), and post-order (visit the left subtree, then
Data Structures and Algorithms

the right subtree, then the node). This method uses a stack to keep track of the nodes to
be visited.

11. What are the objectives of studying data structures?


The objectives of studying data structures are:

● To learn how to organise data in an efficient and effective way.


● To understand the strengths and weaknesses of different data structures for different
types of problems.
● To develop problem-solving skills by applying data structures to solve real-world
problems.
● To optimise algorithms for better performance by using appropriate data structures.
● To improve software engineering skills by designing and implementing efficient and
maintainable data structures.

12. What are the types of queues?

There are several types of queues, including:

● 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

13. State the difference between queues and linked lists

14. Mention the advantages of representing stacks using linked lists


than arrays.

- Dynamic memory allocation


- No overflow or underflow issues
- Easy to implement
- Faster insertion and deletion operations
- No wasted memory

15. What are the different binary tree traversal techniques?

There are three different binary tree traversal techniques:


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.

16. What do you mean by balanced trees?

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.

17. Define adjacent nodes.


Adjacent nodes are nodes in a graph that are connected to each other by an edge. In other
words, two nodes are said to be adjacent if there is an edge between them in the graph. The set
of all adjacent nodes to a particular node is called its neighborhood.

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.

18. Name two algorithms to find a minimum spanning tree.


The two algorithms to find a minimum spanning tree in a list format:

1. Kruskal's algorithm
2. Prim's algorithm

19. Define bubble sort.


Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until
they are in the intended order.
Data Structures and Algorithms

20. Define Hashing.


Hashing is a technique used to efficiently store and retrieve data in a data structure called a
hash table. It involves mapping data elements to unique identifiers called hash values using a
hash function
21. What do you mean by linear data structure and non-linear data
structure?
Linear data structures are those in which the data elements are arranged in a sequential order,
with each element having a unique predecessor and successor, except for the first and last
elements. Examples of linear data structures include arrays, linked lists, stacks, and queues.

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.

23. What is doubly link list?


A doubly linked list is a type of linked list in which each node has two pointers, one pointing to
the previous node and the other pointing to the next node in the sequence. This allows for
bi-directional traversal of the list, making it easy to traverse both forward and backward. The
doubly linked list provides better flexibility than a singly linked list and allows for faster insertions
and deletions at any position in the list. However, it requires more memory than a singly linked
list because each node needs to store two pointers instead of one.
Data Structures and Algorithms

24. What is a circular queue?


A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out)
principle except that the last position is connected to the first position in a circular queue that
forms a circle. It is also known as a Ring Buffer.

25. Why is linked list preferred over array?

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.

26. What is sorting?

Sorting is the process of arranging a collection of data or elements in a particular order,


typically in ascending or descending order. The goal of sorting is to make it easier to
search, filter, or analyze the data.

27. Define Iterative approach.

Iterative approach refers to a method of solving problems or performing computations through a


sequence of repeated steps. In an iterative approach, a process is broken down into smaller,
more manageable steps that can be executed repeatedly until a desired result is achieved.

28. What are rear and front in the queue.

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 dequeue an element, we remove A from the front of the queue.


- If we enqueue a new element E, it is added to the rear end of the queue after D, becoming the
new rear element.
Data Structures and Algorithms

- If we enqueue another new element F, it is added to the rear end of the queue after E,
becoming the new rear element.

29. Explain what do you understand by FIFO and LIFO?

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.

30. How does stack differ from queue.


Data Structures and Algorithms

Group B:

1. What do you mean by Quick Sort? Explain with examples.

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

2. Write a java class to implement a stack with push function.

// Stack implementation in Java

class Stack {

// store elements of stack


private int arr[];
// represent top of stack
private int top;
// total capacity of the stack
private int capacity;

// Creating a stack
Stack(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}

// push elements to the top of stack


public void push(int x) {
if (isFull()) {
System.out.println("Stack OverFlow");

// terminates the program


System.exit(1);
}

// insert element on top of stack


System.out.println("Inserting " + x);
arr[++top] = x;
}

// pop elements from top of stack


public int pop() {

// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}

// pop element from top of stack


return arr[top--];
}

// return size of the stack


Data Structures and Algorithms

public int getSize() {


return top + 1;
}

// check if the stack is empty


public Boolean isEmpty() {
return top == -1;
}

// check if the stack is full


public Boolean isFull() {
return top == capacity - 1;
}

// display elements of stack


public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}

public static void main(String[] args) {


Stack stack = new Stack(5);

stack.push(1);
stack.push(2);
stack.push(3);

System.out.print("Stack: ");
stack.printStack();

}
}
Data Structures and Algorithms

3. What are the types of Binary Trees? Explain with examples.


Data Structures and Algorithms
Data Structures and Algorithms

4. Explain Graph Representation in Data Structure using Adjacency


Matrix, Adjacency List and Multi List.
5. Explain the concept of BFS and DFS.

6. What is an algorithm? Write down the features of an algorithm.

Ans-:An algorithm is a step-by-step procedure for solving a problem or accomplishing a


task. It is a finite set of instructions that take input as data, process it and provides the
desired output.

Features of an algorithm:

1. Well-defined inputs: An algorithm takes a set of inputs as parameters, which are


well-defined and clearly specified. The algorithm specifies what inputs are
required and their expected format.
2. Clear and unambiguous steps: An algorithm provides a clear and unambiguous
description of the steps to be performed. Each step should be well-defined,
understandable, and executable.
3. Finiteness: An algorithm should terminate after a finite number of steps. It should
not run indefinitely or result in an infinite loop.
4. Deterministic: An algorithm produces the same output for the same set of inputs
every time it is executed. It follows a deterministic approach and does not involve
any randomness or unpredictability.
5. Effective and efficient: An algorithm should be effective, meaning it should solve
the problem it is designed for. Additionally, it should be efficient, taking into
consideration factors such as time complexity and space complexity to ensure
optimal performance.
6. Language independent: An algorithm should be independent of any specific
programming language or hardware. It focuses on the logical steps rather than
the implementation details.
Data Structures and Algorithms

7. Describe circular Queue operations in array implementation.

8. What is sorting? Describe the Insertion.

Sorting is the process of arranging a collection of data or elements in a particular order,


typically in ascending or descending order. The goal of sorting is to make it easier to
search, filter, or analyze the data.

Types of sorts are -


● Merge
Data Structures and Algorithms

● 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.

Here's an example of Insertion Sort in action:

Consider the following unsorted 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

9. What is the difference between a two dimensional array and a


multidimensional array?

10. Explain the tower of Hanoi algorithm.

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 is the algorithm to solve the Tower of Hanoi puzzle recursively:

1. Define a function hanoi(n, start, end, aux):


2. If n == 1, move the top disk from the start pole to the end pole and return
3. Call hanoi(n-1, start, aux, end)
4. Move the nth disk from start pole to end pole
5. Call hanoi(n-1, aux, end, start)
Data Structures and Algorithms

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.

11. Explain the infix to postfix conversion algorithm.


Data Structures and Algorithms

12. What do you mean by a double linked list? Explain with examples.
Data Structures and Algorithms

13. Differentiate between pre-order traversal and inorder traversal.

14. Differentiate between sequential searching and binary searching.

15. Discuss Kruskal's algorithm with an example.


Data Structures and Algorithms
Data Structures and Algorithms

Group C

1. What is Postfix expression? Convert infix expression


a+(b+c*(d+e))+f/g.

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;

public Node(int data) {


this.data = data;
Data Structures and Algorithms

this.prev = null;
this.next = null;
}
}

class DoublyLinkedList {
Node head;

public void insertAtBeginning(int data) {


Node newNode = new Node(data);
newNode.next = head;
if (head != null) {
head.prev = newNode;
}
head = newNode;
}

public void insertAtEnd(int data) {


Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node currNode = head;
while (currNode.next != null) {
currNode = currNode.next;
}
currNode.next = newNode;
newNode.prev = currNode;
}

public void deleteNode(int key) {


if (head == null) {
return;
}
Node currNode = head;
while (currNode != null && currNode.data != key) {
Data Structures and Algorithms

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

3. What is binary search tree? Explain with an example. Write an


algorithm to search, insert and delete node in binary search tree.

4. What are external and internal sorting? Explain partition strategies of


Merge sort and Quick sort. Trace these sort algorithms for following
data:
11 45 61 33 55 9 83 25

lab ko copy ma xa . marg ko ho vani


Data Structures and Algorithms

5. Convert Infix to Postfix.


a+(b+c*(d+e))+f/g

6. Write a program in Java to find the Fibonacci series using recursion.

//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) +"
");
}
}
}

Fibonacci Series of 10 numbers: 0 1 1 2 3 5 8 13 21


34
Data Structures and Algorithms

7. Consider the following Inorder and Preorder traversal of a tree

Pre Order - F A E K C D H G B
Inorder – E A C K F H D B G

8. Write a java program to find the Quick Sort.


-skip

9. Define Queue. Write are different applications of queue? Explain


queue operations with example.
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms

10. Convert Infix to Postfix.a+(b+c*(d+e))+f/g .What is linked


list?Explain the process of inserting and removing nodes from a linked
list

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:

11. What is shortest path? Explain Dijkstra algorithm for finding


shortest path using suitable example.

12. Discuss depth first and breadth first traversal of a graph with
suitable example.
Data Structures and Algorithms

Group D

1. Consider a singly linked list implementation in Java.


Questions:
a. Write a Java method to creation and traverse the linked list and
print the value of each node in the list.
b. Write a Java method to insert a new node at the beginning, at the
end and at the specific position of the linked list.
c. Write a Java method to delete a new node at the beginning, at the
end and at the specific position of the linked list.

2. Design a program to implement a stack data structure using Java


programming.
Questions:
a. Write an algorithm to push an element onto the stack and explain
the steps involved.
b. Write a Java code to implement the push() method in the stack.
c. Discuss the time complexity of the push() method and suggest
possible improvements to optimize the performance of the stack
data structure.

3. Design a program to implement a queue data structure using Java


programming.
Questions:
a. Write an algorithm to enqueue an element into a queue and
explain the steps involved.
b. Write a Java code to implement the enqueue() method in a
queue.
c. Discuss the time complexity of the enqueue() method and suggest
possible improvements to optimize the performance of the queue
data structure.

You might also like