Dsa Viva Questions
Dsa Viva Questions
Dsa Viva Questions
The Data Structure is the way data is organized (stored) and manipulated for retrieval and access. It also
defines the way different sets of data relate to one another, establishing relationships and forming algorith
ms.
Lists: A collection of related things linked to the previous or/and following data items.
Arrays: A collection of values that are all the same.
Records: A collection of fields, each of which contains data from a single data type.
Trees: A data structure that organizes data in a hierarchical framework. This form of data structure follows
the ordered order of data item insertion, deletion, and modification.
Tables: The data is saved in the form of rows and columns. These are comparable to records in that the o
utcome or alteration of data is mirrored across the whole table.
Examples of linear data structures are Arrays, Stack, Strings, Queue, and Linked List.
Numerical analysis, operating system, AI, compiler design, database management, graphics, statistical an
alysis, and simulation.
Column-Major Order: In column-major ordering, all of the columns of a 2D array are stored in memory in t
he same order. The first column of the array is entirely saved in memory, followed by the second row of th
e array, and so on until the last column of the array is wholly recorded in memory.
A linked list is a dynamic data structure, where the number of nodes is not fixed, and the list has the abilit
y to grow and shrink on demand.
We deal with an unknown number of objects or dont know how many items are in the list
We need constant-time insertions/deletions from the list, as in real-time computing where time predictabilit
y is critical
Random access to any elements is not needed
The algorithm requires a data structure where objects need to be stored irrespective of their physical addr
ess in memory
We need to insert items in the middle of the list as in a priority queue
Some implementations are stacks and queues, graphs, directory of names, dynamic memory allocation, a
nd performing arithmetic operations on long integers.
10. What are the advantages of a linked list over an array? In which scenarios do we use Linked List and
when Array?
This is another frequently asked data structure interview questions! Advantages of a linked list over an arr
ay are:
3. No Wastage of Memory
As the size of a linked list can increase or decrease depending on the demands of the program, and mem
ory is allocated only when required, there is no memory wasted. In the case of an array, there is memory
wastage. For instance, if we declare an array of size 10 and store only five elements in it, then the space f
or five elements is wasted.
4. Implementation
Data structures like stack and queues are more easily implemented using a linked list than an array.
Examples include:
Examples are the dynamic array, linked list, stack, queue, and heap.
The time complexity of an algorithm quantifies the amount of time taken for an algorithm to run as a functi
on of the length of the input. The space complexity quantifies the amount of space or memory taken by an
algorithm, to run as a function of the length of the input.
PUSH: The push action inserts a new element into the stack. The new feature is placed at the top of the s
tack. However, before inserting the value, we must first verify if TOP=MAX1, since if so, the stack is filled,
and no more insertions are possible. An OVERFLOW message is printed if an attempt is made to put a va
lue into an existing stack.
POP: The pop operation is performed to remove the stack’s topmost element. However, before removing
the value, we must first verify if TOP=NULL, since if it is, the stack is empty, and no further deletions are p
ermitted. An UNDERFLOW notice is produced if an attempt is made to erase a value from a stack that is
already empty.
PEEK: A peek action returns the value of the stack’s topmost element without removing it from the stack.
On the other hand, the Peek operation first checks if the stack is empty, i.e., if TOP = NULL, then an appr
opriate message is written. Otherwise, a value is returned.
As waiting lists for a single shared resource in a printer, CPU, call center systems, or image uploads; whe
re the first one entered is the first to be processed
In the asynchronous transfer of data; or example pipes, file IO, and sockets
As buffers in applications like MP3 media players and CD players
To maintain the playlist in media players (to add or remove the songs)
22. What is a Dequeue?
It is a double-ended queue, or a data structure, where the elements can be inserted or deleted at both en
ds (FRONT and REAR).
Heap is more flexible than the stack because memory space can be dynamically allocated and de-allocat
ed as needed
Heap memory is used to store objects in Java, whereas stack memory is used to store local variables and
function call
Objects created in the heap are visible to all threads, whereas variables stored in stacks are only visible t
o the owner as private memory
When using recursion, the size of heap memory is more whereas it quickly fill-ups stack memory
The acronyms stand for Pushing and Popping operations performed on a stack. These are ways data is st
ored and retrieved.
PUSH is used to add an item to a stack, while POP is used to remove an item.
PUSH takes two arguments, the name of the stack to add the data to and the value of the entry to be add
ed. POP only needs the name of the stack.
When the stack is filled and another PUSH command is issued, you get a stack overflow error, which mea
ns that the stack can no longer accommodate the last PUSH. In POP, a stack underflow error occurs whe
n youre trying to POP an already empty stack.
Cache-efficient: It linearly scans and linearly partitions the input. This means we can make the most of ev
ery cache load.
Can skip some swaps: As QuickSort is slightly sensitive to input that is in the right order, it can skip some
swaps.
Efficient even in worst-case input sets, as the order is generally random.
Easy adaption to already- or mostly-sorted inputs.
When speed takes priority over stability.
Scan all items and find the smallest. Switch over the position as the first item. Repeat the selection sort o
n the remaining N-1 items. We always iterate forward (i from 0 to N-1) and swap with the smallest elemen
t (always i).
Merge Sort is a sorting algorithm as well. The algorithm divides the array into two halves, sorts them recur
sively, and then combines the two sorted halves. The goal of points that are closest together is to identify
the nearest pair of points in an x-y plane collection of points. The issue may be solved in O(n2) time by co
mputing the distances between each pair of locations and comparing them to determine the shortest dista
nce.
Data
37. What are the differences between the B tree and the B+ tree?
The B tree is a self-balancing m-way tree, with m defining the tree’s order. Depending on the number of m
, Btree is an extension of the Binary Search tree in which a node can have more than one key and more t
han two children. The data is provided in the B tree in a sorted manner, with lower values on the left subtr
ee and higher values on the right subtree.
The B+ tree is an advanced self-balanced tree since every path from the tree’s root to its leaf is the same
length. The fact that all leaf nodes are the same length indicates that they all occur at the same level. Spe
cific leaf nodes cant appear at the third level, while others appear at the second level.
38. What are the advantages of binary search over a linear search?
In a sorted list:
A binary search is more efficient than a linear search because we perform fewer comparisons. With linear
search, we can only eliminate one element per comparison each time we fail to find the value we are look
ing for, but with the binary search, we eliminate half the set with each comparison.
Binary search runs in O(log n) time compared to linear searchs O(n) time. This means that the more of th
e elements present in the search array, the faster is binary search compared to a linear search.
42. Name the ways to determine whether a linked list has a loop.
Using hashing
Using the visited nodes method (with or without modifying the basic linked list data structure)
Floyds cycle-finding algorithm
1.Linear
2.Nonlinear
It must rich enough in structure to reflect the actual relationship of data in real world.
The structure should be simple enough for efficient processing of data.
1.Values definition
2.Operation definition
56. What are the advantages of linked list over array (static data structure)?
The disadvantages of array are
unlike linked list it is expensive to insert and delete elements in the array
One cant double or triple the size of array as it occupies block of memory space.
In linked list
each element in list contains a field, called a link or pointer which contains the address of the next elemen
t
Successive elements need not occupy adjacent space in memory.
57. Can we apply binary search algorithm to a sorted linked list, why?
No we cannot apply binary search algorithm to a sorted linked list, since there is no way of indexing the m
iddle element in the list. This is the drawback in using linked list as a data structure.
When new data is to be inserted into the data structure but there is no available space i.e. free storage list
is empty this situation is called overflow.
When we want to delete data from a data structure that is empty this situation is called underflow.
Ascending priority queue- Here smallest item can be removed (insertion is arbitrary)
Descending priority queue- Here largest item can be removed (insertion is arbitrary)
65. What are the disadvantages of representing a stack or queue by a linked list?
1.A node in a linked list (info and next field) occupies more storage than a corresponding element in an ar
ray.
2.Additional time spent in managing the available list.
After a call to free(p) makes a subsequent reference to *p illegal, i.e. though the storage to p is freed but t
he value of p(address) remain unchanged .so the object at that address may be used as the value of *p (i.
e. there is no way to detect the illegality).Here p is called dangling pointer.
To avoid this it is better to set p to NULL after executing free(p).The null pointer value doesnt reference a
storage location it is a pointer that doesnt point to anything.
67. What are the disadvantages of linear list?
1.We cannot reach any of the nodes that precede node (p)
2.If a list is traversed, the external pointer to the list must be persevered in order to reference the list agai
n
1.An info field that contains the information stored in the node
2.Left field that contain pointer to node on left side
3.Right field that contain pointer to node on right side
72. What are the issues that hamper the efficiency in sorting a file?
The issues are
76. List out the areas in which data structures are applied extensively?
Compiler Design,
Operating System,
Database Management System,
Statistical analysis package,
Numerical Analysis,
Graphics,
Artificial Intelligence,
Simulation
77. What are the major data structures used in the following areas : network data model & Hierarchical da
ta model?
RDBMS Array (i.e. Array of structures)
Network data model Graph
Hierarchical data model Trees
78. If you are using C language to implement the heterogeneous linked list, what pointer type will you use
?
The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to con
nect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is cap
able of storing pointer to any type as it is a generic pointer type.
Stack. Because of its LIFO (Last In First Out) property it remembers its caller so knows whom to return w
hen the function has to return. Recursion makes use of system stack for storing the return addresses of th
e function calls.
Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent it
erative procedures are written, explicit stack is to be used.
81. What are the notations used in Evaluation of Arithmetic Expressions using prefix and postfix forms?
Polish and Reverse Polish notations.
82. Convert the expression ((A + B) * C (D E) ^ (F + G)) to equivalent Prefix and Postfix notations?
1.Prefix Notation:
^ * +ABC DE + FG
2.Postfix Notation:
AB + C * DE FG + ^
85. List out few of the applications that make use of Multilinked Structures?
Sparse matrix, Index generation.
87. What is the type of the algorithm used in solving the 8 Queens problem?
Backtracking
89. In RDBMS, what is the efficient data structure used in the internal storage representation?
B+ tree. Because in B+ tree, all the data is stored only in leaf nodes, that makes searching easier. This co
rresponds to the records that shall be stored in leaf nodes.
90. One of the following tree structures, which is, efficient considering space and time complexities?
a) Incomplete Binary Tree.
b) Complete Binary Tree.
c) Full Binary Tree.
b) Complete Binary Tree.
By the method of elimination:
Full binary tree loses its nature when operations of insertions and deletions are done. For incomplete bina
ry trees,
extra property of complete binary tree is maintained even after operations like additions and deletions are
done on it.
92. Does the minimum spanning tree of a graph give the shortest distance between any 2 specified nodes
?
No.Minimal spanning tree assures that the total weight of the tree is kept at its minimum. But it doesnt me
an that the distance between any two nodes involved in the minimum-spanning tree is minimum.