Basic Data Structure Interview Questions for Freshers
Basic Data Structure Interview Questions for Freshers
A data structure is a mechanical or logical way that data is organized within a program.
The organization of data is what determines how a program performs.
Data structures serve a number of important functions in a program. They ensure that
each line of code performs its function correctly and efficiently, they help the programmer
identify and fix problems with his/her code, and they help to create a clear and organized
code base.
Decision Making
Genetics
Image Processing
Blockchain
Numerical and Statistical Analysis
Compiler Design
Database Design and many more
Linear Data Structure: A data structure that includes data elements arranged
sequentially or linearly, where each element is connected to its previous and next
nearest elements, is referred to as a linear data structure. Arrays and linked lists are
two examples of linear data structures.
Non-Linear Data Structure: Non-linear data structures are data structures in which
data elements are not arranged linearly or sequentially. We cannot walk through all
Page 1 of 9
elements in one pass in a non-linear data structure. Trees and graphs are two
examples of non-linear data structures.
A queue is a linear data structure that allows users to store items in a list in a systematic
manner. The items are added to the queue at the rear end until they are full, at which
point they are removed from the queue from the front.
An array data structure is a data structure that is used to store data in a way that is
efficient and easy to access. It is a collection of similar types of data items stored at
contiguous memory locations. It is similar to a list in that it stores data in a sequence.
However, an array data structure differs from a list in that it can hold much more data
than a list can.
9. What is a linked list data structure? What are the applications for the Linked list?
A linked list can be thought of as a series of linked nodes (or items) that are connected by
links (or paths). Each link represents an entry into the linked list, and each entry points to
the next node in the sequence.
Page 2 of 9
10. Elaborate on different types of Linked List data structures?
a. Singly Linked List: A singly linked list is a data structure that is used to store multiple
items. The items are linked together using the key. The key is used to identify the item and
is usually a unique identifier. In a singly linked list, each item is stored in a separate node.
The node can be a single object or it can be a collection of objects. When an item is added
to the list, the node is updated and the new item is added to the end of the list. When an
item is removed from the list, the node that contains the removed item is deleted and its
place is taken by another node. The key of a singly linked list can be any type of data
structure that can be used to identify an object. For example, it could be an integer, a
string, or even another singly linked list. Singly-linked lists are useful for storing many
different types of data. For example, they are commonly used to store lists of items such
as grocery lists or patient records. They are also useful for storing data that is time
sensitive such as stock market prices or flight schedules.
b. Doubly Linked List: A doubly linked list is a data structure that allows for two-way data
access such that each node in the list points to the next node in the list and also points
back to its previous node. In a doubly linked list, each node can be accessed by its address,
and the contents of the node can be accessed by its index. It's ideal for applications that
need to access large amounts of data in a fast manner. A disadvantage of a doubly linked
list is that it is more difficult to maintain than a single-linked list. In addition, it is more
difficult to add and remove nodes than in a single-linked list.
c. Circular Linked List: A circular linked list is a unidirectional linked list where each node
points to its next node and the last node points back to the first node, which makes it
circular.
d. Doubly Circular Linked List: A doubly circular linked list is a linked list where each node
points to its next node and its previous node and the last node points back to the first
node and first node’s previous points to the last node.
Page 3 of 9
Arrays Linked Lists
Memory utilization is ineffective in Memory utilization is effective in the case of an
the case of an array. array.
Operations like insertion and deletion Operations like insertion and deletion are faster in
take longer time in an array. the linked list
12. What is binary tree data structure? What are the applications for binary trees?
A binary tree is a data structure that is used to organize data in a way that allows for
efficient retrieval and manipulation. It is a data structure that uses two nodes, called
leaves and nodes, to represent the data. The leaves represent the data and the nodes
represent the relationships between the leaves. Each node has two children, called
siblings, and each child has one parent. The parent is the node that is closest to the root of
the tree. When a node is deleted from the tree, it is deleted from both its child and its
parent.
All elements in the left subtree of a node should have a value less than or equal to
the parent node's value, and
All elements in the right subtree of a node should have a value greater than or
equal to the parent node's value.
Both the left and right subtrees must be binary search trees too.
Breadth First Search (BFS) Depth First Search (DFS)
It stands for “Breadth First Search” It stands for “Depth First Search”
BFS (Breadth First Search) finds the shortest DFS (Depth First Search) finds the shortest path
path using the Queue data structure. using the Stack data structure.
When compared to DFS, BFS is slower. When compared to BFS, DFS is faster.
BFS performs better when the target is DFS performs better when the target is far
close to the source. from the source.
It is based on the FIFO principle (First In It is based on the LIFO principle (Last In First
Page 4 of 9
Breadth First Search (BFS) Depth First Search (DFS)
13. What is a deque data structure and its types? What are the applications for deque?
A deque can be thought of as an array of items, but with one important difference:
Instead of pushing and popping items off the end to make room, deques are designed to
allow items to be inserted at either end. This property makes deques well-suited for
performing tasks such as keeping track of inventory, scheduling tasks, or handling large
amounts of data.
14. What are the advantages of a linked list over an array? In which scenarios do we use
Linked List and when Array?
Insertion and deletion of nodes is an easier process, as we only update the address present
in the next pointer of a node. It’s expensive to do the same in an array as the room has to be
created for the new elements and existing elements must be shifted.
As a linked list is a dynamic data structure, there is no need to give an initial size as it can
grow and shrink at runtime by allocating and deallocating memory. However, the size is
limited in an array as the number of elements is statically stored in the main memory.
3. No Wastage of Memory
As the size of a linked list can increase or decrease depending on the demands of the
program, and memory 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 for five elements is wasted.
4. Implementation
Data structures like stack and queues are more easily implemented using a linked list than
an array.
Page 5 of 9
When we know the upper limit on the number of elements in advance
When we want to insert items in the middle of the list, such as when
implementing a priority queue
Some scenarios in which we use array over the linked list are:
When we need speed when iterating through all the elements in the sequence
When memory is a concern; filled arrays use less memory than linked lists, as
each element in the array is the data but each linked list node requires the data
as well as one or more pointers to the other elements in the linked list
A single sorting algorithm can’t be considered best, as each algorithm is designed for a
particular data structure and data set. However, the QuickSort algorithm is generally
considered the fastest because it has the best performance for most inputs.
Cache-efficient: It linearly scans and linearly partitions the input. This means we
can make the most of every cache load.
Can skip some swaps: As QuickSort is slightly sensitive to input that is in the right
order, it can skip some swaps.
Page 6 of 9
When speed takes priority over stability.
This is one of the most frequently asked data structure interview questions. Selection sort
works by repeatedly picking the smallest number in ascending order from the list and
placing it at the beginning. This process is repeated moving toward the end of the list or
sorted subarray.
Scan all items and find the smallest. Switch over the position as the first item. Repeat the
selection sort on the remaining N-1 items. We always iterate forward (i from 0 to N-1) and
swap with the smallest element (always i).
17. 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 looking 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 search’s O(n) time. This
means that the more of the elements present in the search array, the faster is
binary search compared to a linear search
Page 7 of 9
only be inserted at front end and the value of front might be so high so that, all the
space before that, can never be filled.
o Array Size: There might be situations in which, we may need to extend the queue to
insert more elements if we use an array to implement queue, It will almost be
impossible to extend the array size, therefore deciding the correct array size is
always a problem in array implementation of queue.
S B Tree B+ Tree
N
1 Search keys cannot repeatedly be stored. Redundant search keys can be present.
2 Data can be stored in leaf nodes as well as Data can only be stored on the leaf nodes.
internal nodes
3 Searching for some data is a slower process since Searching is comparatively faster as data can
data can be found on internal nodes as well as only be found on the leaf nodes.
on the leaf nodes.
4 Deletion of internal nodes is so complicated and Deletion will never be a complexed process
time-consuming. since element will always be deleted from
the leaf nodes.
5 Leaf nodes cannot be linked together. Leaf nodes are linked together to make the
search operations more efficient.
21) Which data structures are used in BFS and DFS algorithm?
o In BFS algorithm, Queue data structure is used.
o In DFS algorithm, Stack data structure is used.
You can find the height of a binary tree using a recursive Depth-First Search (DFS) algorithm,
as shown below:
Page 8 of 9
Page 9 of 9