DSA Interview Questions & Answers PDF
DSA Interview Questions & Answers PDF
DSA
Interview Questions
w i t h a n s w e r s
Basic Level
Linear Data Structure: A data structure is called linear if all of its elements are
arranged in the sequential order. In linear data structures, the elements are
stored in a non-hierarchical way where each item has the successors and
predecessors except the first and last element.
Non-Linear Data Structure: The Non-linear data structure does not form a
sequence i.e. each item or element is connected with two or more other
items in a non-linear arrangement. The data elements are not arranged in
the sequential structure.
·Using concepts like dynamic allocation ensures high efficiency and that
the storage units can be accessed based on requirements in real-time.
Page no: 2
Get free mentorship
from experts?
of Aptitude and Technical skills C++, Java, Python, DSA, CN, OS, DBMS,
After knowing the current level it will Quant, Reasoning, and verbal ability.
become easy for a student to start their Topic-wise questions in every test will help
placement preparation students get strong and weak points
Page no: 4
Join WhatsApp Group for
Placement
11. What is a linked list data structure? What are the applications
for the Linked list?
Answer:
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. The order in
which nodes are added to the list is determined by the order in which they
are created.
Following are some applications of linked list data structure:
Stack, Queue, binary trees, and graphs are implemented using linked
lists.
Dynamic management for Operating System memory.
Round robin scheduling for operating system tasks.
Forward and backward operation in the browser.
Page no: 5
Get free mentorship
from experts?
13. How are the elements of a 2D array are stored in the memory?
Answer:
There are two techniques by using which, the elements of a 2D array can be
stored in the memory.
Row-Major Order: In row-major ordering, all the rows of the 2D array are
stored into the memory contiguously. First, the 1st row of the array is stored
into the memory completely, then the 2nd row of the array is stored into the
memory completely and so on till the last row.
Column-Major Order: In column-major ordering, all the columns of the 2D
array are stored into the memory contiguously. first, the 1st column of the
array is stored into the memory completely, then the 2nd row of the array is
stored into the memory completely and so on till the last column of the array.
Page no: 6
Follow us on Instagram
The size of a linked list can be incremented at runtime which is impossible in the
case of the array.
The List is not required to be contiguously present in the main memory, if the
contiguous space is not available, the nodes can be stored anywhere in the
memory connected through the links.
The List is dynamically stored in the main memory and grows as per the program
demand while the array is statically stored in the main memory, size of which
must be declared at compile time.
The number of elements in the linked list are limited to the available memory
space while the number of elements in the array is limited to the size of an array.
Page no: 7
Join WhatsApp Group for
Placement
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.
Intermediate Level
Page no: 8
Get free mentorship
from experts?
Queues are widely used as waiting lists for a single shared resource like
a printer, disk, CPU.
Queues are used in the asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for eg.
pipes, file IO, sockets.
Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
Queues are used to maintain the playlist in media players to add and
remove the songs from the play-list.
Queues are used in operating systems for handling interrupts.
26. How can AVL Tree be useful in all the operations as compared to
Binary search tree?
Answer:
AVL tree controls the height of the binary search tree by not letting it be skewed.
The time taken for all operations in a binary search tree of height h is O(h).
However, it can be extended to O(n) if the BST becomes skewed (i.e. worst case). By
limiting this height to log n, AVL tree imposes an upper bound on each operation
to be O(log n) where n is the number of nodes.
Page no: 10
Join WhatsApp Group for
Placement
Page no: 11
Get free mentorship
from experts?
32. What are the advantages of Binary search over linear search?
Answer:
There are relatively less number of comparisons in binary search than that in
linear search. In average case, linear search takes O(n) time to search a list of n
elements while Binary search takes O(log n) time to search a list of n elements.
Place
d in A Rohit Borse
ccent
ure
Shrinija
Kalluri 6.5 LP
Placed in
Oracle A
9 LPA
Page no: 12
Follow us on Instagram
Stack Queue
Only one pointer is available for both Two pointers are available for
addition and deletion: addition and deletion: front() and
top() rear()
Page no: 13
Join WhatsApp Group for
Placement
Answer:
Following are different types of linked lists:
1. 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.
Page no: 14
Get free mentorship
from experts?
2. 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.
3. 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.
4. 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.
5. Header List: A list that contains the header node at the beginning of the list, is
called the header-linked list. This is helpful in calculating some repetitive
operations like the number of elements in the list etc.
The memory size of an array is fixed The memory size of a linked list is
and cannot be changed during allocated during runtime.
runtime.
It is easier and faster to access an In the linked list, it takes time to access
element in an array. an element.
Operations like insertion and deletion Operations like insertion and deletion
take longer time in an array. are faster in the linked list.
Page no: 16
Join WhatsApp Group for
Placement
Step 1: First, read the search element (Target element) in the array.
Step 2: In the second step compare the search element with the first element in
the array.
Step 3: If both are matched, display “Target element is found” and terminate the
Linear Search function.
Step 4: If both are not matched, compare the search element with the next
element in the array.
Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is
compared with the last element of the array.
Step 6 – If the last element in the list does not match, the Linear Search Function
will be terminated, and the message “Element is not found” will be displayed.
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Page no: 17
Get free mentorship
from experts?
The average and worst-case complexity of selection sort is O(n2), where n is the
number of items. Due to this, it is not suitable for large data sets.
Algorithm
SELECTION SORT(arr, n)
Step 1: Repeat Steps 2 and 3 for i = 0 to n-1
Step 2: CALL SMALLEST(arr, i, n, pos)
Step 3: SWAP arr[i] with arr[pos]
[END OF LOOP]
Step 4: EXIT
SMALLEST (arr, i, n, pos)
Step 1: [INITIALIZE] SET SMALL = arr[i]
Step 2: [INITIALIZE] SET pos = i
Step 3: Repeat for j = i+1 to n
if (SMALL > arr[j])
SET SMALL = arr[j]
SET pos = j
[END OF if]
[END OF LOOP]
Step 4: RETURN pos
Insertion Sort
In Insertion Sort, the values are inserted in a list/array wherein some of the
values are previous sorted.
It is a stable sorting algorithm.
The best-case time complexity is O(N) (when the list has been sorted in
ascending order).
A key value is defined, and the array is iterated from the beginning to the
end.
The current element during iteration is compared to the key value.
The number of comparison operations made during insertion sort is less
than the number of the element swapping that is done.
If the key element is smaller than the element with which it is being
compared, the previous elements are compared with it.
The elements greater than key are moved one position up to make space
for the element that is swapped.
The elements are known before hand, only their position is determined
during insertion sort.
Selection sort
In Selection Sort, at first, the minimum or the maximum number from the
list is obtained.
The list is sorted in ascending or descending order.
It is considered as an unstable sorting algorithm.
The time complexity in all cases is O(n^2).
It is less efficient in comparison to insertion sort.
The number of comparisons made during iterations is more than the
element swapping that is done.
The location of every element in the list is previously known.
This means the user only searches for the element that needs to be
inserted at the specific position.
Page no: 19
Join WhatsApp Group for
Placement
Talent Battle is
associated with TCS iON
for content partnership &
providing internships to
students across India.
Page no: 20
Get free mentorship
from experts?
Advanced Level
It stands for “Breadth First Search” It stands for “Depth First Search”
BFS (Breadth First Search) finds the DFS (Depth First Search) finds
shortest path using the Queue data the shortest path using the
structure. Stack data structure.
We walk through all nodes on the DFS begins at the root node and
same level before passing to the proceeds as far as possible
next level in BFS. through the nodes until we
reach the node with no
unvisited nearby nodes.
Page no: 21
Get free mentorship
from experts?
BFS performs better when the DFS performs better when the
target is close to the source. target is far from the source.
Nodes that have been traversed When there are no more nodes
multiple times are removed from to visit, the visited nodes are
the queue. added to the stack and then
removed.
Page no: 22
Get free mentorship
from experts?
The number of children of a node is equal to its key count plus one.
A node's keys are sorted in ascending order. The child of two keys k1 and
k2 contains all keys between k1 and k2.
In contrast to Binary Search Tree, B-Tree grows and shrinks from the root.
Following are key operations performed on the Segment tree data structure:
Building Tree: In this step, we create the structure and initialize the
segment tree variable.
Updating the Tree: In this step, we change the tree by updating the array
value at a point or over an interval.
Querying Tree: This operation can be used to run a range query on the
array.
Following are real-time applications for Segment Tree:
Page no: 23
Get free mentorship
from experts?
Trie is also referred to as the digital tree or the prefix tree. The key to which a
node is connected is determined by its position in the Trie. Trie allows us to
insert and find strings in O(L) time, where L is the length of a single word. This
is clearly faster than BST. Because of how it is implemented, this is also faster
than Hashing. There is no need to compute a hash function. There is no need
to handle collisions (like we do in open addressing and separate chaining)
Another benefit of Trie is that we can easily print all words in alphabetical
order, which is not easy with hashing. Trie can also perform prefix search (or
auto-complete) efficiently.
The main disadvantage of tries is that they require a large amount of memory
to store the strings. We have an excessive number of node pointers for each
node
Following are some real-time applications for Trie data structure:
A red-black tree is a Binary tree in which each node has a colour attribute,
either red or black. By comparing the node colours on any simple path from
the root to a leaf, red-black trees ensure that no path is more than twice as
long as any other, ensuring that the tree is generally balanced.
Page no: 24
Get free mentorship
from experts?
Red-black trees are similar to binary trees in that they both store their data in
two's complementary binary formats. However, red-black trees have one
important advantage over binary trees: they are faster to access. Because red-
black trees are so fast to access, they are often used to store large amounts of
data.
Red-black trees can be used to store any type of data that can be represented
as a set of values.
Following are some real-time applications for the Red-Black Tree data
structure:
The majority of self-balancing BST library functions in C++ or Java use Red-
Black Trees.
It is used to implement Linux CPU Scheduling.
It is also used to reduce time complexity in the K-mean clustering
algorithm in machine learning.
MySQL also employs the Red-Black tree for table indexes in order to reduce
searching and insertion time.
51. Which data structures are used for implementing LRU cache?
Answer:
LRU cache or Least Recently Used cache allows quick identification of an
element that hasn’t been put to use for the longest time by organizing items in
order of use. In order to achieve this, two data structures are used:
Hashmap – Hashmap stores the page number as the key along with the
address of the corresponding queue node as the value.
Page no: 25
Get free mentorship
from experts?
Max-Heap:
In a Max-Heap the data element present at the root node must be the 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.
SN B Tree B+ Tree
1 Search keys cannot repeatedly be Redundant search keys can be
stored. present.
2 Data can be stored in leaf nodes as Data can only be stored on the leaf
well as internal nodes nodes.
5 Leaf nodes cannot be linked together. Leaf nodes are linked together to
make the search operations more
efficient.
Page no: 26
Get free mentorship
from experts?
Cycle: A Cycle can be defined as the closed path where the initial vertex is
identical to the end vertex. Any vertex in the path cannot be visited twice
Circuit: A Circuit can be defined as the closed path where the intial vertex is
identical to the end vertex. Any vertex may be repeated.
57. Which data structures are used in BFS and DFS algorithm?
Answer:
In BFS algorithm, Queue data structure is used.
Page no: 27
Get free mentorship
from experts?
Page no: 28
Get free mentorship
from experts?
62. Name the ways to determine whether a linked list has a loop.
Answer:
Using hashing
Using the visited nodes method (with or without modifying the basic linked
list data structure)
Floyd’s cycle-finding algorithm
Page no: 29
Get free mentorship
from experts?
67. What are the time complexities of linear search and binary search?
Answer:
Binary search is more effective as it takes lesser comparisons to search for an
element in an array. The time complexity for linear search is O(n), while it is O(log
n) for binary search.
Page no: 30
Get free mentorship
from experts?
Page no: 31
Get free mentorship
from experts?
75. Explain the Dijkstra's Algorithm to find the shortest path between a
given node in a graph to any other node in the graph.
Answer:
Let us call the node where we are starting the process as the initial node. Let the
distance from the initial node to Y be the distance of node Y. Dijkstra's algorithm
will begin with unlimited distances and attempt to improve them incrementally.
Page no: 32
Get free mentorship
from experts?
Step 1: Mark all nodes that have not been visited yet. The unvisited set is a
collection of all the nodes that have not been visited yet.
Step 2: Assign a tentative distance value to each node: set it to zero for our first
node and infinity for all others. The length of the shortest path discovered so far
between the node v and the initial node is the tentative distance of a node v.
Because no other vertex other than the source (which is a path of length zero) is
known at the start, all other tentative distances are set to infinity. Set the current
node to the beginning node.
Step 3: Consider all of the current node's unvisited neighbours and determine
their approximate distances through the current node. Compare the newly
calculated tentative distance to the current assigned value and choose the one
that is less. If the present node A has a distance of 5 and the edge linking it to a
neighbour B has a length of 3, the distance to B through A will be 5 +3 = 8.
Change B to 8 if it was previously marked with a distance greater than 8. If this is
not the case, the current value will be retained.
Step 4: Mark the current node as visited and remove it from the unvisited set
once we have considered all of the current node's unvisited neighbours. A node
that has been visited will never be checked again.
Stop if the destination node has been marked visited (when planning a route
between two specific nodes) or if the smallest tentative distance between the
nodes in the unvisited set is infinity (when planning a complete traversal; occurs
when there is no connection between the initial node and the remaining
unvisited nodes). The algorithm is now complete.
Step 5: Otherwise, return to step 3 and select the unvisited node indicated with
the shortest tentative distance as the new current node.
It is not required to wait until the target node is "visited" as described above
while constructing a route: the algorithm can end once the destination node has
the least tentative distance among all "unvisited" nodes (and thus could be
selected as the next "current"). For arbitrary directed graphs with unbounded
non-negative weights, Dijkstra's algorithm is asymptotically the fastest known
single-source shortest path algorithm with time complexity of O(|E| + |V|log(|V|)),
where |V| is the number of nodes and|E| is the number of edges in the graph.
Page no: 33
Get free mentorship
from experts?
Algorithm
Page no: 34
Get free mentorship
from experts?
Algorithm:
Page no: 35
Get free mentorship
from experts?
Step 1: Check if the linked list has no value (or is empty). If yes, then set the
new node as the head and return it.
Step 2: Check if the value of the node to be inserted is smaller than the value
of the head node. If yes, place it at the beginning and make it the head node.
Step 3: Find the suitable node after which the input node should be added in
a loop. To discover the required node, begin at the head and work your way
forward until you reach a node whose value exceeds the input node. The
preceding node is the correct node.
Step 4: After the correct node is found in step 3, insert the node.
Page no: 36
Introducing...
Complete Placement
Preparatory Masterclass
What is included?
400+ Hours Foundation of LIVE + Recorded
Training on Quant, Reasoning, Verbal, C, C++,
Java, Python, DSA, OS, CN, DBMS
97.6% 4.91 / 5
Selection Ratio Overall Rating
of Complete Masterclass Students out of 5
JOIN NOW
Our Team
Industry Mentors
@talentbattle.in
@talentbattle_2023
@talentbattle_2024
@talentbattle_2025
@talentbattle_2026
WhatsApp Group
Free Mentorship
Talent Battle Facebook
Talent Battle YouTube
Talent Battle LinkedIn
https://talentbattle.in/