100 Data Structure Interview QnA
100 Data Structure Interview QnA
Interview QnA
Made by Want More
A data structure is a way of organizing and storing data in a computer so that it can be
accessed and manipulated efficiently.
The main types of data structures are arrays, linked lists, stacks, queues, trees, graphs,
and hash tables.
3. What is an array?
A linked list is a data structure where each element (node) contains a value and a
pointer to the next element.
5. What is a stack?
A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where
elements are added and removed from the top.
6. What is a queue?
A queue is a data structure that follows the First-In-First-Out (FIFO) principle, where
elements are added at the rear and removed from the front.
7. What is a tree?
A tree is a hierarchical data structure composed of nodes, where each node can have
zero or more child nodes.
A binary tree is a tree in which each node has at most two child nodes, referred to as
the left child and the right child.
9. What is a graph?
A graph is a collection of nodes (vertices) and edges, where edges represent
relationships between nodes.
A binary tree is a tree in which each node has at most two child nodes, referred to as
the left child and the right child.
9. What is a graph?
A hash table is a data structure that uses a hash function to map keys to values,
allowing for efficient retrieval and insertion.
An array has a fixed size, requires contiguous memory, and allows for random access,
whereas a linked list can dynamically grow and doesn't require contiguous memory
but doesn't support random access.
The time complexity of searching an element in a linked list is O(n) (linear time).
A doubly linked list is a linked list in which each node contains two pointers, one
pointing to the previous node and one pointing to the next node.
A circular linked list is a linked list where the last node points back to the first node,
forming a loop.
The main operations on a stack are push (to add an element), pop (to remove the top
element), and peek (to view the top element without removing it).
The main operations on a queue are enqueue (to add an element), dequeue (to remove
the front element), and peek (to view the front element without removing it).
An AVL tree is a self-balancing binary search tree in which the heights of the left and
right subtrees differ by at most one.
A hash function is a function that takes an input (key) and produces a fixed-size value
(hash code), which is used to index the data in a hash table.
Collision handling is the process of dealing with situations where two different keys
map to the same hash code. Common techniques include chaining (using linked lists)
and open addressing.
22. What is the time complexity of searching in a balanced binary search tree?
The time complexity of searching in a balanced binary search tree, such as an AVL tree,
is O(log n) (logarithmic time).
Breadth-first search is a graph traversal algorithm that explores all the vertices of a
graph in breadth-first order, visiting all the neighbors of a node before moving to the
next level.
Depth-first search is a graph traversal algorithm that explores as far as possible along
each branch before backtracking, visiting all the descendants of a node before moving
to the next sibling.
A heap is a complete binary tree that satisfies the heap property, which means that
each parent node is either greater than or equal to (in a max heap) or less than or
equal to (in a min heap) its children.
26. What is the difference between a binary tree and a binary search tree?
A binary tree is a tree in which each node can have at most two child nodes, whereas a
binary search tree is a binary tree that satisfies the property that the left child of a
node is less than or equal to the node, and the right child is greater than or equal to
the node.
28. What is the time complexity of removing the root element from a heap?
The time complexity of removing the root element from a heap is O(log n) (logarithmic
time).
A trie, also known as a prefix tree, is a tree-like data structure that stores a collection of
strings, where each node represents a common prefix.
The time complexity of searching in a trie is O(m), where m is the length of the search
key.
A self-balancing binary search tree is a binary search tree that automatically adjusts its
structure to maintain a balance, ensuring efficient search, insertion, and deletion
operations.
A B-tree is a self-balancing search tree that can have multiple keys and children per
node, designed to reduce the number of disk accesses required for large datasets.
A red-black tree is a self-balancing binary search tree in which each node is colored red
or black, and a set of properties are maintained to ensure balance.
A stack follows the Last-In-First-Out (LIFO) principle, while a queue follows the First-In-
First-Out (FIFO) principle.
The average time complexity of searching in a hash table is O(1) (constant time), but it
can be O(n) in the worst case if there are many collisions.
41. What is the difference between a singly linked list and a doubly linked list?
In a singly linked list, each node contains a pointer to the next node, while in a doubly
linked list, each node contains pointers to both the next and previous nodes.
A circular queue is a queue implemented in a circular manner, where the last element
points back to the first element, allowing for efficient memory utilization.
A priority queue is an abstract data type that supports inserting elements with
associated priorities and retrieving the element with the highest (or lowest) priority.
44. What is the time complexity of inserting an element into a priority queue?
The time complexity of inserting an element into a priority queue depends on the
implementation. In a binary heap-based priority queue, the time complexity is O(log n)
(logarithmic time).
A self-balancing binary search tree is a binary search tree that automatically adjusts its
structure to maintain balance, ensuring efficient search, insertion, and deletion
operations.
A stack is a region of memory used for automatic storage of variables and function call
information, while a heap is a region of memory used for dynamic memory allocation.
47. What is the time complexity of searching in a self-balancing binary search tree?
The time complexity of searching in a self-balancing binary search tree, such as an AVL
tree or red-black tree, is O(log n) (logarithmic time).
A circular linked list is a linked list in which the last node points back to the first node,
forming a loop.
49. What is the time complexity of inserting an element into a linked list?
The time complexity of inserting an element into a linked list at a specific position is
O(1) (constant time) if the position is known, or O(n) (linear time) if the position needs
to be searched.
A skip list is a data structure that allows for efficient search, insertion, and deletion
operations, resembling a linked list with multiple parallel layers.
A disjoint set data structure is a data structure that keeps track of a collection of
disjoint (non-overlapping) sets and supports efficient union and find operations.
53. What is the time complexity of finding the root of a disjoint set?
The time complexity of finding the root of a disjoint set using the find operation is
typically O(α(n)), where α is the inverse Ackermann function (a very slowly growing
function).
A suffix tree is a data structure that represents all the suffixes of a given string in a
compressed form, enabling efficient pattern matching and substring search
operations.
The time complexity of building a suffix tree for a string of length n is O(n).
A bit array, also known as a bit set or bit vector, is a data structure that represents an
array of bits, where each bit can be set (1) or cleared (0), allowing for efficient storage
and manipulation of binary data.
57. What is the time complexity of inserting an element into a bit array?
The time complexity of inserting an element into a bit array depends on the specific
operation being performed.
59. What is the time complexity of inserting an element into a bloom filter?
The time complexity of inserting an element into a bloom filter is O(k), where k is the
number of hash functions used.
A Fibonacci heap is a data structure that supports efficient insertion, deletion, and
extraction of the minimum element, making it suitable for certain graph algorithms.
61. What is the time complexity of extracting the minimum element from a Fibonacci
heap?
The time complexity of extracting the minimum element from a Fibonacci heap is
O(log n) amortized time, where n is the number of elements in the heap.
63. What is the time complexity of finding the root of a disjoint-set forest?
The time complexity of finding the root of a disjoint-set forest using the find operation
with path compression is nearly O(1) (constant time).
A van Emde Boas tree is a tree-like data structure that provides efficient operations for
a universe of size M, achieving a time complexity of O(log log M).
65. What is the time complexity of searching in a van Emde Boas tree?
The time complexity of searching in a van Emde Boas tree is O(log log M), where M is
the size of the universe.
A Fenwick tree, also known as a Binary Indexed Tree (BIT), is a data structure that
allows efficient prefix sum calculations and single element updates in an array.
67. What is the time complexity of calculating a prefix sum using a Fenwick tree?
The time complexity of calculating a prefix sum using a Fenwick tree is O(log n), where
n is the size of the array.
A range tree is a tree-like data structure that allows efficient range queries, such as
finding all elements within a given range.
The time complexity of searching in a range tree is O(log n + k), where n is the number
of elements and k is the number of elements within the range.
70. What is a segment tree?
A segment tree is a tree-like data structure that allows efficient range queries and
updates on an array.
The time complexity of searching in a segment tree is O(log n + k), where n is the size
of the array and k is the number of elements within the range.
A trie, also known as a prefix tree, is a tree-like data structure that stores a collection of
strings, where each node represents a common prefix.
The time complexity of searching in a trie is O(m), where m is the length of the search
key.
The time complexity of searching in a balanced kd-tree is typically O(log n), where n is
the number of points.
77. What is the time complexity of inserting an element into a bloom filter?
The time complexity of inserting an element into a bloom filter is O(k), where k is the
number of hash functions used.
A trie, also known as a prefix tree, is a tree-like data structure that stores a collection of
strings, where each node represents a common prefix.
The time complexity of searching in a trie is O(m), where m is the length of the search
key.
An interval tree is a tree-like data structure used to efficiently store and search for
intervals, enabling range-based queries.
The time complexity of searching in an interval tree is typically O(log n + k), where n is
the number of intervals and k is the number of intervals intersecting the search range.
A radix tree, also known as a Patricia trie or compact prefix tree, is a compressed trie
data structure used for efficient storage and retrieval of strings with common prefixes.
The time complexity of searching in a radix tree is O(m), where m is the length of the
search key.
A heap is a complete binary tree that satisfies the heap property, which means that
each parent node is either greater than or equal to (in a max heap) or less than or
equal to (in a min heap) its children.
The time complexity of inserting an element into a heap is O(log n) (logarithmic time).
A monotonic stack is a stack data structure where the elements are either in non-
increasing or non-decreasing order, allowing for efficient computations on certain
types of sequences.
87. What is the time complexity of finding the next greater element using a
monotonic stack?
The time complexity of finding the next greater element using a monotonic stack is
O(n), where n is the number of elements in the sequence.
A trie, also known as a prefix tree, is a tree-like data structure that stores a collection of
strings, where each node represents a common prefix.
The time complexity of searching in a trie is O(m), where m is the length of the search
key.
A skip list is a data structure that allows for efficient search, insertion, and deletion
operations, resembling a linked list with multiple parallel layers.
91. What is the time complexity of searching in a skip list?
A self-balancing binary search tree is a binary search tree that automatically adjusts its
structure to maintain balance, ensuring efficient search, insertion, and deletion
operations.
A stack is a region of memory used for automatic storage of variables and function call
information, while a heap is a region of memory used for dynamic memory allocation.
95. What is the time complexity of searching in a self-balancing binary search tree?
The time complexity of searching in a self-balancing binary search tree, such as an AVL
tree or red-black tree, is O(log n) (logarithmic time).
A circular linked list is a linked list in which the last node points back to the first node,
forming a loop.
A priority queue is an abstract data type that supports inserting elements with
associated priorities and retrieving the element with the highest (or lowest) priority.
98. What is the time complexity of inserting an element into a priority queue?
The time complexity of inserting an element into a priority queue depends on the
implementation. In a binary heap-based priority queue, the time complexity is O(log n)
(logarithmic time).
A disjoint set data structure is a data structure that keeps track of a collection of
disjoint (non-overlapping) sets and supports efficient union and find operations.
100. What is the time complexity of finding the root of a disjoint set?
The time complexity of finding the root of a disjoint set using the find operation is
typically O(α(n)), where α is the inverse Ackermann function (a very slowly growing
function).