DSA Viva Questions
DSA Viva Questions
A data structure is a model for organizing and storing data. Stacks, Queue, Linked Lists, and
Trees are the examples of different data structures. Data structures are classified as either
linear or non-linear:
• A data structure is said to be linear if only one element can be accessed from an element
directly. Eg: Stacks, Lists, Queues.
A data structure is non-linear if more than one elements can be accessed from an element
directly. Eg: Trees, Graphs, Heap.
2. Define ADT?
An abstract data type is a set of objects together with a set of operations. Abstract data
types are mathematical abstraction. Objects such as lists, sets, graphs, trees can be viewed
as abstract data types.
3. What do you mean by LIFO and FIFO? LIFO stands for 'Last In First Out', it says how the
data to be stored and retrieved. It means the data or element which was stored last should
come out first. Stack follows LIFO.
* FIFO stands for 'First In First Out', here the data(or element) which was stored first should
be the one to come out first. It is implemented in Queue.
4. What is Stack?
A stack is a list of elements in which insertion and deletions can take place only at one end,
this end is called as stack 'top' Only top element can be accessed. A stack data structure
has LIFO (Last In First Out) property. A stack is an example of linear data structure.
• Pop operation deletes the top element from the stack. • Peek returns or give the top
element without deleting it.
4. Stack is very important data structure being used to implement function calls efficiently.
5. Parsing
7. What is a Queue?
A Queue is a particular data structure in which the elements in the collection are kept in
order. Unlike Stack, queue is opened at both end. Queue follows 'FIFO' method where
element is inserted from rear end and deleted or removed from front end.
Dequeue operation deletes the front element from the list. • Isempty() operation checks
whether queue is empty or not.
A binary tree is a 2-ary tree in which each node (N) has atmost 2 children (either 0 or 1). The
node with 2 children are called internal nodes, and the nodes with 0 children are called
external nodes or leaf nodes.
A Binary tree is complete binary tree if all levels are completely filled except possibly the
last/lowest level are full and in the last/lowest level all the items are on the left.
12. What is Perfect Balanced Binary Tree?
A perfect balanced binary tree is a binary tree where each node has same number of nodes
in both subtrees..
Height is a general measured as number of edges from the root to deepest node in the tree.
Traversal is used to visit each node in the tree exactly once. A full traversal of a binary tree
gives a linear ordering of the data in the tree. There are 3 different type of tree traversal:
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
A BST(Binary Search Tree) is a binary tree in which each node satisfies search property.
Each node's key value must be greater than all values in its left subtree and must be less
than all values in its right subtree.
AVL tree is a self-balancing binary search tree with height- balanced condition. For every
node the height of left subtree and right subtree can be differ by at most 1.
A B-Tree is a tree data structure that keeps data sorted and allows searches, insertions,
deletions, and sequential access in logarithmic amount of time.
In B-Tree, internal nodes can have a variable number of child nodes within some pre-defined
range. A B-tree is kept balanced by requiring that all leaf nodes are at the same depth.
5. Leaf nodes contain atleast m-1 children and atmost 2m-1 keys.
A B+ Tree is a tree data structure which represents sorted data in a way that allows for
efficient insertion, retrieval and removal of records, each of which is identified by a key. It is a
dynamic multilevel index, with maximum and minimum bounds on the number of keys in
each segment (usually called a 'block' or 'node')
It is a complete binary tree where all the levels except possibly the last/lower level are full
and in the last/lower level all the items are on the left. Due to this fact that binary heap is a
complete binary it can be implemented using a simple array.
Order Property
The heap order property for MinHeap is 'a parent is less than (or equal to) its children
whereas for MaxHeap a parent is greater than its children'.
Bubble sort is a simple sorting algorithm, it works by repeatedly stepping through the list to
be sorted comparing each pair of adjacent items and swapping if they are in the worng order.
iteration of insertion sort removes an element from the input data and insert it into the correct
position in the already sorted list until no input element remains. The running time is the total
number of comparisons that is n(n-1)/2 which implies O(n²) time complexity.
Insertion sort is a simple comparison sorting algorithm, the algorithm works as follows:
3. Repeat the steps above for the remainder of the list (starting at the second position and
advancing each time).
The running time is the total number of comparisons that is n(n-1)/2 which implies O(n²) time
complexity.
Merge sort is an O(nlogn) comparison-based divide and conquer sorting algorithm, it works
as follows:
algorithm.
* If the running time of merge sort for a list of length n is T(n) then the recurrence relation is
T(n) = 2T(n/2) + n, thus after simplifying the recurrence relation T(n) = O(nlogn).
Heap Sort is a comparison-based sorting algorithm which is much more efficient version of
selection sort, it works by determining the largest (or smallest) element of the list, placing
that at the end (or beginning) of the list, then continuing with the rest of the list, but
accomplishes this task efficiently by using a data structure called a heap. Once the data list
has been made into a heap, the root node is gurantedd to be the largest (or smallest)
element.
Quick sort sorts by employing a divide and conquer strategy to divide a list into two sub-lists.
The steps are:
2. Reorder the list so the elements which are less than the pivot come before the pivot and
the elements greater than pivot come after it. After this partitioning the pivot is in its final
positon.
3. Recursively sort the sub-list of lesser elements and the sub- list of greater elements.
The running time complexity for worst case is O(n²) and for best and average case it is same
i.e., O(nlogn).
A graph is a pair of sets (V,E), where V is the sets of vertices and E is the set of edges
connecting the pair of vertices.
A undirected graph is connected if there is a path from every vertex to every other vertex. A
directed graph with this property is called strongly connected.
A complete graph is a graph in which every vertex is connected to every other vertex. That
means each vertex's degree in undirected graph is n-1.
* A graph can be represented in two forms 'Adjacency matrix' and 'Adjacency list.
Adjacency matrix is a two dimensional arrays where for each edge, (u,v) A[u,v] = true
otherwise it will be false. The space complexity to represent a graph using this is O(IVI).
Adjacency list are used where each list stores adjacent vertices of the corresponding vertex.
The space complexity to represent a graph using this is O(IV|+|E|).
A Spanning tree is a graph which must include every vertex and if the graph contain n
vertices, spanning tree must contain exactly (n-1) edges and is a subgraph of G.
A Minimum Spanning tree is a spanning tree such that the summ of all the weights of edges
in spanning tree is minimum.
37. Explain algorithm for finding Minimum Spanning Tree? There are 2 widely used algorithm
for finding minimum spanning tree:
1. Prim's Algorithm
2. Kruskal Algorithm
Prims Algorithm computes the minimum spanning tree by including appropriate vertex and
thus one edge into existing partially constructed tree in successive stages. The time
complexity of the Prim's Algorithm is O((V+E)logV). Kruskal Algorithm computes the
minimum spanning tree by adding edges one by one into a growing spanning tree. Initially
from the graph G, consider a forest of all the vertices without any edge.