You are on page 1of 30

KCC Institute of Technology & Management

Greater Noida (U.P)


Data Structure Interview Question

Q1. What is data structure?

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.

Q2. Differentiate between file and structure storage structure.

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.

Q3. What are various data-structures available?

Answer:

Data Structure availability may vary by Programming Languages. Commonly available data
structures are list, arrays, stake, queues, graph, tree etc.

Q4. What is algorithm?

Answer:

Algorithm is a step by step procedure, which defines a set of instructions to be executed in


certain order to get the desired output.

Q5. What is hashing?

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.

Q6. How do you reference all the elements in a one-dimension array?

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.

Q7.  In what areas do data structures are applied?

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.

Q8. What is LIFO?

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.

Q9. What is a queue?

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.

Q10. What are binary trees?

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.

Q12. What is a stack?

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.

Q13. Explain Binary Search Tree.

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.

Q14. What are multidimensional arrays?

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.

Q15. Are linked lists considered linear or non-linear data structures?

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.

Q16. How does dynamic memory allocation help in managing data?

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.

Q17. What is FIFO?

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.

Q18. What is an ordered list?

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.

Q20. Differentiate NULL and VOID

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.

Q21. What is the primary advantage of a linked list?

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.

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

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.

Q23. What is a linear search?

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.

Q24. How does variable declaration affect memory allocation?

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.

Q25. What is the advantage of the heap over a stack?

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.

Q26. What is a postfix expression?

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.

Q27. What is Data abstraction?

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.

Q28. How do you insert a new item in a binary search tree?

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.

Q29. How does a selection sort work for an array?

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.

Q30. How do signed and unsigned numbers affect memory?

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.

Q32. What are dynamic data structures?

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.

Q33. In what data structures are pointers applied?

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.

Q34. Do all declaration statements result in a fixed reservation in memory?

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.

Q35. What are ARRAYs?

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.

Q37. Which sorting algorithm is considered the fastest?


Answer:

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.

Q38. Differentiate STACK from ARRAY.

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.

Q39. Give a basic algorithm for searching a binary search tree.

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

Q40. What is a dequeue?

Answer:

A dequeue is a double-ended queue. This is a structure wherein elements can be inserted or


removed from either end.

Q41. What is a bubble sort and how do you perform it?

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.

Q42. What are the parts of a linked list?

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.

Q43. How does selection sort work?


Answer:

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.

Q44. What is a graph?

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.

Q45. Differentiate linear from a nonlinear data structure.

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.

Q46. What is an AVL tree?

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.

Q47. What are doubly linked lists?

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.

Q48. What is Huffman’s algorithm?

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.

Q49. What is Fibonacci search?


Answer:

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.

Q50. Briefly explain recursive algorithm.

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.

Q51. How do you search for a target key in a linked list?

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:

The basic operations performed on data structures are as follows:

 Insertion - Adds a new data element in the data structure.  


 Deletion - Removes a data element in a data structure. 
 Searching - Involves searching for a specified data element in a data structure.
 Traversal - Processing all data elements present in it.
 Merging - Combines two similar data structures to form a new data structure of the
same type.
 Sorting - Arranging data elements of a data structure in a specified order.

Q53. Explain what is a linked list?

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.

Q56. What is infix, prefix, and postfix in data structure?

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 −

Infix Notation Prefix Notation Postfix Notation


x+y +xy xy+
(x + y) ∗ z ∗+xyz xy+z∗
x ∗ (y + z) ∗x+yz xyz+∗
x/y+z/w +/xy/zw xy/zw/+
(x + y) ∗ (z + w) ∗+xy+zw xy+zw+∗
((x + y) ∗ z) - w -∗+xyzw xy+z∗w-

Q57. Explain the terminology LIFO?

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.

Q58. Explain what is a binary tree?

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.

Q59. Define what is a stack?

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.

Q60. Explain what are multidimensional arrays?

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.

Also, they are capable of storing simple structured data types.

