Professional Documents
Culture Documents
Data Structure
Data Structure
Syllabus :-
Time and space complexity, Data Structures – Introduction to Data Structures, abstract data types,
Linear list – singly linked list implementation, insertion, deletion and searching operations on linear
list, circular linked list implementation, Double linked list implementation, insertion, deletion and
searching operations. Applications of linked lists.
Stacks-Operations, array and linked representations of stacks, stack applications -infix to postfix
conversion, postfix expression evaluation, recursion implementation.
Searching and Sorting – Sorting- selection sort, bubble sort, insertion sort, quick sort, merge sort, shell sort,
radix sort, Searching-linear and binary search methods, comparison of sorting and searching methods
Trees – Definitions, tree representation, properties of trees, Binary tree, Binary tree representation, binary tree
properties, binary tree traversals, binary tree implementation, applications of trees.
A data structure is a storage that is used to store and organize data. It is a way of arranging data on
a computer so that it can be accessed and updated efficiently.
● Linear data structure: Data structure in which data elements are arranged sequentially or
linearly, where each element is attached to its previous and next adjacent elements, is called a
linear data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
○ Static data structure: Static data structure has a fixed memory size. It is easier to
access the elements in a static data structure.
An example of this data structure is an array.
○ Dynamic data structure: In dynamic data structure, the size is not fixed. It can be
randomly updated during the runtime which may be considered efficient concerning
the memory (space) complexity of the code.
Examples of this data structure are queue, stack, etc.
● Non-linear data structure: Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all
the elements in a single run only.
Examples of non-linear data structures are trees and graphs.
Popular types of Data Structures:
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of
the same type together. This makes it easier to calculate the position of each element by simply adding an offset
to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the
array).
Advantages:-
● Code Optimization: we can retrieve or sort the data efficiently.
● Random access: We can get any data located at an index position.
Disadvantages:-
● Size Limit: We can store only the fixed size of elements in the array. It doesn’t grow its size at runtime.
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.
a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the
list.
Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the
end. A circular linked list can be a singly circular linked list or doubly circular linked list.
1) Any node can be a starting point. We can traverse the whole list by starting from any point. We just need to
stop when the first visited node is visited again.
2) Useful for implementation of queue. Unlike this implementation, we don’t need to maintain two pointers for
front and rear if we use circular linked list. We can maintain a pointer to the last inserted node and front can
always be obtained as next of last.
Circular Doubly Linked Lists are used for implementation of advanced data structures like Fibonacci Heap.
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be
LIFO(Last In First Out) or FILO(First In Last Out).
A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In
First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came
first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most
recently added; in a queue, we remove the item the least recently added.
Queue is used when things don’t have to be processed immediately, but have to be processed in First
In First Out order like Breadth First Search.
1. When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk
Scheduling.
2. When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes. Examples include IO Buffers, pipes, file IO, etc.
3. In Operating systems:
○ Semaphores
○ FCFS ( first come first serve) scheduling, example: FIFO queue
○ Spooling in printers
○ Buffer for devices like keyboard
4. In Networks:
○ Queues in routers/ switches
○ Mail Queues
Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated
with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in
which they are removed). If in any case the elements have same priority, they are served as per their ordering in the
queue.
A Priority Queue is different from a normal queue, because instead of being a “first-in-first-out”, values come out in order
by priority. It is an abstract data type that captures the idea of a container whose elements have “priorities” attached to
them.
Dijkstra’s Shortest Path Algorithm using priority queue: When the graph is stored in the form of adjacency list or
matrix, priority queue can be used to extract minimum efficiently when implementing Dijkstra’s algorithm.
Prim’s algorithm: It is used to implement Prim’s Algorithm to store keys of nodes and extract minimum key node at every
step.
Heap Sort : Heap sort is typically implemented using Heap which is an implementation of Priority Queue.
Applications of Deque: Since Deque supports both stack and queue operations, it can be used as both.
The Deque data structure supports clockwise and anticlockwise rotations in O(1) time which can be
useful in certain applications.
A Circular Queue is a special version of 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. It is
also called ‘Ring Buffer’.
Binary Tree
tree is a popular data structure that is non-linear in nature. Unlike other data structures like array, stack,
queue, and linked list which are linear in nature, a tree represents a hierarchical structure. The ordering
information of a tree is not important. A tree contains nodes and 2 pointers. These two pointers are the left
child and the right child of the parent node. Let us understand the terms of tree in detail.
● Root: The root of a tree is the topmost node of the tree that has no parent node. There is only one
root node in every tree.
● Edge: Edge acts as a link between the parent node and the child node.
● Leaf: A node that has no child is known as the leaf node. It is the last node of the tree. There can
be multiple leaf nodes in a tree.
● Depth: The depth of the node is the distance from the root node to that particular node.
● Height: The height of the node is the distance from that node to the deepest node of the tree.
● Height of tree: The Height of the tree is the maximum height of any node.
Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary
tree can have only 2 children, we typically name them the left and right child.
● PreOrder Traversal: Here, the traversal is: root – left child – right child. It means that the root node is
traversed first then its left child and finally the right child.
● InOrder Traversal: Here, the traversal is: left child – root – right child. It means that the left child is
traversed first then its root node and finally the right child.
● PostOrder Traversal: Here, the traversal is: left child – right child – root. It means that the left child is
traversed first then the right child and finally its root node.
Tree
1 //Root Node
/\
2 3
/\ /\
4 5 6 7 //Leaf Nodes
Big Oh Notation, Ο :- The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time.
It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete.
For example, for a function f(n) Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
Omega Notation:- Ω The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It
measures the best case. For example, for a function f(n) Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤
c.f(n) for all n > n0. }
Theta Notation,:- θ The notation θ(n) is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows − θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all
n > n0. }
Which of the following is the correct way to declare a multidimensional array in Java?
a) int[] arr;
b) int arr[[]];
c) int[][]arr;
d) int[[]] arr;
KEY POINTS:-
● ArrayIndexOutOfBoundsException is a run-time exception and the compilation is error-free.
● Whenever a particular memory location is referred to, it is likely that the locations nearby are also referred, arrays are
stored as contiguous blocks in memory, so if you want to access array elements, spatial locality makes it to access
quickly.
● Arrays are of fixed size. If we insert elements less than the allocated size, unoccupied positions can’t be used again.
Wastage will occur in memory.
a) 1
b) 40
c) 74
d) -18
What data structure would you mostly likely see in non recursive implementation of a
recursive algorithm?
a) Linked List
b) Stack
c) Queue
d) Tree
a) *AB/CD+
b) AB*CD/+
c) A*BC+/D
d) ABCD+/*
Which of the following statement(s) about stack data structure is/are NOT correct?
a) Linked List are used for implementing Stacks
b) Top of the Stack always contain the new node
c) Stack is the FIFO data structure
d) Null link is present in the last node at the bottom of the stack
a) Reversing a string
b) Evaluation of postfix expression
c) Implementation of recursion
d) Job scheduling
Binary Search Tree is a node-based binary tree data structure which has the following properties:
● The left subtree of a node contains only nodes with keys lesser than the node’s key.
● The right subtree of a node contains only nodes with keys greater than the node’s key.
● The left and right subtree each must also be a binary search tree.
Heap :-- A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Generally,
Heaps can be of two types:
1. Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present
at all of it’s children. The same property must be recursively true for all sub-trees in that Binary
Tree.
2. Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present
at all of it’s children. The same property must be recursively true for all sub-trees in that Binary
Tree.
A normal queue, if implemented using an array of size MAX_SIZE, gets full when?
a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front
A linear collection of data elements where the linear node is given by means of pointer is
Called?
a) Linked list
b) Node list
c) Primitive list
d) Unordered list
The concatenation of two lists can be performed in O(1) time. Which of the following
variation of the linked list can be used?
a) Data
b) Link
c) Data and Link
d) Node
Which of the following points is/are not true about Linked List data structure when it is
compared with an array?
a) Arrays have better cache locality that can make them better in terms of performance
b) It is easy to insert and delete elements in Linked List
c) Random access is not allowed in a typical implementation of Linked Lists
d) Access of elements in linked list takes less time than compared to arrays
Which of the following sorting algorithms can be used to sort a random linked list with
minimum time complexity?
a) Insertion Sort
b) Quick Sort
c) Heap Sort
d) Merge Sort
Explanation: Time complexity of inserting a new node at the head of the list is O(n) because you have to traverse
through the list to find the tail node.
What is the best case time complexity of deleting a node in a Singly Linked list?
a) O (n)
b) O (n2)
c) O (nlogn)
d) O (1)
If the array is not full, then we can directly add the new element through the index. In this case, the time complexity
would be constant, i.e., O(1). If the array is full, we first need to copy the array into another array and add a new
element. In this case, the time complexity would be O(n).
In a circular queue, how do you increment the rear end of the queue?
a) rear++
b) (rear+1) % CAPACITY
c) (rear % CAPACITY)+1
d) rear–
In case of insertion into a linked queue, a node borrowed from the __________ list is
inserted in the queue.
a) AVAIL
b) FRONT
c) REAR
d) NULL
a) ^
b) +
c) /
d) #
Reverse polish Notation is the other name for a postfix expression whereas Polish
Notation, Warsaw notation are the other names for a prefix expression.
Which of these operators have the highest order of precedence?
a) +A*CB
b) *B+AC
c) +A*BC
d) *A+CB
A binary tree is a rooted tree and also an ordered tree (i.e) every node in a binary tree has at most two children.
Breadth first traversal, also known as level order traversal is the traversal strategy used in a binary tree. It involves
visiting all the nodes at a given level.
Which of the following pair’s traversals on a binary tree can build the tree uniquely?
Which of the following tree data structures is not a balanced binary tree?
a) AVL tree
b) Red-black tree
c) Splay tree
d) B-tree
All the tree data structures given in options are balanced, but B-tree can have more than two children. AVL , red black
and splay tree are balanced binary tree.
Which of the following is not the self balancing binary search tree?
a) AVL Tree
b) 2-3-4 Tree
c) Red – Black Tree
d) Splay Tree
Explanation: 2-3-4 Tree is balanced search trees. But it is not a binary tree. So, it is not a
self balancing binary tree. AVL tree, Red-Black Tree and Splay tree are self balancing
binary search tree.
In AVL tree is a self – balancing binary search tree, in which the heights of the two child sub
trees of any node differ by _________
a) At least one
b) At most one
c) Two
d) At most two
Explanation: In an AVL tree, the difference between heights of the two child sub trees of any node is at most one. If
the height differs by more than one, AVL tree performs rotations to balance the tree.
1. General tree
A general tree data structure has no restriction on the number of nodes. It means that a parent node can have any
number of child nodes.
2. Binary tree
A node of a binary tree can have a maximum of two child nodes. In the given tree diagram, node B, D, and F are left
children, while E, C, and G are the right children.
3. Balanced tree
If the height of the left sub-tree and the right sub-tree is equal or differs at most by 1, the tree is known as a balanced tree.
As the name implies, binary search trees are used for various searching and sorting algorithms. The examples include
AVL tree and red-black tree. It is a non-linear data structure. It shows that the value of the left node is less than its parent,
while the value of the right node is greater than its parent.
1. Spanning trees: It is the shortest path tree used in the routers to direct the packets to the destination.
2. Binary Search Tree: It is a type of tree data structure that helps in maintaining a sorted stream of data.
Trie: It is a fast and efficient way for dynamic spell checking. It is also used for locating specific keys from
within a set.
Heap: It is also a tree data structure that can be represented in a form of an array. It is used to implement
priority queues.
a) 2-3 tree
b) Threaded binary tree
c) AA tree
d) Treap
a) Priority queue
b) Hash table
c) Heap sort
d) Priority queue and Heap sort
Explanation: Self-balancing binary search trees can be used to construct and maintain ordered lists,
to achieve the optimal worst case performance. So, self – balancing binary search tree can be used to
implement a priority queue, which is ordered list.
In which of the following self – balancing binary search tree the recently accessed element
can be accessed quickly?
a) AVL tree
b) AA tree
c) Splay tree
d) Red – Black tree
Splay tree:- In a Splay tree, the recently accessed element can be accessed quickly. In Splay tree, the
frequently accessed nodes are moved towards the root so they are quick to access again.
Treap, also known as random binary search tree, Random binary tree and Uniform spanning tree are all
random tree. Random tree is a tree formed by a random process of addition and deletion of nodes.
a) Colors
b) Levels
c) Node size
d) Heaps
AA Tree :- AA trees are the variation of the red-black trees, a form of binary search tree.
AA trees use the concept of levels to aid in balancing binary trees. The level of node (instead of colour) is used for
balancing information. A link where child and parent’s levels are same, is called a horizontal link, and is analogous to a
red link in the red-black tree.
Red-Black Tree:- A red-black tree is a kind of self-balancing binary search tree where each node has an extra
bit, and that bit is often interpreted as the color (red or black). These colors are used to ensure that the tree
remains balanced during insertions and deletions.
Rules That Every Red-Black Tree Follows:
1. Every node has a color either red or black.
2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4. Every path from a node (including root) to any of its descendants NULL nodes has the same number of black
nodes.
5. All leaf nodes are black nodes.
Explanation: A skew removes a left horizontal link by right rotation and a split removes a right horizontal link by left
rotation
What is the prime condition of AA-tree which makes it simpler than a red-black tree?
Comparing the speed of execution of Red-Black trees and AA-trees, which one has the
faster search time?
a) AA-tree
b) Red-Black tree
c) Both have an equal search time
d) It depends
AVL tree :- AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left
and right subtrees cannot be more than one for all nodes.
A Cartesian tree is a tree data structure created from a set of data that obeys the following structural
invariants:
1. The tree obeys in the min (or max) heap property – each node is less (or greater) than its children.
2. An inorder traversal of the nodes yields the values in the same order in which they appear in the
initial sequence.
Which data structure is used to maintain a dynamic forest using a link or cut operation?
a) Top Tree
b) Array
c) Linked List
d) Stack
Top tree:-Top tree is a type of data structure which is based on unrooted dynamic binary tree and is used to solve path
related problems. It allows an algorithm called divide and conquer.
Like AVL and Red-Black Trees, Splay tree is also self-balancing BST. The main idea of splay tree is to bring the
recently accessed item to root of the tree, this makes the recently searched item to be accessible in O(1) time if
accessed again. Splay trees have excellent locality properties. Frequently accessed items are easy to find.
Infrequent items are out of way. Unlike AVL tree, a splay tree can change even with read-only operations like
search.
B-tree of order n is a order-n multiway tree in which each non-root node contains
__________
a) at most (n – 1)/2 keys
b) exact (n – 1)/2 keys
c) at least 2n keys
d) at least (n – 1)/2 keys
Explanation: A B+ -tree has larger fanout and therefore have a depth smaller than that of corresponding B-tree
Which one of the following data structures are preferred in database-system implementation?
a) AVL tree
b) B-tree
c) B+ -tree
d) Splay tree
In binary search trees we have seen the average-case time for operations like search/insert/delete is O(log N) and the
worst-case time is O(N) where N is the number of nodes in the tree.
Like other Trees include AVL trees, Red Black Tree, B tree, 2-3 Tree is also a height balanced tree.
The time complexity of search/insert/delete is O(log N) .
A 2-3 tree is a B-tree of order 3.
Nodes with two children are called 2-nodes. The 2-nodes have one data value and two children
Nodes with three children are called 3-nodes. The 3-nodes have two data values and three children.
Data is stored in sorted order.
It is a balanced tree.
All the leaf nodes are at same level.
Each node can either be leaf, 2 node, or 3 node.
Always insertion is done at leaf.
Which of the following the BST is isometric with the 2-3 tree?
a) Splay tree
b) AA tree
c) Heap
d) Red – Black tree
Explanation: AA tree is isometric of the 2-3 trees. In an AA tree, we store each node a level, which is the height of
the corresponding 2-3 tree node. So, we can convert a 2-3 tree to an AA tree.
Suffix tree is a compressed trie of all the suffixes of a given string. Suffix trees help in solving a lot of string related
problems like pattern matching, finding distinct substrings in a given string, finding longest palindrome etc. suffix tree is
also known as PAT tree or position tree. It is a compressed search tree or prefix tree in which keys contain the suffix of
text values as the text position In this tutorial following points will be covered:
Compressed Trie
Suffix Tree Construction (Brute Force)
Brief description of Ukkonen's Algorithm
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as
vertices and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph can be defined
as,
A Graph consists of a finite set of vertices(or nodes) and a set of Edges that connect a pair of nodes. Graphs are
used to solve many real-life problems. Graphs are used to represent networks. The networks may include
paths in a city or telephone network or circuit network.
Explanation: By euler’s formula the relation between vertices(n), edges(q) and regions(r) is given by n-q+r=2.
A graph G = (V, E) consists of a set of vertices V = { V1, V2, . . . } and set of edges E = { E1, E2, . . . }. The set of
unordered pairs of distinct vertices whose elements are called edges of graph G such that each edge is identified with
an unordered pair (Vi, Vj) of vertices.
Finite Graphs: A graph is said to be finite if it has finite number of vertices and finite number of edges.
Infinite Graph: A graph is said to be infinite if it has infinite number of vertices as well as infinite number of edges.
Trivial Graph: A graph is said to be trivial if a finite graph contains only one vertex and no edges.
Simple Graph: A simple graph is a graph which does not contains more than one edge between the pair of vertices. A
simple railway tracks connecting different cities is an example of simple graph.
Regular Graph: A simple graph is said to be regular if all vertices of a graph G are of equal degree. All complete
graphs are regular but vice versa is not possible.
Bipartite Graph: A graph G = (V, E) is said to be bipartite graph if its vertex set V(G) can be partitioned into two
non-empty disjoint subsets.
Connected or Disconnected Graph: A graph G is said to be connected if for any pair of vertices (Vi, Vj) of a graph G
are reachable from one another. Or a graph is said to be connected if there exists at least one path between each and
every pair of vertices in graph G, otherwise it is disconnected. A null graph with n vertices is disconnected graph
consisting of n components. Each component consists of one vertex and no edge.
Advantage:
They are useful for implementing algorithms like DFS and BFS.
Graph provides data accuracy.
Graphs are useful for finding the shortest path.
To find the minimum spanning tree.
It is used in complex problems.
It is useful for organizing data.
Disadvantages:
Graphs make data structures complex as they use pointers.
A graph can have a large memory complexity.
The graph is represented by an adjacency matrix that does not allow parallel edges.
Multiplication in a graph is complex.
For a given graph G having v vertices and e edges which is connected and has no cycles, which of the following
statements is true?
a) v=e
b) v = e+1
c) v + 1 = e
d) v = e-1
For the given conditions, which of the following is in the correct order of increasing space requirement?
i) Undirected, no weight
ii) Directed, no weight
iii) Directed, weighted
iv) Undirected, weighted
a) ii iii i iv
b) i iii ii iv
c) iv iii i ii
d) i ii iii iv
a dense graph is a graph in which the number of edges is close to the maximal number of edges (where every pair of
vertices is connected by one edge). The opposite, a graph with only a few edges, is a sparse graph.
Space complexity for an adjacency list of an undirected graph having large values of V
(vertices) and E (edges) is ___________
a) O(E)
b) O(V*V)
c) O(E+V)
d) O(V)
Sorting algorithm:-
A Sorting Algorithm is used to rearrange a given array or list elements according to acomparison operator on the
elements. The comparison operator is used to decide the new order of element in the respective data structure.
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The
array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the
correct position in the sorted part.
This algorithm is one of the simplest algorithm with simple implementation
Basically, Insertion sort is efficient for small data values
Insertion sort is adaptive in nature, i.e. it is appropriate for data sets which are already partially sorted.
Insertion Sort algorithm follows incremental approach. insertion sort is an in-place sorting algorithm. insertion sort is a
stable sorting algorithm. Insertion sort is used when number of elements is small. It can also be useful when input array
is almost sorted, only few elements are misplaced in complete big array.
An insertion algorithm consists of N-1 passes when an array of N elements is given
● Insertion sort is similar to that of a binary heap algorithm because of the use of
temporary variable to swap.
● The total number of pairs in a list L is N(N-1)/2. Thus, an average list has half this
amount, or N(N-1)/4 inversions.
● To perform an insertion sort, we use two basic loops- an outer for loop and an
inner while loop
● Binary search can be used in an insertion sort algorithm to reduce the number of
comparisons. This is called a Binary insertion sort.
● An insertion sort is stable, adaptive, in-place and incremental in nature.
● Insertion sort is an example of an incremental algorithm.
● The insertion sort is good for sorting small arrays. It sorts smaller arrays faster than any other sorting algorithm.
● Insertion sort is not an exchange sort
What will be the number of passes to sort the elements using insertion sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1
Explanation: The number of passes is given by N-1. Here, N=6. Therefore,
6-1=5 passes.
Which of the following sorting algorithms is the fastest for sorting small arrays?
a) Quick sort
b) Insertion sort
c) Shell sort
d) Heap sort
Which of the following sorting algorithm is best suited if the elements are already sorted?
a) Heap Sort
b) Quick Sort
c) Insertion Sort
d) Merge Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending
order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
Time Complexity: The time complexity of Selection Sort is O(N2). Auxiliary Space: O(1) as the only extra memory
used is for temporary variable while swapping two values in Array. Stability : The default implementation is not stable.
However it can be made stable.
Selection is based on keys, hence a file with large values and small keys can be efficiently sorted with selection sort.
selection sort is an in-place sorting algorithm, it does not require additional storage. Selection sort is not scalable.
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in
the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite
high.
Worst and Average Case Time Complexity: O(N2). The worst case occurs when an array is reverse sorted.
Best Case Time Complexity: O(N). The best case occurs when an array is already sorted.
Auxiliary Space: O(1)
Merge sort is preferred for linked list over arrays. It is because in a linked list the insert operation takes only O(1)
time and space which implies that we can implement merge operation in constant time.
● Tim sort is a hybrid sorting algorithm as it uses more than one sorting algorithm internally. It makes use of merge
sort and insertion sort.
● Bottom up merge sort uses the iterative method in order to implement sorting. Bottom up merge sort does not use
recursion
● Merge Sort is useful for sorting linked lists in O(nLogn) time.
Choose the incorrect statement about merge sort from the following?
a) it is a comparison based sort
b) it is an adaptive algorithm
c) it is not an in place algorithm
d) it is stable algorithm
Explanation: Merge sort is not an adaptive sorting algorithm. This is because it takes O(nlog n) time complexity
irrespective of any case.
Which of the following stable sorting algorithm takes the least time when applied to an almost sorted array?
a) Quick sort
b) Insertion sort
c) Selection sort
d) Merge sort
QuickSort
QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array
around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
The default implementation of quick sort is not stable. However any sorting algorithm can be made stable by
considering indexes as comparison parameter. As per the broad definition of in-place algorithm it qualifies as
an in-place sorting algorithm as it uses extra space only for storing recursive function calls but not for
manipulating the input.
Quick sort is the fastest known sorting algorithm because of its highly optimized inner loop.
● In quick sort, the array is divided into sub-arrays and then it is sorted (divide-and-conquer strategy).
● Median-of-three partitioning is the best method for choosing an appropriate pivot element. Picking a first,
last or random element as a pivot is not much effective.
● Insertion sort is used along with quick sort to sort the sub arrays. It is used only at the end.
● Quick sort uses join operation since join is a faster operation than merge.
● Is QuickSort stable? :- The default implementation is not stable. However any sorting algorithm can be made stable
by considering indexes as comparison parameter.
● In stable sorting algorithm the records with equal keys appear in the same order in the sorted sequence as they
appear in the input unsorted sequence.
Quick sort does not preserve the relative order of equal sort items. Therefore, Quick sort is not
a stable sort.
● Quick sort is a space-optimised version of the binary tree sort
Which one of the following sorting algorithm is best suited to sort an array of 1 million
elements?
a) Bubble sort
b) Insertion sort
c) Merge sort
d) Quick sort
Explanation: The Quick sort is best suited to sort the array of 1 million elements. The practical
implementations of Quick sort use randomised version. In practice randomised Quick sort algorithms rarely
shows worst case behaviour and is almost always O(nlogn). And Quick sort requires little additional space and
exhibits good cache locality..
Explanation: Once a pivot is chosen, its position is finalized in the sorted array, it cannot be modified.
ShellSort :- Shell sort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position
ahead. When an element has to be moved far ahead, many movements are involved. The idea of ShellSort is to allow
the exchange of far items.
Time Complexity: Time complexity of the above implementation of Shell sort is O(n2). In the above implementation, the
gap is reduced by half in every iteration. There are many other ways to reduce gaps which leads to better time
complexity. See this for more details.
● The other name for a shell sort algorithm is diminishing decrement sort as the distance between comparisons
decreases as the algorithm runs until the last phase.
● Shell sort broke the quadratic time barrier as it works by comparing elements that are distant
● Shell sort is an example of internal sorting because sorting of elements is done internally using an array.
● Shell sort uses an increment sequence h1, h2, h3… and this sequence will work as long as h1=1.
● Shell sort is an in-place sorting algorithm. ● Shell sort is not an stable sorting algorithm.
● Shell sort is an improvement on insertion sort that allows the exchange of elements that are far apart.
● Insertion sort is more efficient than Shell sort if the length of array is small because insertion sort is quite simple to
program and involve very few actions other than comparisons and replacements on each pass.
● Shell sort’s passes completely sort the elements before going on to the next-smallest gap while Comb sort’s passes
do not completely sort the elements.
Which of the following statements is the basic for loop for a shell sort algorithm?
a) for(increment=N/2;increment>0;increment/=2)
b) for(i=1;i<n;i++)
c) for(i=n/2;i>=0;i- -)
d) for(i=0;i< n;i++;numelements- -)
Explanation: In Shell sort, the relative order of elements with equal values may change. Therefore, it is not a stable
sorting algorithm. Shell sort is an in-place sorting algorithm as it requires O(1) auxiliary space.
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection
sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same
process for the remaining elements.
● Heap sort is based on the algorithm of priority queue and it gives the best sorting time.
● Heap sort is slower than Shell sort because Shell sort uses Sedgewick’s increment sequence.
● Heap sort is a comparison based sorting algorithm and has time complexity O(nlogn) in the average case.
Heap sort is an in-place algorithm as it needs O(1) of auxiliary space. Heap sort uses heap and operations on heap can
change the relative order of items with the same key values. Therefore, Heap sort is not a stable sort.
● Heap sort is an implementation of selection sort using the input array as a heap representing a descending
priority queue.
● Heap sort is an in-place algorithm.
● Smooth sort is a variation of Heap sort.
● Introsort is a hybrid sorting algorithm that combines Quick sort and Heap sort to retain advantages of both.
Which of the following sorting algorithms can be considered as improvement to the binary tree sort?
a) Heap sort
b) Quick sort
c) Selection sort
d) Insertion sort
Explanation: Heap sort is basically improvement to the binary tree sort. Heap sort builds a heap on the input
element by adjusting the position of the elements within the original array, rather than creating nodes as in
binary tree sort.
Cycle sort :- Cycle sort is an in-place sorting Algorithm, unstable sorting algorithm, a comparison sort that is
theoretically optimal in terms of the total number of writes to the original array.
Explanation: Cycle sort is a slow sorting algorithm as compared to quick sort, merge sort etc. and is also not
adaptive. But it offers an advantage that it performs minimal write operations which can be very useful in a
situation where the write operation is expensive.
Which of the following algorithm is best suited for the case where swap operation is expensive?
a) bubble sort
b) cycle sort
c) cocktail sort
d) merge sort
Explanation: Cycle sort is a slow sorting algorithm but it requires a minimum number of write operations in order to sort
a given array. So it is useful when the write/swap operation is expensive.
Bogosort is also known by names like stupid sort, monkey sort, permutation sort, slow sort and shotgun sort.These
names are particularly chosen due to its inefficient algorithm. Bogosort algorithm successively generates permutations
of its input. This process is repeated until the sorted version of the array is found. bogosort is not stable. bogosort is an
in-place sorting algorithm
Which of the following sorting techniques is most efficient if the range of input data is not significantly greater than a
number of elements to be sorted?
a) selection sort
b) bubble sort
c) counting sort
d) insertion sort
bucket sort:- bucket sort is an example of a non-comparison sort so it is able to sort an array without making any
comparison.
Which of the following is not true about bucket sort?
a) It is a non comparison based integer sort
b) It is a distribution sort
c) It can also be considered as comparison based sort
d) It is in place sorting algorithm
Explanation: Bucket sort is a non comparison based integer sort. It sorts the given data by distributing the array
elements into a number of buckets. It is not an in place sorting technique.
Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is,
unlike trees, graphs may contain cycles (a node may be visited twice). To avoid processing a node more than once, use
a boolean visited array. it is equivalent to the pre-order traversal of a Binary Tree. The Depth First Search explores
every node once and every edge once (in worst case), so it’s time complexity is O(V + E). The Depth First Search is
implemented using recursion. So, stack can be used as data structure to implement depth first search.
A person wants to visit some places. He starts from a vertex and then wants to visit every vertex till it finishes from one
vertex, backtracks and then explore other vertex from same vertex. What algorithm he should use?
a) Depth First Search
b) Breadth First Search
c) Trim’s algorithm
d) Kruskal’s Algorithm
Which of the following data structure is used to implement DFS?
a) linked list
b) tree
c) stack
d) queue
Explanation: Stack is used in the standard implementation of depth first search. It is used to store the elements which
are to be explored
Breadth-First Traversal (or Search) for a graph is similar to Breadth-First Traversal of a tree . The only catch here
is, that, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node
more than once, we use a boolean visited array. For simplicity, it is assumed that all vertices are reachable from the
starting vertex. BFS uses a queue data structure for traversal.
Choose the incorrect statement about DFS and BFS from the following?
a) BFS is equivalent to level order traversal in trees
b) DFS is equivalent to post order traversal in trees
c) DFS and BFS code has the same time complexity
d) BFS is implemented using queue
Explanation: DFS is equivalent to pre order traversal in trees, not post order traversal. It is so because in DFS we
keep on exploring as far as possible along each branch before backtracking. So it should be equivalent to pre order
traversal.
Explanation: Breadth First Search can be applied to Bipartite a graph, to find the shortest path between two nodes,
in GPS Navigation. In Path finding, Depth First Search is used.
● Best First Search is a searching algorithm used in graphs. It explores it by choosing a node by heuristic
evaluation rule. It is used in solving searching for related problems.
● The greedy best first search algorithm was used to predict the closeness of the end of the path and its solution by
some of the computer scientists.
● Stack is the data structure is used for implementing LIFO branch and bound strategy.
● Queue is the data structure is used for implementing FIFO branch and bound strategy.
● Priority Queue is the data structure is used for implementing best first branch and bound strategy. Dijkstra’s
algorithm is an example of best first search algorithm.
● FIFO branch and bound leads to breadth first search. Whereas backtracking leads to depth first search.
● Both backtrackings as well as branch and bound are problem solving algorithms. Both LIFO branch and
bound strategy and backtracking leads to depth first search.
Which of the following can traverse the state space tree only in DFS manner?
a) branch and bound
b) dynamic programming
c) greedy algorithm
d) backtracking
A Spanning tree is a subset to a connected graph G, where all the edges are connected, i.e, we can traverse to any
edge from a particular edge with or without intermediates. Also, a spanning tree must not have any cycle in it. Thus we
can say that if there are n vertices in a connected graph then the no. of edges that a spanning tree may have is n-1.
A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, undirected graph is a
spanning tree with a weight less than or equal to the weight of every other spanning tree.
A minimum spanning tree has (V – 1) edges where V is the number of vertices in the given graph. Time Complexity:
O(ElogE) or O(ElogV).
Explanation: A graph can have many spanning trees. Each spanning tree of a graph G is a subgraph of the
graph G, and spanning trees include every vertex of the gram. Spanning trees are always acyclic.
Explanation: In the travelling salesman problem we have to find the shortest possible route that visits every city
exactly once and returns to the starting point for the given a set of cities. So, travelling salesman problem can be
solved by contracting the minimum spanning tree.
Which of the following is not the algorithm to find the minimum spanning tree of the given
graph?
a) Boruvka’s algorithm
b) Prim’s algorithm
c) Kruskal’s algorithm
d) Bellman–Ford algorithm
The Boruvka’s algorithm, Prim’s algorithm and Kruskal’s algorithm are the algorithms that can be used to find the
minimum spanning tree of the given graph. The Bellman-Ford algorithm is used to find the shortest path from
the single source to all other vertices.
Explanation: The Kruskal’s algorithm is used to find the minimum spanning tree of the connected graph. It
construct the MST by finding the edge having the least possible weight that connects two trees in the forest.
Kruskal’s algorithm uses a greedy algorithm approach to find the MST of the connected weighted graph
● the time complexity of Kruskal’s algorithm is O(E log V)
● Kruskal’s algorithm can also run on the disconnected graphs.
● Prim’s algorithm iterates from one node to another, so it can not be applied for disconnected graph.
● Kruskal’s algorithm is comparatively easier and simpler than prim’s algorithm.
● Kruskal algorithm does not contain any cycle.
● Kruskal’s algorithm can efficiently implemented using the disjoint-set data structure.
Prim’s algorithm uses a greedy algorithm approach to find the MST of the connected weighted graph. In greedy
method, we attempt to find an optimal solution in stages.
Explanation: Steps in Prim’s algorithm: (I) Select any vertex of given graph and add it to
MST (II) Add the edge of minimum weight from a vertex not in MST to the vertex in MST;
(III) It MST is complete the stop, otherwise go to step (II).
● Prim’s algorithm resembles Dijkstra’s algorithm.
● Prim’s algorithm and Kruskal’s algorithm perform equally in case of the sparse
graphs
● Prim’s algorithm can be implemented using Fibonacci heap and it never accepts
Cycles
Explanation: The Prim’s algorithm was developed by Vojtěch Jarník and it was latter discovered by the duo
Prim and Dijkstra. Therefore, Prim’s algorithm is also known as DJP Algorithm.
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like Prim’s MST, we
generate a SPT (shortest path tree) with a given source as a root. We maintain two sets, one set contains
vertices included in the shortest-path tree, other set includes vertices not yet included in the shortest-path
tree. At every step of the algorithm, we find a vertex that is in the other set (set of not yet included) and has a
minimum distance from the source.
Dijkstra’s Algorithm is used for solving single source shortest path problems. In this algorithm,
a single node is fixed as a source node and shortest paths from this node to all other nodes in graph is found.
Which of the following is the most commonly used data structure for implementing Dijkstra’s
Algorithm?
a) Max priority queue
b) Stack
c) Circular queue
d) Min priority queue
Dijkstra’s algorithm is a Greedy algorithm and time complexity is O((V+E)LogV) (with the use of
Fibonacci heap). Dijkstra doesn’t work for Graphs with negative weights, Bellman-Ford works
for such graphs. Bellman-Ford is also simpler than Dijkstra and suites well for distributed systems. But
time complexity of Bellman-Ford is O(VE), which is more than Dijkstra.
Negative weights are found in various applications of graphs. For example, instead of paying cost for a path,
we may get some advantage if we follow the path.
Bellman-Ford works better (better than Dijkstra’s) for distributed systems. Unlike Dijkstra’s where we need
to find the minimum value of all vertices, in Bellman-Ford, edges are considered one by one.
Bellman-Ford does not work with undirected graph with negative edges as it will declared as
negative cycle.
Bellmann Ford algorithm is used to indicate whether the graph has negative weight cycles or not.
Dijikstra’s Algorithm is more efficient than Bellmann Ford Algorithm.
Relaxation is the basic principle behind Bellmann Ford Algorithm.
Bellmann Ford Algorithm can be applied for all directed and weighted graphs.
Bellmann Ford Algorithm is an example for Dynamic programming.
The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to
find shortest distances between every pair of vertices in a given edge weighted directed Graph.
Floyd Warshall Algorithm follows dynamic programming approach. ● Floyd Warshall Algorithm
can be used for finding Transitive closure.
The Stable Marriage Problem states that given N men and N women, where each person has ranked all
members of the opposite sex in order of preference, marry the men and women together such that there are
no two people of opposite sex who would both rather have each other than their current partners. If there are
no such people, all the marriages are “stable”. Stable marriage problem is an example for recursive algorithm
because it recursively uses backtracking algorithm to find an optimal solution.
Explanation: The correct technique for finding a maximum matching in a bipartite graph is by using a
Breadth First Search(BFS).
In Fractional Knapsack, we can break items for maximizing the total value of knapsack. This
problem in which we can break an item is also called the fractional knapsack problem. Fractional
knapsack problem is also called continuous knapsack problem. Fractional knapsack is solved
using dynamic programming. Greedy algorithm is used to solve this problem. The objective is to fill the
knapsack of some given volume with different materials such that the value of selected items is maximized.
In 0/1 knapsack problem items are indivisible and in fractional knapsack items are divisible.
Which of the following algorithms is the best approach for solving Huffman codes?
a) exhaustive search
b) greedy algorithm
c) brute force algorithm
d) divide and conquer algorithm
Explanation: Greedy algorithm is the best approach for solving the Huffman codes problem since it greedily
searches for an optimal solution. In Huffman encoding, data is always stored at the leaves of a tree inorder
to compute the codeword effectively. Backtracking approach is used to solve complex combinatorial
problems which cannot be solved by exhaustive search algorithms.
Explanation: N-queen problem, subset sum problem, Hamiltonian circuit problems can be solved
by backtracking method whereas travelling salesman problem is solved by Branch and bound
method.
The problem of finding a list of integers in a given specific range that meets certain conditions is called?
a) Subset sum problem
b) Constraint satisfaction problem
c) Hamiltonian circuit problem
d) Travelling salesman problem
Constraint satisfaction problem is the problem of finding a list of integers under given constraints.
Constraint satisfaction problem is solved using a backtracking approach.
Explanation: The fractional knapsack problem is solved using a greedy algorithm. The 0-1
Knapsack problem can not be solved using Greedy algorithm.
Explanation: The most common hamming codes generalize to form hamming(7, 4) code. It encodes four bits
of data into seven bits by adding three parity bits.
NP Problem:
The NP problems set of problems whose solutions are hard to find but easy to verify and are solved by
Non-Deterministic Machine in polynomial time.
NP-Hard Problem:
A Problem X is NP-Hard if there is an NP-Complete problem Y, such that Y is reducible to X in polynomial
time. NP-Hard problems are as hard as NP-Complete problems. NP-Hard Problem need not be in NP class.
NP-Complete Problem:
A problem X is NP-Complete if there is an NP problem Y, such that Y is reducible to X in polynomial time.
NP-Complete problems are as hard as NP problems. A problem is NP-Complete if it is a part of both NP and
NP-Hard Problem. A non-deterministic Turing machine can solve NP-Complete problem in polynomial
time.
Hamiltonian Path :-- A Hamiltonian path is defined as the path in a directed or undirected graph which
visits each and every vertex of the graph exactly once.
Which of the following algorithm can be used to solve the Hamiltonian path problem efficiently?
a) branch and bound
b) iterative improvement
c) divide and conquer
d) greedy algorithm
Explanation: The Hamiltonian path problem can be solved efficiently using branch and bound
approach. It can also be solved using a backtracking approach.