Data Structure & Algorithem Interview
Data Structure & Algorithem Interview
Answer:
Data structure is a way of defining, storing & retrieving of data in a structural & systemetic way.
A data structure may contain different type of data items.
Answer:
The key difference between both the data structure is the memory area that is being accessed.
When dealing with the structure that resides the main memory of the computer system, this is
referred to as storage structure. When dealing with an auxiliary structure, we refer to it as file
structures.
Answer:
Data Structure availability may vary by Programming Languages. Commonly available data
structures are list, arrays, stake, queues, graph, tree etc.
Answer:
Answer:
Hashing is a technique to convert a range of key values into a range of indexes of an array. By
using hash tables, we can create associative data storage where data index can be find by
providing its key values.
Answer:
To reference all the elements in a one -dimension array, you need to use an indexed loop, So
that, the counter runs from 0 to the array size minus one. In this manner, you can reference all the
elements in sequence by using the loop counter as the array subscript.
Answer:
Data structures are essential in almost every aspect where data is involved. In general, algorithms
that involve efficient data structure is applied in the following areas: numerical
analysis, operating system, A.I., compiler design, database management, graphics, and statistical
analysis, to name a few.
Answer:
LIFO is a short form of Last In First Out. It refers how data is accessed, stored and retrieved.
Using this scheme, data that was stored last should be the one to be extracted first. This also
means that in order to gain access to the first data, all the other data that was stored before this
first data must first be retrieved and extracted.
Answer:
A queue is a data structure that can simulate a list or stream of data. In this structure, new
elements are inserted at one end, and existing elements are removed from the other end.
Answer:
A binary tree is one type of data structure that has two nodes, a left node, and a right node. In
programming, binary trees are an extension of the linked list structures.
Q11. Which data structures are applied when dealing with a recursive function?
Answer:
Recursion is a function that calls itself based on a terminating condition, makes use of the stack.
Using LIFO, a call to a recursive function saves the return address so that it knows how to return
to the calling function after the call terminates.
Answer:
A stack is a data structure in which only the top element can be accessed. As data is stored in the
stack, each data is pushed downward, leaving the most recently added data on top.
Answer:
A binary search tree stores data in such a way that they can be retrieved very efficiently. The left
subtree contains nodes whose keys are less than the node’s key value, while the right subtree
contains nodes whose keys are greater than or equal to the node’s key value. Moreover, both
subtrees are also binary search trees.
Answer:
Multidimensional arrays make use of multiple indexes to store data. It is useful when storing data
that cannot be represented using single dimensional indexing, such as data representation in a
board game, tables with data stored in more than one column.
Answer:
It depends on where you intend to apply linked lists. If you based it on storage, a linked list is
considered non-linear. On the other hand, if you based it on access strategies, then a linked list is
considered linear.
Answer:
Apart from being able to store simple structured data types, dynamic memory allocation can
combine separately allocated structured blocks to form composite structures that expand and
contract as needed.
Answer:
FIFO stands for First-in, First-out, and is used to represent how data is accessed in a queue. Data
has been inserted into the queue list the longest is the one that is removed first.
Answer:
An ordered list is a list in which each node’s position in the list is determined by the value of its
key component, so that the key values form an increasing sequence, as the list is traversed.
Q19. What is merge sort?
Answer:
Merge sort, is a divide-and-conquer approach for sorting the data. In a sequence of data, adjacent
ones are merged and sorted to create bigger sorted lists. These sorted lists are then merged again
to form an even bigger sorted list, which continues until you have one single sorted list.
Answer:
Null is a value, whereas Void is a data type identifier. A variable that is given a Null value
indicates an empty value. The void is used to identify pointers as having no initial size.
Answer:
A linked list is an ideal data structure because it can be modified easily. This means that editing a
linked list works regardless of how many elements are in the list.
Answer:
Pushing and popping applies to the way data is stored and retrieved in a stack. A push denotes
data being added to it, meaning data is being “pushed” into the stack. On the other hand, a pop
denotes data retrieval, and in particular, refers to the topmost data being accessed.
Answer:
A linear search refers to the way a target key is being searched in a sequential data structure. In
this method, each element in the list is checked and compared against the target key. The process
is repeated until found or if the end of the file has been reached.
Answer:
The amount of memory to be allocated or reserved would depend on the data type of the variable
being declared. For example, if a variable is declared to be of integer type, then 32 bits of
memory storage will be reserved for that variable.
Answer:
The heap is more flexible than the stack. That’s because memory space for the heap can be
dynamically allocated and de-allocated as needed. However, the memory of the heap can at times
be slower when compared to that stack.
Answer:
A postfix expression is an expression in which each operator follows its operands. The advantage
of this form is that there is no need to group sub-expressions in parentheses or to consider
operator precedence.
Answer:
Data abstraction is a powerful tool for breaking down complex data problems into manageable
chunks. This is applied by initially specifying the data objects involved and the operations to be
performed on these data objects without being overly concerned with how the data objects will
be represented and stored in memory.
Answer:
Assuming that the data to be inserted is a unique value (that is, not an existing entry in the tree),
check first if the tree is empty. If it’s empty, just insert the new item in the root node. If it’s not
empty, refer to the new item’s key. If it’s smaller than the root’s key, insert it into the root’s left
sub tree, otherwise, insert it into the root’s right sub tree.
Answer:
The selection sort is a fairly intuitive sorting algorithm, though not necessarily efficient. In this
process, the smallest element is first located and switched with the element at subscript zero,
thereby placing the smallest element in the first position.
The smallest element remaining in the sub array is then located next to subscripts 1 through n-1
and switched with the element at subscript 1, thereby placing the second smallest element in the
second position. The steps are repeated in the same manner till the last element.
Answer:
In the case of signed numbers, the first bit is used to indicate whether positive or negative, which
leaves you with one bit short. With unsigned numbers, you have all bits available for that
number. The effect is best seen in the number range (an unsigned 8-bit number has a range 0-
255, while the 8-bit signed number has a range -128 to +127.
Q31. What is the minimum number of nodes that a binary tree can have?
Answer:
A binary tree can have a minimum of zero nodes, which occurs when the nodes have NULL
values. Furthermore, a binary tree can also have 1 or 2 nodes.
Answer:
Dynamic data structures are structures that expand and contract as a program runs. It provides a
flexible means of manipulating data because it can adjust according to the size of the data.
Answer:
Pointers that are used in linked list have various applications in the data structure. Data structures
that make use of this concept include the Stack, Queue, Linked List and Binary Tree.
Answer:
Most declarations do, with the exemption of pointers. Pointer declaration does not allocate
memory for data, but for the address of the pointer variable. Actual memory allocation for the
data comes during run-time.
Answer:
When dealing with arrays, data is stored and retrieved using an index that refers to the element
number in the data sequence. This means that data can be accessed in any order. In
programming, an array is declared as a variable having a number of indexed elements.
Q36. What is the minimum number of queues needed when implementing a priority
queue?
Answer:
The minimum number of queues needed in this case is two. One queue is intended for sorting
priorities while the other queue is used for actual storage of data.
There are many types of sorting algorithms: quick sort, bubble sort, balloon sort, radix sort,
merges sort, etc. Not one can be considered the fastest because each algorithm is designed for a
particular data structure and data set. It would depend on the data set that you would want to sort.
Answer:
Stack follows a LIFO pattern. It means that data access follows a sequence wherein the last data
to be stored when the first one to be extracted. Arrays, on the other hand, does not follow a
particular order and instead can be accessed by referring to the indexed element within the array.
Answer:
1.if the tree is empty, then the target is not in the tree, end search
2. If the tree is not empty, the target is in the tree
3. Check if the target is in the root item
4. If a target is not in the root item, check if a target is smaller than the root’s value
5. If a target is smaller than the root’s value, search the left sub tree
6. Else, search the right sub tree
Answer:
Answer:
A bubble sort is one sorting technique that can be applied to data structures such as an array. It
works by comparing adjacent elements and exchanges their values if they are out of order. This
method lets the smaller values “bubble” to the top of the list, while the larger value sinks to the
bottom.
Answer:
A linked list typically has two parts: the head and the tail. Between the head and tail lie the actual
nodes. All these nodes are linked sequentially.
Selection sort works by picking the smallest number from the list and placing it at the front. This
process is repeated for the second position towards the end of the list. It is the simplest sort
algorithm.
Answer:
A graph is one type of data structure that contains a set of ordered pairs. These ordered pairs are
also referred to as edges or arcs and are used to connect nodes where data can be stored and
retrieved.
Answer:
The linear data structure is a structure wherein data elements are adjacent to each other.
Examples of linear data structure include arrays, linked lists, stacks, and queues. On the other
hand, a non-linear data structure is a structure wherein each data element can connect to more
than two adjacent data elements. Examples of nonlinear data structure include trees and graphs.
Answer:
An AVL tree is a type of binary search tree that is always in a state of partially balanced. The
balance is measured as a difference between the heights of the sub trees from the root. This self-
balancing tree was known to be the first data structure to be designed as such.
Answer:
Doubly linked lists are a special type of linked list wherein traversal across the data elements can
be done in both directions. This is made possible by having two links in every node, one that
links to the next node and another one that connects to the previous node.
Answer:
Huffman’s algorithm is used for creating extended binary trees that have minimum weighted
path lengths from the given weights. It makes use of a table that contains the frequency of
occurrence for each data element.
Fibonacci search is a search algorithm that applies to a sorted array. It makes use of a divide-and-
conquer approach that can significantly reduce the time needed in order to reach the target
element.
Answer:
Recursive algorithm targets a problem by dividing it into smaller, manageable sub-problems. The
output of one recursion after processing one sub-problem becomes the input to the next recursive
process.
Answer:
To find the target key in a linked list, you have to apply sequential search. Each node is traversed
and compared with the target key, and if it is different, then it follows the link to the next node.
This traversal continues until either the target key is found or if the last node is reached.
Q52. What are the basic operations performed on various data structures?
Answer:
Answer:
A linked list is nothing but a sequence of nodes. With this sequence, each node is connected to
the following node. It forms a chain of data storage.
Q54. Explain the process of how do you reference all the elements in the one-dimension
array in detail?
Answer:
To reference all the elements in the one-dimension array, we have to use an indexed loop. With
the help of this, it executes from “0” to array size minus one. By following this process the loop
counter will be able to refer to all the elements.
Q55. List out the areas where the data structure is applied?
Answer:
The data structure is a vital aspect while handling data. The following are specific areas where
the data structure is applied:
Numerical analysis
Operating systems
A.I.
Database management
Statistical analysis
The above are few areas where the data structure is applied and not limited to.
Answer:
The way to write arithmetic expressions is known as notation. There are three types of notations
used in an arithmetic expression, i.e., without changing the essence or output of expression.
These notations are:
Prefix (Polish) Notation - In this, the operator is prefixed to operands, i.e. ahead of
operands.
Infix Notation - In this, operators are used in between operands.
Postfix (Reverse-Polish) Notation - In this, the operator is postfixes to the operands, i.e.,
after the operands.
The following table briefly tries to show the difference in all three notations −
Answer:
LIFO stands for Last In First Out.
This process describes how the data is accessed, stored, and then retrieved. So the latest data that
is stored in the database can be extracted first.
Answer:
It is one type of data structure that has two nodes, has left node and a right node. In a
programming language, binary trees are considered to be an extension to the linked list.
Answer:
The stack is considered as a data structure where the top layer element can be accessed. The data
is stored in the stack and every time when data is stored, it pushes the data downwards which
enables the users to access the latest data from the top layers.
Answer:
Multidimensional arrays use multiple indexes in order to store data in the database. In a few
scenarios, data cannot be stored using a single dimension index; in these scenarios
multidimensional arrays are useful.
Q61. Explain whether a linked list is considered as a linear or non-linear data structure?
Answer:
This is purely determined on the requirement basis, a linked list can be considered as a linear
data structure or a non-linear data structure. For example: If the linked list is used on storage,
then the linked list is considered as a nonlinear data structure.
If linked lists are used against access strategies then they are considered as a linear data structure.
Q62. Explain how does dynamic memory allocation will help you in managing data?
Answer:
A dynamic memory allocation will help you effectively manage your data by allocating
structured blocks to have composite structures that can be flexible, i.e. it can expand and can
contract based on the need.
Answer:
This process defines or depicts how the data is stored inserted and accessed in a queue. Within
this process, the data that is inserted at the beginning of the queue will only be extracted or
accessed first.
Answer:
A merge sort is nothing but a process where the data is divided and sorted to reach the end goal.
Within this process, the adjacent elements are merged and sorted to create bigger elements.
These sorted elements are gathered again and made the even bigger list. This process is
continuous and repetitive until and unless they have nailed it down to a single sorted list.
Answer:
The important aspect or advantage of a linked list is that it is the perfect data structure where the
data can be modified very easily. Also, it doesn’t matter how many elements are available on the
linked list.
Answer:
The two main activities, i.e. Pushing and Popping apply the way how data is stored and retrieved
in an entity. So if you check in detail, a Push is nothing but a process where data is added to the
stack. On the contrary, a Pop is an activity where data is retrieved from the stack. When we
discuss data retrieval it only considers the topmost available data.
Q67. Can you explain with an example how does a variable declaration activity will
consume or affect the memory allocation?
Answer:
The amount of space or memory is occupied or allocated depends upon the data type of the
variables that are declared. So let’s explain the same by considering an example: Let’s say the
variable is declared as an integer type then 32 bits of memory storage is allocated for that
particular variable.
So based on the data type of the variable, the memory space will be allocated.
Q68. Define the advantages and disadvantages of the heap compared to a stack?
Answer:
On the contrary, the disadvantages of the heap compared to a stack are listed below:
1. The memory of the heap is slower when compared to the memory of the stack
Q69. Explain how new data can be inserted into the tree?
Answer:
The following are the steps that you need to follow to insert the data into the tree:
1. First of all, check whether the data is unique or not ( i.e. check whether the data that you
are going to insert doesn’t already exist in the tree).
2. Then check if the tree is empty. If the tree is empty then all you need to do is just insert a
new item into the root. If the key is smaller than that of a root’s key then insert that data
into the root’s left sub tree or otherwise, insert the data into the right side of the root’s sub
tree.
Q70. Can you tell me the minimum number of nodes that a binary tree can have?
Answer:
A binary tree is allowed or can have a minimum of zero nodes. Further, a binary tree can also
have 1 or 2 nodes.
Answer:
The nature of the dynamic data structure is different compared to the standard data structures, the
word dynamic data structures means that the data structure is flexible in nature. As per the need,
the data structure can be expanded and contracted. Thus it helps the users to manipulate the data
without worrying too much about the data structure flexibility.
Q72. Define what is an array?
Answer:
While referring to array the data is stored and utilized based on the index and this number
actually co-relates to the element number in the data sequence. So thus making it flexible to
access data in any order. Within programming language, an array is considered as a variable
having a certain number of indexed elements.
Q73. Can you tell me the minimum number of queues that are needed to implement a
priority queue?
Answer:
The minimum number of queues that are needed is two. Out of which, one queue is intended for
sorting priorities and the other queue is meant for the actual storage of data.
Q74. List out all different sorting algorithms that are available and state which sorting
algorithm is considered as the fastest?
Answer:
1. Quicksort
2. Bubble sort
3. Balloon sort
4. Radix sort
5. Merge sort
Out of the above sorting options, none of the sorting algorithms can be tagged as the fastest
algorithm, because each of these sorting algorithms is defined for a specific purpose. So based on
the data structure and data sets available the sorting algorithms are used.
Answer:
A de queue is nothing but a double-ended queue. Within this structure, the elements can be
inserted or deleted from both sides.
Answer:
A selection sort is a process where it picks up the smallest number from the entire data setlist and
places it at the beginning. The same process is continued where the second position is already
filled in. The same process is continued all the way until the list is completed. The selection sort
is defined as a simple sort algorithm when compared to others.
Answer:
A graph is nothing but a type of data structure that has a set of ordered pairs. In turn, these pairs
are again acknowledged as edges or arcs. These are used to connect different nodes where the
data can be accessed and stored based on the needs.
Answer:
Yes, you can implement a stack using two queues. Any data structure to act like a stack should
have a push() method to add data on top and a pop() method to remove the top data.
Answer:
Using two stacks, you can implement a queue. The purpose is to complete the queue's en queue
operation so that the initially entered element always ends up at the top of the stack.
First, to enqueue an item into the queue, migrate all the elements from the beginning
stack to the second stack, push the item into the stack, and send all elements back to the
first stack.
To dequeue an item from the queue, return the top item from the first stack.
Answer:
In data structures, you use LRU (Least Recently Used) cache to organize items in order of use,
enabling you to quickly find out which item hasn't been used for a long time.
Answer:
To implement the LRU cache, you should use two data structures: a hashmap and a doubly
linked list.
A hashmap helps with the lookup of cached keys, and a doubly-linked list helps maintain the
eviction order.
Array and Linked list are two ways of organizing the data in memory. The below table lists the
various differences between the array and linked lists:
Answer:
Dynamic Data Structure - A Linked list is a dynamic data structure so that it can grow
at runtime by deallocating and allocating memory. There is no need to present the linked
list initial size.
Insertion and Deletion Operations - Insertion and Deletion operations are easier in
linked lists. You need to have to update the address present in the next pointer of a node.
Implementation - Data structures like queue, tree, and stack are easily implemented
using the Linked list.
No memory wastage - Efficient memory utilization can be achieved in the linked lists.
Answer:
Expression evaluation
Backtracking
Function calling and return
Memory management
Checking parenthesis matching in an expression
Q85. What are the different types of Linked List?
Answer:
Singly Linked List - This is the most common type, and each node has data and a pointer
to the next node.
Doubly Linked List - In this type, the pointer is added to the previous node, either in a
forward or backward direction.
Circular Linked List - In this type, the last element is linked to the first element.
Q86. What is the difference between storage structure and file structure?
Answer:
The main difference between storage structure and file structure depends on the memory area
that is accessed.
Answer:
Push operation: To add an item to the stack. If the stack is complete, then it is in an
overflow condition.
Pop operation: It is used to remove an item from the stack. If it's an empty stack, then it
is in underflow condition.
isEmpty operation: If the stack is empty returns true, else false.
Peek or Top operation: This returns the top element of the stack.
Answer:
Operating system
Artificial Intelligence
Statistical analysis
Database management
Compiler design
Graphics
Simulation
Numerical analysis
Answer:
A linked list is a series of data structures connected via links. In simple words, it's a sequence of
links that contain items. After the array, the linked list is the second most used data structure.
The essential terms to understand the linked list are:
Next - In a linked list, each link is connected to the following link called next.
LinkedList - It contains the connection link to the first link called first.
The below diagram depicts how nodes are connected in the Linked List:
Basic operations supported by a linked list:
Answer:
It depends on where you plan to use Linked lists. You can consider a linked list for both non-
linear and linear data structures. When used for data storage, it is regarded as a non-linear data
structure. When used for access strategies, it is considered a linear data structure.
Answer:
A doubly linked list is one of the complex types of the linked list, where a node contains a
pointer to the previous and the next node in the sequence. It consists of three parts: node data, a
pointer to the next node in sequence (next pointer), a pointer to the previous node (previous
pointer)
Answer:
A queue is a linear data structure that supports a specific order in which operations are
performed. The order is FIFO (First in First Out) methodology, i.e., data items stored first will be
accessed first. Unlike stack, the queue is open at both ends, and one end is always used to insert
data and another one to remove data.
Answer:
As the name suggests, the queue is used whenever you need to manage a group of objects in the
order FIFO. A few of the queue data structure applications are listed below:
Serving requests on a single shared resource, like CPU task scheduling, printer, etc.
Handling interruptions in real-time systems.
Buffers in apps like CD player and MP3 media players
In maintaining a playlist in media players, like adding or removing songs.
Answer:
Both stack and heap are used for memory needs. The stack is primarily used to save the method
execution order and local variables, and always follow the LIFO order.
Whereas heap is used for dynamic allocation and deallocation of memory blocks. It stores
objects in Java. Memory allocated to the heap lives until one of the following events occurs:
Memory free
Program terminated
The size of heap memory is more when using recursion when compared with the stack, as it
quickly fill-ups stack memory.
Q95. Name a few graph data structure applications.
Answer:
Social graphs
Path optimization algorithms
Recommendation engines
Scientific computations
Answer:
An AVL (Adelson, Velskii, and Landi) is a self-balancing binary search tree where the variation
of heights of the right and left subtrees of any node is not more than one.
A tree is balanced if the balance factor of each node is between -1 to 1. Or else, the tree is
unbalanced and needs to be balanced.
Answer:
Answer:
Jagged arrays are a particular type of arrays used for storing rows of data of varying lengths to
improve efficiency when working with multidimensional arrays.
Answer:
A max heap in a data structure is a complete binary tree where each internal node's value is
greater than or equal to that node's children's values.
Answer:
You can find the height of a binary tree using a recursive Depth-First Search (DFS) algorithm, as
shown below:
Answer:
General Tree
Binary Tree
Forests
Expression Tree
Binary Search Tree
Tournament Tree
Q101. Which data structures do you use in DFS and BFS algorithms?
Answer:
Answer:
Stacks follows the LIFO method and the addition and retrieval of a data item takes only Ο (n)
time. Stacks are used where we need to access data in the reverse order or their arrival. Stacks
are used commonly in recursive function calls, expression parsing, depth first traversal of graphs
etc.
Q103. What are the criteria of algorithm analysis?
Answer:
An algorithm is generally analyzed on two factors − time and space. That is, how much
execution time and how much extra space required by the algorithm.
Answer:
Answer:
Asymptotic analysis can provide three levels of mathematical binding of execution time of an
algorithm − Best case is represented by Ω (n) notation. Worst case is represented by Ο (n)
notation. Average case is represented by Θ (n) notation.
Answer:
A linear data-structure has sequentially arranged data items. The next time can be located in the
next memory address. It is stored and accessed in a sequential manner. Array and list are
examples of linear data structure.
Answer:
The following operations are commonly performed on any data-structure − Insertion − adding a
data item Deletion − removing a data item Traversal − accessing and/or printing all data items
Searching − finding a particular data item Sorting − arranging data items in a pre-defined
sequence
Answer:
There are three commonly used approaches to develop algorithms − Greedy Approach − finding
the solution by choosing the next best option Divide and Conquer − diving the problem to a
minimum possible sub-problem and solving them independently Dynamic Programming −
diving the problem to a minimum possible sub-problem and solving them combined.
Q109. Give some examples greedy algorithms.
Answer:
The below given problems find their solution using greedy algorithm approach –
The below given problems find their solution using divide and conquer algorithm
approach − Merge Sort
Quick Sort
Binary Search
Strassen's Matrix Multiplication
Closest pair (points)
Q111. What are some examples of dynamic programming algorithms?
Answer:
The below given problems find their solution using divide and conquer algorithm approach −
A linked-list is a list of data-items connected with links i.e. pointers or references. Most modern
high-level programming language does not provide the feature of directly accessing memory
location; therefore, linked-list is not supported in them or available in form of inbuilt functions.
Q113. What is stack?
Answer:
In data-structure, stack is an Abstract Data Type (ADT) used to store and retrieve values in Last
In First Out method.
Q114. Why do we use stacks?
Answer:
Stacks follows the LIFO method and the addition and retrieval of a data item takes only Ο (n)
time. Stacks are used where we need to access data in the reverse order or their arrival. Stacks
are used commonly in recursive function calls, expression parsing, depth first traversal of graphs
etc.
Q115. What operations can be performed on stacks?
Answer:
Answer:
Queue is an abstract data structure, somewhat similar to stack. In contrast to stack, queue is
opened at both end. One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored
first will be accessed first.
Answer:
As queues follow FIFO method, they are used when we need to work on data-items in the exact
sequence of their arrival. Every operating system maintains queues of various processes. Priority
queues and breadth first traversal of graphs are some examples of queues.
Answer:
Answer:
Bubble sort is a comparison based algorithm in which each pair of adjacent elements is
compared and elements are swapped if they are not in order. Because the time complexity is
Ο (n2), it is not suitable for large set of data.
Answer:
Insertion sort divides the list into two sub-list, sorted and unsorted. It takes one element at a time
and finds it appropriate location in sorted sub-list and insert there. The output after insertion is a
sorted sub-list. It iteratively works on all the elements of unsorted sub-list and inserts them to
sort sub-list in order.
Answer:
Selection sort is in-place sorting technique. It divides the data set into two sub-lists: sorted and
unsorted. Then it selects the minimum element from the unsorted sub - list and places it in the
sorted list. This iterates unless all the elements from the unsorted sub - list are consumed into a
sorted sub - list.
Answer:
Merge sort is sorting algorithm based on divide and conquer programming approach. It keeps on
dividing the list into smaller sub-list until all sub-list has only 1 element. And then it merges
them in a sorted way until all sub-lists are consumed. It has run-time complexity of Ο (n log n)
and it needs Ο (n) auxiliary space.
Answer:
Shell sort can be said a variant of insertion sort. Shell sort divides the list into smaller sublist
based on some gap variable and then each sub-list is sorted using insertion sort. In best cases, it
can perform up to Ο (n log n).
Answer:
Quick sort uses divide and conquer approach. It divides the list in smaller 'partitions' using
'pivot'. The values which are smaller than the pivot are arranged in the left partition and greater
values are arranged in the right partition. Each partition is recursively sorted using quick sort.
Answer:
A graph is a pictorial representation of a set of objects where some pairs of objects are connected
by links. The interconnected objects are represented by points termed as vertices, and the links
that connect the vertices are called edges.
Answer:
Depth First Search algorithm (DFS) traverses a graph in a depthward motion and uses a stack to
remember to get the next vertex to start a search when a dead end occurs in any iteration.
Answer:
Breadth First Search algorithm (BFS) traverses a graph in a breadthwards motion and uses a
queue to remember to get the next vertex to start a search when a dead end occurs in any
iteration.
Answer:
Answer:
A binary tree has a special condition that each node can have two children at a maximum.
A binary search tree is a binary tree with a special provision where a node's left child must have
a value less than its parent's value and the node's right child must have a value greater than it's
parent value.
Answer:
Tree traversal is a process to visit all the nodes of a tree. Because, all nodes are connected via
edges (links) we always start from the root (head) node. There are three ways which we use to
traverse a tree –
In-order Traversal
Pre-order Traversal
Post-order Traversal
Answer:
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. A spanning tree does not have cycles and it cannot be disconnected.
Answer:
It depends on how connected the graph is. A complete undirected graph can have maximum nn-1
number of spanning trees, where n is the number of nodes.
Answer:
This algorithm treats the graph as a forest and every node, it as an individual tree. A tree
connects to another only and only if it has least cost among all available options and does not
violate MST properties.
Answer:
A heap is a special balanced binary tree data structure where the root - node key is compared
with its children and arranged accordingly. A min-heap, a parent node has key value less than its
children and a max-heap parent node has value greater than its children.
Answer:
A recursive function is one which calls itself, directly or calls a function that in turn calls it.
Every recursive function follows the recursive properties − base criteria where functions stops
calling itself and progressive approach where the functions tries to meet the base criteria in each
iteration.
Answer:
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than
one ring. All rings are of different size and stacked upon each other where the larger disk is
always below the small disk. The aim is to move the tower of disk from one peg to another,
without breaking its properties.
Answer:
Fibonacci Series generates a subsequent number by adding two previous numbers. For
example − 0 1 1 2 3 5 8 13.
Answer:
Hashing is a technique to convert a range of key values into a range of indexes of an array. By
using hash tables, we can create associative data storage where data index can be found by
providing its key values.
Answer:
Interpolation search is an improved variant of binary search. This search algorithm works in the
probing position of required values.
Q143. What is the prefix and post fix notation of (a + b) * (c + d) ?
Answer:
Prefix Notation − * + a b + c, d
Postfix Notation − a b + c d + *