Q63. What is FIFO?

Answer:

FIFO in data terminology stands as “First in, First Out”.

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.

Q64. Explain what merge sort is and how it is useful?

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.

Q65. List out all the advantages of a linked 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.

Q66. Explain the main difference between PUSH and a POP?

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:

The advantages of the heap compared to a stack are listed below:

1. Heap is more flexible when compared to a stack


2. Memory space of the heap can actually be allocated and de-allocated as per the need.

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.

Q71. Explain a little bit about the dynamic data structure?

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:

The list of all sorting algorithms are below:

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.

Q75. Explain what is a dequeue?

Answer:

A de queue is nothing but a double-ended queue. Within this structure, the elements can be
inserted or deleted from both sides.

Q76. Explain the process of how a selection sort works?

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.

Q77. Explain what is a graph?

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.

Q78. Is it possible to implement a stack using a queue?

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.

Q79. How would you implement a queue using a stack?

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.

Q80. Where is the LRU cache used in data structure?

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.

Q81.  Which Data Structure is used to implement LRU cache? 

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.

Q82. What is the difference between an array and a linked list?


Answer:

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:

Array Linked List


A Linked list is a set of objects known as a node,
An array is a sequence of elements of a similar
where it internally consists of two parts, i.e., data
data type.
and address.
It can be accessed irregularly using the array Linked lists support random access. Only
index. supports sequential access.
New elements can be stored anywhere, and a
Array elements store in contiguous locations in
reference is created for the new element using
memory.
pointers.
In arrays, memory allocation is done during In linked lists, memory allocation is done during
compile time. runtime.
Array size must be defined at the time of Linked list size grows when new elements are
declaration/initialization. inserted or deleted.

Q83. What are the advantages of Linked Lists over Array?

Answer:

The following are the advantages of Linked Lists over Arrays:

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

Q84. What are the applications of a stack in a data structure?

Answer:

Following are some of the essential applications in a data structure:

 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:

The following are the different types of Linked Lists.

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

 Storage structure: It's a data structure representation in computer memory. 


 File structure: It's a storage structure representation in the auxiliary memory.
Q87. What operations can be performed on a stack?

Answer:

Mainly the following operations are performed on a stack:

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

Q88. Name some applications of data structures?

Answer:

 Operating system
 Artificial Intelligence
 Statistical analysis
 Database management
 Compiler design
 Graphics
 Simulation 
 Numerical analysis

Q89. Explain about Linked List Data Structure.

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:

Link - In a linked list, each link stores data called an element.

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:

 Insertion - Inserts an element at the list beginning.


 Deletion - Deletes an element at the list beginning.
 Display - Displays the complete list.
 Search - Searches an element using the given key.
 Delete - Deletes an element using the given key.

Q90. Are linked lists considered non-linear or linear data structures?

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.

Q91. What is a doubly-linked list used for?

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)

The below diagram depicts the working of doubly linked list:


Some of the real-time applications where doubly-linked lists used are navigation systems and
browsers (when both back and front navigation is required).

Q92. What is a queue in data structure?

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.

The basic operations associated with queues -

 Dequeue - To remove an item


 Enqueue - To insert an item
 isempty() − Confirms whether the queue is empty.
 isfull() − Confirms whether the queue is full.
 peek() − Gets the element at the front of the queue without removing it.

Q93. List a few queue data structure applications.

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.

Q94. What is the difference between stack and heap?

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:

Applications of graph data structures in real-time are:

 Social graphs
 Path optimization algorithms
 Recommendation engines
 Scientific computations

Q96. What is an AVL tree?

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.

An example of an AVL tree:

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.

Q97. How do you detect a loop in a linked list?

Answer:

 Using Floyd's cycle-finding Algorithm


 Using hashing
 Using the visited nodes method 

Q98. What is a Jagged array?

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.

Q98. What is the max heap in the data structure?

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.

Q99.  How to find the height of a node in a tree?

Answer:

