DS Viva Questions
DS Viva Questions
DS is how data and the relationship amongst different data is represented, that
aids in how efficiently various functions or operations or algorithms can be
applied.
Types
Applications
The B-trees data structures are suitable for the databases implementation.
Some of the most important areas where data structures are used are as
follows:
Artificial intelligence
Compiler design
Machine learning
Blockchain
Cryptography
Any given problem has constraints on how fast the problem should be
solved (time) and how much less resources the problem consumes(space).
That is, a problem is constrained by the space and time complexity within
which it has to be solved efficiently.
Selection of proper data structure becomes the most important step before
applying algorithm to any problem.
The core concepts of data structures remains the same across all the
programming languages. Only the implementation differs based on the
syntax or the structure of the programming language.
The implementation in procedural languages like C is done with the help of
structures, pointers, etc.
Having sound knowledge of the concepts of each and every data structures
helps you to stand apart in any interviews as selecting right data structure
is the first step towards solving problem efficiently.
Viva Questions
1. Can you explain the difference between file structure and storage
structure?
Storage Structure: In this type, data is stored in the main memory i.e RAM,
and is deleted once the function that uses this data gets completely
executed.
The difference is that storage structure has data stored in the memory of
the computer system, whereas file structure has the data stored in the
auxiliary memory.
2. Can you tell how linear data structures differ from non-linear data
structures?
Lists, stacks, and queues are examples of linear data structures whereas
graphs and trees are the examples of non-linear data structures.
3. What is an array?
Multi-dimensional arrays are those data structures that span across more
than one dimension.
This indicates that there will be more than one index variable for every
point of storage. This type of data structure is primarily used in cases
where data cannot be represented or stored using only one dimension.
Most commonly used multidimensional arrays are 2D arrays.
A linked list is a data structure that has sequence of nodes where every
node is connected to the next node by means of a reference pointer. The
elements are not stored in adjacent memory locations. They are linked
using pointers to form a chain. This forms a chain-like link for data storage.
a data field
The first node in a linked list is called the head and the last node in the list
has the pointer to NULL. Null in the reference field indicates that the node
is the last node. When the list is empty, the head is a null reference.
Linked lists can be considered both linear and non-linear data structures.
This depends upon the application that they are used for.
When linked list is used for access strategies, it is considered as a linear
data-structure. When they are used for data storage, it can be considered
as a non-linear data structure.
Linked list is a dynamic data structure that means there is no need to give
an initial size at the time of creation as it can grow and shrink at runtime by
allocating and deallocating memory.
No wastage of memory
As the size of a linked list can grow or shrink based on the needs of the
program, there is no memory wasted because it is allocated in runtime.
8. Explain the scenarios where you can use linked lists and arrays.
Following are the scenarios where we use linked list over array:
Below are the cases where we use arrays over the linked list:
When we need speed while iterating over the elements in the sequence.
Due to the nature of arrays and linked list, it is safe to say that filled arrays
use less memory than linked lists.
Each element in the array indicates just the data whereas each linked list
node represents the data as well as one or more pointers or references to
the other elements in the linked list.
This is a complex type of a linked list wherein a node has two references:
This structure allows traversal of the data elements in both directions (left
to right and vice versa).
A music playlist with next song and previous song navigation options.
The undo and redo functionality on platforms such as word, paint etc,
where you can reverse the node to get to the previous page.
10. What is a stack? What are the applications of stack?
Stack is a linear data structure that follows LIFO (Last In First Out)
approach for accessing elements.
Push, pop, and top (or peek) are the basic operations of a stack.
Reverse a string
A queue is a linear data structure that follows the FIFO (First In First Out)
approach for accessing elements.
Dequeue from the queue, enqueue element to the queue, get front element
of queue, and get rear element of queue are basic operations that can be
performed.
Using concepts like dynamic allocation ensures high efficiency and that
the storage units can be accessed based on requirements in real time.
Here, the oldest element is always at the top of stack1 which ensures
dequeue operation to occur in O(1) time complexity.
Pseudocode:
enqueue(q, data):
deQueue(q):
else
Here, for enqueue operation, the new element is pushed at the top of
stack1. Here, the enqueue operation time complexity is O(1).
Pseudocode:
enqueue(q, data):
dequeue(q):
If stack2 is empty:
Let stack be ‘s’ and queues used to implement be ‘q1’ and ‘q2’. Then, stack
‘s’ can be implemented in two ways:
This method ensures that newly entered element is always at the front of
‘q1’, so that pop operation just dequeues from ‘q1’.
‘q2’ is used as auxillary queue to put every new element at front of ‘q1’
while ensuring pop happens in O(1) complexity.
Pseudocode:
push(s, data):
Enqueue data to q2
pop(s):
Pseudocode:
push(s,data):
Enqueue data to q1
pop(s):
Step1: Dequeue every elements except the last element from q1 and
enqueue to q2.
Step2: Dequeue the last item of q1, the dequeued item is stored in result
variable.
Step3: Swap the names of q1 and q2 (for getting updated data after
dequeue)
The key or value object that gets used in hashmap must implement
equals() and hashcode() method.
The hash code is used when inserting the key object into the map and
equals method is used when trying to retrieve a value from the map.
In the worst case scenario, it can happen that all key might have the same
hashcode, which will result in the hash table turning into a linked list. In
this case, searching a value will take O(n) complexity as opposed to O(1)
time due to the nature of the linked list. Hence, care has to be taken while
selecting hashing algorithm.
19. What is the time complexity of basic operations get() and put() in
HashMap class?
The time complexity is O(1) assuming that the hash function used in hash
map distributes elements uniformly among the buckets.
20. Which data structures are used for implementing LRU cache?
Hashmap – Hashmap stores the page number as the key along with the
address of the corresponding queue node as the value.
A priority queue is an abstract data type that is like a normal queue but has
priority assigned to elements.
Elements with higher priority are processed before the elements with a
lower priority.
In order to implement this, a minimum of two queues are required - one for
the data and the other to store the priority.
22. Can we store a duplicate key in HashMap?
No, duplicate keys cannot be inserted in HashMap. If you try to insert any
entry with an existing key, then the old value would be overridden with the
new value. Doing this will not change the size of HashMap.
This is why the keySet() method returns all keys as a SET in Java since it
doesn't allow duplicates.
The most commonly used tree data structure is a binary tree and its
variants.
Filesystems —files inside folders that are inturn inside other folders.
A binary Tree is a special type of tree where each node can have at most
two children. Binary tree is generally partitioned into three disjoint subsets,
i.e. the root of the tree, left sub-tree and right sub-tree.
Consider that every node of a tree represents a class called Node as given
below:
int data;
Node left;
Node right;
if (node == null)
else
//use the larger among the left and right height and plus 1 (for the
root)
if (root ==null)
return 0;
else
count += countNodes(root.left);
count += countNodes(root.right);
return count;
Tree traversal is a process of visiting all the nodes of a tree. Since root
(head) is the first node and all nodes are connected via edges (or links) we
always start with that node. There are three ways which we use to traverse
a tree −
Inorder Traversal:
Algorithm:
if (root == null)
return;
printInorderTraversal(root.left);
printInorderTraversal(root.right);
Preorder Traversal:
Algorithm:
if (root == null)
return;
printPreorderTraversal(root.left);
printPreorderTraversal(root.right);
Uses:
Postorder Traversal:
Algorithm:
if (root == null)
return;
printPostorderTraversal(root.left);
printPostorderTraversal(root.right);
Uses:
A binary search tree (BST) is a variant of binary tree data structure that
stores data in a very efficient manner such that the values of the nodes in
the left sub-tree are less 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 the left and right sub-trees are their own binary search
trees at all instances of time.
AVL trees are height balancing BST. 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 and is calculates as. BalanceFactor =
height(left subtree) − height(right subtree)
The main idea to solve this problem is to traverse the tree in pre order
manner and pass the level information along with it. If the level is visited
for the first time, then we store the information of the current node and the
current level in the hashmap. Basically, we are getting the left view by
noting the first node of every level.
At the end of traversal, we can get the solution by just traversing the map.
Consider the following tree as example for finding the left view:
Left view of a binary tree in Java:
import java.util.HashMap;
class Node
int data;
Node(int data) {
this.data = data;
if (root == null) {
return;
}
// if you are visiting the level for the first time
if (!map.containsKey(level)) {
map.put(level, root.data);
// traverse the tree and find out the first nodes of each level
leftViewUtil(root, 1, map);
leftView(root);
Transport grids where stations are the nodes and routes are the edges of
the graph.
Power or water utility graphs where vertices are connection points and
edge the wires or pipes connecting them.
35. What is the difference between tree and graph data structure?
Tree and graph are differentiated by the fact that a tree structure must be
connected and can never have loops whereas in the graph there are no
restrictions.
Tree provides insights on relationship between nodes in a hierarchical
manner and graph follows a network model.
36. What is the difference between the Breadth First Search (BFS) and
Depth First Search (DFS)?
BFS and DFS both are the traversing methods for a graph. Graph traversal
is nothing but the process of visiting all the nodes of the graph.
The main difference between BFS and DFS is that BFS traverses level by
level whereas DFS follows first a path from the starting to the end node,
then another path from the start to end, and so on until all nodes are
visited.
Furthermore, BFS uses queue data structure for storing the nodes whereas
DFS uses the stack for traversal of the nodes for implementation.
DFS yields deeper solutions that are not optimal, but it works well when the
solution is dense whereas the solutions of BFS are optimal.
You can learn more about BFS here: Breadth First Search and DFS here:
Depth First Search.
If it is known that the solution is not far from the root of the tree, a breadth
first search (BFS) might be better.
If the tree is very deep and solutions are rare, depth first search (DFS)
might take an extremely long time, but BFS could be faster.
If the tree is very wide, a BFS might need too much memory, so it might be
completely impractical. We go for DFS in such cases.
If solutions are frequent but located deep in the tree we opt for DFS.
Applications:
data serialization
// V - total vertices
void topologicalSort()
visited[j] = false;
// Call the util function starting from all vertices one by one
for (int i = 0; i < V; i++)
if (visited[i] == false)
Stack<Integer> stack)
visited[v] = true;
Integer i;
Iterator<Integer> it = graph.get(v).iterator();
while (it.hasNext()) {
i = it.next();
if (!visited[i])
topologicalSortUtil(i, visited, stack);
stack.push(new Integer(v));
39. Given an m x n 2D grid map of '1’s which represents land and '0’s that
represents water, return the number of islands (surrounded by water and
formed by connecting adjacent lands in 2 directions - vertically or
horizontally). Assume that the boundary cases - which is all four edges of
the grid are surrounded by water.
Constraints are:
m == grid.length
n == grid[i].length
Example:
Input: grid = [
Output: 3
Solution:
class InterviewBit {
if(grid==null || grid.length==0||grid[0].length==0)
return 0;
int m = grid.length;
int n = grid[0].length;
int count=0;
if(grid[i][j]=='1'){
count++;
mergeIslands(grid, i, j);
}
return count;
int m=grid.length;
int n=grid[0].length;
if(i<0||i>=m||j<0||j>=n||grid[i][j]!='1')
return;
grid[i][j]='X';
mergeIslands(grid, i, j-1);
mergeIslands(grid, i, j+1);
In a Max-Heap the data element present at the root node must be greatest
among all the data elements present in the tree.
This property should be recursively true for all sub-trees of that binary tree.
Min-Heap:
In a Min-Heap the data element present at the root node must be the
smallest (or minimum) among all the data elements present in the tree.
This property should be recursively true for all sub-trees of that binary tree.
1) What is data structure?
Data structure refers to the way data is organized and manipulated. It seeks to
find ways to make data access more efficient. When dealing with the data
structure, we not only focus on one piece of data but the different set of data and
how they can relate to one another in an organized manner.
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.
A binary search is an algorithm that is best applied to search a list when the
elements are already in order or sorted. The list is searched starting in the
middle, such that if that middle value is not the target search key, it will check to
see if it will continue the search on the lower half of the list or the higher half. The
split and search will then continue in the same manner.
A linked list is a sequence of nodes in which each node is connected to the node
following it. This forms a chain-like link for data storage.
Skip Ad
5) How do you reference all the elements in a one-dimension array?
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.
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.
7) What is LIFO?
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.
8 ) What is a queue?
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.
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.
10) Which data structures are applied when dealing with a recursive
function?
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 subtree, otherwise, insert it into the
root’s right subtree.
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 subarray 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.
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.
30) What is the minimum number of nodes that a binary tree can have?
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.
31) What are dynamic data structures?
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.
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.
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.
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.
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, merge 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.
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.
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 subtree
6. else, search the right subtree
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.
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.
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.
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
subtrees from the root. This self-balancing tree was known to be the first data
structure to be designed as such.
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.
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.