Data Structure VIVA
Data Structure VIVA
In computer science, a data structure is a particular way of storing and organizing data in a computer’s
memory so that it can be used efficiently.
- Quantitative analysis of the structure, which includes determining the amount of memory needed to
store the structure and the time required to process the structure.
• Linear: A data structure is said to be linear if its elements form a sequence or a linear list.
Examples: Array. Linked List, Stacks and Queues
• Non-Linear: A data structure is said to be non-linear if the traversal of nodes is nonlinear in
nature. Example: Graph and Trees.
4. What are the various operations that can be performed on different Data Structures?
• Insertion Add a new data item in the given collection of data items.
• Deletion Delete an existing data item from the given collection of data items.
• Traversal Access each data item exactly once so that it can be processed.
• Searching Find out the location of the data item if it exists in the given collection of data
items.
• Sorting Arranging the data items in some order i.e. in ascending or descending order in
case of numerical data and in dictionary order in case of alphanumeric data .
5. What do you understand by Complexity?
The complexity of an algorithm is the function which gives the running time and /or space in terms of the
input size. It describes the efficiency of the algorithm in terms of the amount of data the algorithm must
process. There are two main complexity measures of the efficiency of an algorithm:
Time complexity
Space complexity
• The size of the arrays is fixed, Linked Lists are Dynamic in size.
• Inserting and deleting a new element in an array of elements is expensive, Whereas both
insertion and deletion can easily be done in Linked Lists.
• Random access is not allowed in Linked Listed.
• Extra memory space for a pointer is required with each element of the Linked list.
• Arrays have better cache locality that can make a pretty big difference in performance.
8. What is a Queue, how it is different from the stack and how is it implemented?
Queue is a linear structure that follows the order is First In First Out (FIFO) to access elements. Mainly the
following are basic operations on queue: Enqueue, Dequeue, Front, Rear
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. Both Queues and Stacks can be
implemented using Arrays and Linked Lists.
9. What are Infix, prefix, Postfix notations?
• Infix notation: X + Y – Operators are written in-between their operands. This is the usual
way we write expressions. An expression such as
A*(B+C)/D
• Postfix notation (also known as “Reverse Polish notation”): X Y + Operators are written
after their operands. The infix expression given above is equivalent to
A B C + * D/
• Prefix notation (also known as “Polish notation”): + X Y Operators are written before
their operands. The expressions given above are equivalent to
/*A+BCD
A linked list is a linear data structure (like arrays) where each element is a separate object. Each element
(that is node) of a list is comprising of two items – the data and a reference to the next node.Types of
Linked List :
1. Singly Linked List : In this type of linked list, every node stores address or reference of
next node in list and the last node has next address or reference as NULL. For example 1 -
>2->3->4->NULL
2. Doubly Linked List : Here, here are two references associated with each node, One of the
reference points to the next node and one to the previous node. Eg. NULL<-1<->2<->3-
>NULL
3. Circular Linked 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. Eg. 1->2->3->1 [The next pointer of last node is
pointing to the first]
11. Which data structures are used for BFS and DFS of a graph?
12. What is the difference between a File Structure and a Data Structure?
Low compatibility with external apps High compatibility with external apps
13. What is a linked list?
A linked list is a data structure that consists of individual entities called nodes. These nodes have the
capability to connect to other nodes and create a chain in the process. This continuous chain structure forms
a linked list, as the name suggests.
Various string operations have been developed. These are described below.
• Substring
• Indexing
• Concatenation
• Length
The operations usually associated with word processing are the following:
• Insertion
• Deletion and
• Replacement
Pattern matching is the problem of deciding whether or not a given string pattern P appears in a string text
T.
17. Where are Data Structures primarily used?
Data structures are very much needed in almost all of the fields that you can think of. Algorithms are the
primary requirement in every data handling situation. Following are some of the scenarios where data
structures are widely used:
• Numerical computation
• Artificial Intelligence
• Compiler design
• Database handling
• Lexical analysis
• Statistics
The two primary methods of searching are linear search and binary search.
Linear search involves iterating over a data unit in order to perform the required operation.
Binary search is more efficient in a way that it has the ability to split the data unit into chunks and then
perform a search operation.
A queue is a widely used data structure that is used to denote the ordered access and manipulation of an
element. The operation of this data structure is exactly the same as a literal queue in the real world. Elements
are added one after the other and are processed on the front end.
20. Distinguish between Array and Link List
❑ Memory Allocation
– The maintenance of linked lists in memory assumes the possibility of inserting new nodes
into the lists and hence requires some mechanism which provides unused memory space
for the new nodes.
❑ Garbage Collection
– Suppose some memory space becomes reusable because a node is deleted from a list or
an entire list is deleted from a program. Clearly, we want the space to be available for
future use. One way to bring this about is to immediately reinsert the space into the free-
storage list.
22. What is Overflow and Underflow?
Overflow
– Sometimes new data are to be inserted into a data structure but there is no available space.
This situation is usually called overflow.
– Overflow will occur in linked list when AVAIL = NULL and there is an insertion.
Underflow
– Underflow refers to the situation where one wants to delete data from a data structure that
is empty.
– Underflow will occur in linked lists when START= NULL and there is a deletion.
–
A header linked list is a linked list which always contains a special node, called the header node at
beginning of the list. The following are two kinds of widely used header lists:
– A grounded header list is a header list where the last node contains the null pointer.
– A circular header list is a header list where the last node points back to the header
node.
A two-way list is a new list structure which can be traversed in two directions:
- in the usual forward direction from the beginning of the list to the end.
- in the backward direction from the end of the list to the beginning.
25. What is a Complete Binary Tree?
A tree is said to be complete if all its levels, except possibly the last, have the maximum number of possible
nodes, and if all the nodes at the last level appear as far left as possible.
A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children.
A stack is a widely used data structure that provides users with the ability to work with data at one point
only that is the top element.
LIFO stands for the Last in, First out access order. It is directly corresponding to how the data can be worked
on and modified. The data entity that is stored or pushed in last is the first one to be worked on at any point
in time. If there is a requirement to access the very first element stored, then first you have to retrieve all of
the data that came in after that element.
32. What is a Binary Search Tree?
A binary search tree is a data structure that stores data in a very efficient manner. It consists of two primary
nodes from the root node. The main thing here is that the values of the nodes in the left sub-tree are less
in number than the value of the root node, and the values of the nodes on the right of the root node are
correspondingly higher than the root. Also, individually both of these left and right sub-trees are their own
binary search trees at all points of time.
FIFO, also known as First in, First out, is a way of representing a data operation on factors such as how data
is accessed and in what order. Here, the data that is first put into the list will be the first entity to exit from
the ordered data structure.
34. What is the difference between void and null in Data Structures?
Void is a data type identifier in data structures, while null is considered to be a value with no physical
presence. When void is used, it indicates that there is no size while initializing the data structure.
Both push and pop operations denote how data can be stored and used when required in a stack. The push
operation denotes that users are adding data into the structure, and the pop operation denotes that the
data is being pulled or removed from the structure. Usually, the top-most element is considered when
performing push and pop operations.
A variable is stored based on the amount of memory that is needed. First, the required quantity of memory
is assigned, and later, it is stored based on the data structure being used. Using concepts such as dynamic
allocation ensures high efficiency and that the storage units can be supplied based on the requirements in
real time.
37. What is merge sort?
Merge sort is a method of sorting, which is based on the divide and conquer technique. Here, data entities
adjacent to each other are first merged and sorted in every iteration to create sorted lists. These smaller
sorted lists are combined at the end to form the completely sorted list.
Post-fix expressions are used in a scenario where every operator is preceded by its operands. Using this
ensures to eliminate the need for parentheses or sub-expressions when it comes to the concept of operator
precedence.
Dynamic data structures provide users with a lot of flexibility in terms of the provision of data storage and
manipulation techniques, which can change during the operation of the algorithm or the execution of the
program.
An AVL tree is a type of a binary search tree where the tree is only slightly balanced. Balance is the unit of
comparison between the heights of the subtrees from the main (root) node.
Huffman’s algorithm uses a table, containing the frequency of the occurrence of every data entity on the
list. This is used for creating extended binary trees, which are known to have minimum weights for the path
lengths . This is considering each of the corresponding weights.
Next up on this compilation of top Data Structures interview questions, we can check out the questions
categorized as advanced.
Recursive algorithms are algorithms that solve a problem by breaking it down into simpler sub-problems
and then solving them iteratively. The output of one recursion operation is usually the direct input for the
next iteration operation, and this process goes on.
43. How does bubble sort work?
Bubble sort is one of the most used sorting techniques out there. It is applied to arrays where elements
adjacent to each other are compared and values are exchanged based on the order of arrangement. It’s
called bubble sort because of the concept of exchanging elements like a bubble floating to the top of the
water and larger entities sinking down to the bottom end.
Among the many types of algorithms such as bubble sort, quick sort, merge sort, and more, it is not right
to put one method on the podium for performance as this greatly varies based on data, the reaction after
the algorithm processes the data, and how it’s stored. The concept of time complexity is considered here.
Tree data structures are used in a variety of applications. Following are some of them:
Next on this Data Structures and Algorithms interview questions is a question that is commonly asked, so
pay attention.
47. What are the Data Structures that are used in graphs?
To implement graphs, two data structures play a key role. They are:
In the depth-first search (DFS), the stack data structure is made use of.
In the case of the breadth-first search (BFS) technique, queues are used.
49. What are the time complexities of linear search and binary search?
Binary search is more effective as it takes lesser comparisons to search for an element in an array. The time
complexity for linear search is O(n), while it is O(log n) for binary search.
There are two main downsides when implementing queues using arrays. They are as follows:
• Array sizing: The queue has to be constantly extended to make way for more elements that
get implemented. Always extending the size of the array will not be feasible as there will be a
discrepancy in the creation of the correct array size.
• Memory dumps: The memory that is used to store the queue elements cannot be reused to
actually store the queue. This is because of the working of queues where insertion happens
at the head node only.
One inherent advantage of linked list is that it is very easy to modify irrespective of the number of elements
that are there in the list.
52. What is the least number of nodes that a binary tree can have?
A binary tree can have zero nodes as the least number. Further, the number can be increased to 1 or 2
nodes.
A binary search is an algorithm that is best applied to search a list when the elements are already in order
or sorted.
54. What is the advantage of the heap over a stack?
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.
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.
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.
A problem can be solved in more than one ways. So, many solution algorithms can be derived for a given
problem. We analyze available algorithms to find and implement the best suitable algorithm.
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.
Asymptotic analysis can provide three levels of mathematical binding of execution time of an algorithm −
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
combinedly
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
The below given problems find their solution using divide and conquer algorithm approach −
• Project scheduling
Stacks follows LIFO method and 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.
As queues follows FIFO method, they are used when we need to work on data-items in 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.
Bubble sort is 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.
67. Tell me something about 'insertion sort'?
Insertion sort divides the list into two sub-list, sorted and unsorted. It takes one element at 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 sorted sub-list in order.
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 unsorted sub-list and places it into the sorted list. This iterates
unless all the elements from unsorted sub-list are consumed into sorted sub-list.
Both sorting techniques maintains two sub-lists, sorted and unsorted and both take one element at a time
and places it into 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.
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.
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 upto Ο(n log
n).
72. How quick sort works?
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.
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.
A binary search tree is a binary tree with a special provision where a node's left child must have value less
than its parent's value and node's right child must have value greater than it's parent value.
AVL trees are height balancing binary search tree. AVL tree checks the height of left and right sub-trees and
assures that the difference is not more than 1. This difference is called Balance Factor.
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 can not be disconnected.
It depends on how connected the graph is. A complete undirected graph can have maximum nn-1 number
of spanning trees, where n is number of nodes.
Heap is a special balanced binary tree data structure where root-node key is compared with its children and
arranged accordingly. A min-heap, a parent node has key value less than its childs and a max-heap parent
node has value greater than its childs.