You can find the height of a binary tree using a recursive Depth-First Search (DFS) algorithm, as
shown below:

 Base case: If there is no node, return 0.


 Else: If there are 1 or 2 children, return the peak of the height of the left and right sub
trees, plus 1 to account for the current node.

Q100. List the types of trees.

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:

 In the DFS algorithm, you use the Stack data structure.


 In the BFS algorithm, you use the Queue data structure.

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

Q104. What is asymptotic analysis of an algorithm?

Answer:

Asymptotic analysis of an algorithm refers to defining the mathematical boundation /framing of


its run-time performance. Using asymptotic analysis, we can very well conclude the best case,
average case and worst case scenario of an algorithm.

Q105. What are asymptotic notations?

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.

Q106. What is linear data structure?

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.

Q107. What are common operations that can be performed on a 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

Q108. Briefly explain the approaches to develop algorithms.

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 –

 Travelling Salesman Problem


 Prim's Minimal Spanning Tree Algorithm
 Kruskal's Minimal Spanning Tree Algorithm
 Dijkstra's Minimal Spanning Tree Algorithm
 Graph - Map Coloring Graph - Vertex Cover
 Knapsack Problem
 Job Scheduling Problem

Q110. What are some examples of divide and conquer algorithms?


Answer:

 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 −

 Fibonacci number series


 Knapsack problem
 Tower of Hanoi
 All pair shortest path by Floyd-Warshall
 Shortest path by Dijkstra
 Project scheduling
Q112. What is a linked-list?
Answer:

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:

The below operations can be performed on a stack –


Push () − adds an item to stack
Pop () − removes the top stack item
Peek () − gives the value of top item without removing it
Isempty () − checks if stack is empty
Useful () − checks if stack is full

Q116. What is a queue in data-structure?

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.

Q117. Why do we use queues?

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.

Q118. What operations can be performed on Queues?

Answer:

The below operations can be performed on a stack –


Enqueue () − adds an item to rear of the queue
Dequeue () − removes the item from the front of the queue
Peek () − gives a value of front item without removing it
Isempty () − checks if stack is empty
Useful () − checks if stack is full
Q119. What is bubble sort and how bubble sort works?

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.

Q120. Tell me something about 'insertion sort'?

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.

Q121. What is selection sort?

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.

Q122. How insertion sort and selection sorts are different?


Answer:
Both sorting techniques maintain two sub-lists, sorted and unsorted and both take one element at
a time and places it into a sorted sub - list. Insertion sort works on the current element in hand
and places it in the sorted array at appropriate location maintaining the properties of insertion
sort. Whereas, selection sort searches the minimum from the unsorted sub-list and replaces it
with the current element in hand.

Q123. What is merge sort and how it works?

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.

Q124. What is shell sort?

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

Q125. How quick sort works?

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.

Q126. What is a graph?

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.

Q127. How depth first traversal works?

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.

Q128. How breadth first traversal works?

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.

Q129. What is a tree?

Answer:

A tree is a minimally connected graph having no loops and circuits.

Q130. What is a binary tree?

Answer:

A binary tree has a special condition that each node can have two children at a maximum.

Q131. What is a binary search tree?


Answer:

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.

Q132. What is tree traversal?

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

Q133. What is a spanning tree?

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.

Q134. How many spanning trees can a graph has?

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.

Q135. How Kruskal's algorithm works?

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.

Q136. What is a minimum spanning tree (MST)?


Answer:
In a weighted graph, a minimum spanning tree is a spanning tree that has a minimum weight that
all other spanning trees of the same graph.

Q137. What is a heap in data structure?

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.

Q138. What is a recursive function?

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.

Q139. What is tower of hanoi?

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.

Q140. What is fibonacci series?

Answer:

Fibonacci Series generates a subsequent number by adding two previous numbers. For
example − 0 1 1 2 3 5 8 13.

Q141. What is hashing?

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.

Q142. What is interpolation search technique?

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 + *

You might also like