Data Structure Questions
Data Structure Questions
The aim is to ensure that data can be organised and accessed efficiently. Data organisation
determines how a program performs. Moreover, data dependency and relationships between
two or more datasets play a crucial role in data structures.
While designing code, we need to pay utmost attention to how data is structured because
incorrectly structured or inefficiently stored data can hamper the overall performance of the
code.
● Artificial intelligence
● Database management
● Graphical processing
● Lexical analysis
● Blockchain
● Decision making
● Simulation
It includes data elements that are arranged sequentially or linearly. Also, each element is
connected to its previous or next adjacent element.
● Static Data Structure: It has a fixed memory size, and it’s easier to access data
elements. An example includes Array.
● Dynamic Data Structure: Size is not fixed and can be updated randomly. Examples
include Queue and Stack.
● Array Data Structure: All the elements or values are of the same type.
● Stack Data Structure: It follows LIFO (Last in, first out) order to perform operations,
so the last element to be stored will be the first to be retrieved.
● Queue Data Structure: It follows FIFO (first in, first out) order for operations, so the
item stored first will be the first to be accessed.
● Linked List Data Structure: Elements in this data structure are connected through a
series of nodes, each containing data items and the address of the next node.
Non-linear Data Structure
Here, data elements are not arranged in a sequence, and all elements can’t be traversed in
a single run. Common non-linear data structures are:
● Tree Data Structure: A hierarchical data structure where elements are placed in a
tree-like structure. It contains a central node, structural nodes, and sub-nodes
connected via edges. Tree-based data structures are Binary Tree, Binary Search
Tree, B- Tree, B+ Tree, AVL Tree, and Red-Black Tree.
● Graph Data Structure: Consists of vertices and edges, and a pair of nodes is
connected through edges. It is used to solve complex problems.
Data is intact until deleted manually. Once the function using the data
gets executed, it is deleted from the
memory.
It is one of the most asked data structure interview question for freshers.
5. What is a stack data structure? What are the applications of a
stack?
A stack data structure represents the state of an application at a specific point in time. It
consists of a series of elements arranged in a linear structure like a real physical stack or
pile where you add or removes items from the top.
This linear data structure follows a particular order, LIFO (Last In First Out) and FILO (First
In Last Out), to perform operations.
Elements are arranged in a sequence, so the item added last will be restricted first. A
real-life example is a stack of clothes where the cloth placed on the top, or the last to be
added, will be the first to be picked.
● Reversing a string
● Parenthesis matching
● Syntax parsing
● Backtracking
push():
It inserts a new element at the top of the stack. However, before we insert a value, it is
important to verify if TOP=MAX–1. If the stack is filled, no more insertion is possible, and you
can see an OVERFLOW message if y try to put an element in the already-filled stack.
pop():
Its basic function is to remove an element from the top of the stack. Before you retrieve an
item, verify if TOP=NULL. If the stack is empty, no further deletions are allowed, and you will
get an UNDERFLOW message if you try to remove an element from an empty stack.
peek():
It returns a value of the top-most element in the stack but doesn't remove it from the data
elements collection.
isFull():
To check if the stack has reached its maximum capacity of data insertion.
isEmpty():
size():
Basic multidimensional arrays are 2D and 3D arrays. It’s technically an array of different
arrays. A 2D array, also known as a matrix or a table, consists of rows and columns.
A key is constant and defines the dataset (e.g., colour, gender, price), while a value is
variable and belong to any of the dataset (e.g., red, female/male, 100).
A binary search tree has a specific order to place elements and is known for three
qualities:
● The values of elements of nodes in the left sub-tree are less or equal to that of the
root nodes.
● Moreover, the values of the right root nodes are higher or equal to the root node.
● Both the left and right sub-tree are two individual binary search trees at all points in
time.
Each link is an entry into the linked list, and every entry point, also known as the head,
points to the next node. In an empty list, the head is a null reference, with the last node
consisting of a reference to the null.
The order to add nodes to the list is based on the order in which they are created. A linked
list data structure doesn't have a fixed number of nodes, and the list can grow or shrink as
per demand.
Some of the key applications of a linked list data structure are as follows:
The linked list is a dynamic data structure, so there is no need for an initial size. It grows and
shrinks at runtime through memory allocation and deallocation.
In an array, size is limited because the number of elements is stored statistically in the
memory.
Node insertion and deletion are easier as you just have to update the address present in the
next pointer.
However, it is expensive in an array because you need to create room for new elements by
shifting existing elements.
c) Implementation
Implementing stack and queue data structures using a linked list is easier.
d) Memory Usage
A linked list can be decreased or increased based on the demand of the program, and
memory is allocated only when needed, so there is no memory wastage.
An array cause more memory wastage. For example, if we store five elements in an array
when the size is 10, then the remaining space will be wasted.
● You want to insert an item in the middle of a list, like implementing a priority queue.
● You already know the number of elements in an array, so you can allocate the correct
amount of memory.
● In case memory is a major concern, arrays use less memory than linked lists
because each element in an array is data, whereas each node in a linked list requires
data and one or more pointers to other elements.
In a nutshell, it comes to time, space, and ease of implementation while deciding between a
linked list and an array.
Rows of a 2D array are stored contiguously. The first row is stored in the memory, then the
second row of the array, then the third, and so on until the final row.
b) Column-Major Order
All the columns are stored in the same order. In the beginning, the first column is stored in
the memory, then the second column, and so on, until the last column is saved in the
memory.
So, these are the common data structure interview questions for freshers.
Data Structures and Algorithms (DSA) Interview Questions
16. What is an algorithm?
An algorithm shows a step-by-step process to solve a problem or manipulate data. It is a set
of instructions to be executed in a specific order to get the desired results.
The time complexity determines the amount of time required for an algorithm to run as a
function of input length, and the space complexity determines the amount of space or
memory consumed by an algorithm.
● Quicksort
● Bubble sort
● Radix sort
● Balloon sort
● Merge sort
Among all the options available, putting one on the podium for enhanced and fastest
performance is neither fair nor possible. Each sorting algorithm serves a specific purpose,
and its performance varies according to the data, the way it’s stored, and the reaction once
the algorithm processes the data.
It is used to find the best case (Omega Notation, Ω), worst case (Big Oh Notation, O), and
average case (Theta Notation, θ) performances of an algorithm. It is not a deep learning
method but an essential tool for programmers to analyse the efficiency of an algorithm.
20. What is a queue data structure?
A queue data structure supports systematic operations, i.e., elements are accessed and
manipulated in a specific order. It follows FIFO (First in, First out) method, so elements are
added to the queue one after the other from the rear end and are removed or processed on
the front end.
It is similar to the literal queues in the real world. Unlike stack, it is open at both ends, with
one end being used to insert elements and another one to remove them. Queues are
commonly used when users want to store items for a long period.
21. What is a priority queue? What are the applications for the
priority queue?
A priority queue is a type of abstract data resembling a normal queue, but here, each
element is assigned a priority value. The order of elements in the priority queue determines
their priority.
So, the elements with higher priority are processed first. In case two or more elements have
the same priority, they are processed in the order they appear in the queue.
Be prepared for this type of interview questions in data structures, as these are often asked
to both freshers and experienced professionals.
● Operating system: CPU scheduling, job scheduling operations, and Disk scheduling.
● dequeue() removes an element from the queue. If the queue is empty, you get
UNDERFLOW notification.
● enqueue() adds an element to the queue. If the queue is full, it will be an overflow
condition.
● front is used to find the value of the first element without removing it.
This feature makes deque suitable for various tasks, such as scheduling, keeping track of
inventory, and handling a large amount of data. Moreover, it can be used as a stack and
queue and performs better than stacks and linked lists.
25. What are the different types of deque? What are its
applications?
There are two main types of deque:
Here, insertion is performed at one end, but deletion is performed at both ends.
b) Output Restricted Deque
● As it supports all data structure operations, it can be used as a stack and queue.
● The heap is used to store data elements in Java, while the stack is used for
variables.
● When you use recursion, heap memory size is more, but in the case of stack
memory, it fills up quickly.
● Objects in a heap are visible to all threads, whereas variables are stored as private
memory in the stack and are visible to only the owner.
● It requires the name of the stack and the value of the entity.
● In case the stack is filled but you still use the ‘Push’ command, you’ll get an ‘overflow’
error, indicating that the stack can no longer accommodate another element.
● When you try to pull an element from an empty stack, you see an ‘underflow’ error.
When we talk about adding or removing an element, it refers to the top-most available data.
● Dynamic allocation ensures enhanced efficiency, and one can access units based on
their real-time requirements.
● Implement file systems where every individual node represents a directory or file.
a) Insertion
Although insertion in an AVL tree is the same as in a binary search tree, it may cause a
violation of the property. Hence, the tree must be balanced, for which rotations can be used.
b) Deletion
It is also the same as in the binary search tree. However, deletion can lead to disruption in
the tree’s balance, so rotations are used to balance it.
c) Left Rotation
When inserting a node into the right subtree of the right subtree cause the tree to be
unbalanced, users need to perform a single left rotation.
d) Right Rotation
If the tree is unbalanced because of insertion in the left subtree of the left subtree, it requires
a single right rotation.
e) Left-Right Rotation
It is the extended version of the single rotation and is also known as double rotation. When
node insertion is into the right subtree of the left subtree, a left rotation is followed by a right
rotation to balance the tree.
f) Right-Left Rotation
When node insertion is done into the left subtree of the right subtree, a right rotation is
followed by a left rotation.
● Database applications with fewer insertions and deletions but frequent data lookups.
36. What is the difference between the B tree and the B+ tree?
a) B Tree
It refers to a self-balancing m-way tree, where ‘m’ signifies the tree’s order. It is an extension
of the binary search tree where nodes can have more than two children and one key. The
data is sorted in a B tree with lower values placed on the left subtree while higher values are
placed on the right subtree.
b) B+ Tree
It refers to an advanced self-balanced tree where every path from the root to the leaf is of
the same length. This shows that all the paths occurred at the same level. So, when all leaf
nodes appear on the second level, no other lead node can appear on the third level.
● Minimum degree ‘t’ refers to a B-tree, and its value is determined by the disk block
size.
● Each node must have at least t-1 keys. The root is the only exception to this, which
must contain at least one key.
● The number of children of a node= number of keys in the node plus one.
● Unlike the binary search tree, which grows and shrinks from downward, B-tree grows
and shrinks from the root.
● Keys are sorted in ascending order. A child between k1 and k2 consists of all keys
ranging from k1 to k2.
● Dijkstra algorithm to determine the smallest path between two or more nodes
● Utility graph or water or power with vertices representing connection points and
edges of the wires.
● Operating System, in the Resource Allocation Graph where each process and
resource are represented as vertices
40. What is the difference between the Breadth First Search (BFS)
and Depth First Search (DFS)?
Need to walk through all the nodes Begins at the root node and then
on the same level to go to the next proceeds as far as possible
level in BFS. through all the nodes to reach the
node with no unvisited nearby
nodes.
Based on the FIFO principle (First In Based on the LIFO principle (Last
First Out). In First Out).
a) General Tree
A generic tree with no constrained hierarchy. Here, nodes can have an endless number of
offspring and the rest of the trees are subsets of the tree.
b) Binary Tree
It is the most commonly-known tree. Here, each parent has a minimum of two offspring,
which are called the left and right youngsters. When a binary tree has certain features and
limitations, other trees, such as RBT, AVL, and binary search trees, are used.
Also known as BST, it is an extension of a binary tree with various optional constraints. The
left child value of a node should be less than or equal to the parent value, and the correct
child value should be higher than or equal to the parent value.
It is an auto-balancing tree with red or black nodes. Hence, the name Red-Black Tree. It
keeps the forest balanced. The tree might not be balanced, but the searching process takes
O(log n) time.
f) N-ary Tree
N represents the maximum number of children. In this tree, every node’s children are either
0 or N.
● Hashing
● Linear search allows you to eliminate only one element per comparison each time
you fail to find the value you want. However, a binary search allows you to eliminate
half the data set with each comparison.
● The binary search runs in O(log n) time while the linear search runs in O(n) time. So,
the more elements in the search array, the faster the binary search is.
● Max-Heap: The value of the root node must be greatest from all other keys of its
children node. The same is recursively true for all sub-trees in that binary tree.
● Min-Heap: The value of the key at the root node must be the smallest among all
keys of the children node. The same stands recursively true for all other sub-trees in
that binary tree.
Each node is either red or black, and by comparing these colours on a path from root to leaf,
the tree ensures that the path is not more than twice as long as others, making it balanced. It
can be used to store any kind of data represented as a set of values.
Although it is similar to a binary tree, there is one difference. The read-black tree is faster to
access, making it suitable for storing a large amount of data.
48. What is a Trie data structure?
Trie stands for ‘Retrieval’ and is also known as the prefix tree or digital tree. It stores a set of
strings in the form of a sorted tree. Let’s say strings range from letters ‘a to z’ of the English
alphabet, so each trie node can’t have more than 26 points.
Also, every individual node has the same number of pointers as alphabet characters. This
data structure can check the work in the dictionary by its prefix. Moreover, it allows you to
print all words alphabetically, which is an advantage over hashing. It ensures efficient prefix
search or auto-complete.
The key to which each node is connected is based on its position in the Trie data structure.
You can add or find strings in O(L) time, where L = Length of a single word. This is a faster
way than BST and hashing, considering the way it is implemented. You don’t need to
compute a hash function or manage collisions like in open addressing or separate chaining.
● Building Tree: To create a data structure and initialise the segment tree variable.
● Updating the Tree: To change the tree by updating the value of an array at a certain
point over an interval.
● Computation geometry
● HTML Course
● React Js Course
● Javascript Course
● PHP Curse
● Power BI Course
● Tableau Course
● Python Course