Pdf 9 - Data Structure based Questions
Q1. What are Data Structures?
Answer:
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. There are many
types of data structures, each with its own uses. When designing code, we need to pay
particular attention to the way data is structured. If data isn't stored e ciently or correctly
structured, then the overall performance of the code will be reduced.
Q2. Why Create Data Structures?
Answer:
Data structures serve a number of important functions in a program. They ensure that
each line of code performs its function correctly and e ciently, they help the programmer
identify and x problems with his/her code, and they help to create a clear and organized
code base.
Q3. What are some applications of Data structures?
Answer:
• Decision Making
• Genetics
• Image Processing
• Blockchain
• Numerical and Statistical Analysis
• Compiler Design
• Database Design and many more
Q4. Explain the process behind storing a variable in memory.
Answer:
• A variable is stored in memory based on the amount of memory that is needed.
Following are the steps followed to store a variable:
• The required amount of memory is assigned rst.
• Then, it is stored based on the data structure being used.
• Using concepts like dynamic allocation ensures high e ciency and that the storage
units can be accessed based on requirements in real-time.
Q5. Can you explain the di erence between le structure and storage structure?
Answer:
• File Structure: Representation of data into secondary or auxiliary memory say any
device such as a hard disk or pen drives that stores data which remains intact until
manually deleted is known as a le structure representation.
• 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.
fi
ff
fi
fi
fi
ffi
ffi
ffi
The di erence is that the storage structure has data stored in the memory of the
computer system, whereas the le structure has the data stored in the auxiliary memory.
Q6. Describe the types of Data Structures?
Answer:
• 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 elements in one pass in a non-linear data structure, as in a linear data
structure. Trees and graphs are two examples of non-linear data structures.
Q7. What is a stack data structure? What are the applications of stack?
Answer:
A stack is a data structure that is used to represent the state of an application at a
particular point in time. The stack consists of a series of items that are added to the top
of the stack and then removed from the top. It is a linear data structure that follows a
particular order in which operations are performed.
Following are some applications for stack data structure:
• It acts as temporary storage during recursive operations
• Redo and Undo operations in doc editors
• Reversing a string
• Parenthesis matching
• Post x to In x Expressions
• Function calls order
Q8. What are di erent operations available in stack data structure?
Answer:
• push: This adds an item to the top of the stack. The over ow condition occurs if
the stack is full.
• pop: This removes the top item of the stack. Under ow condition occurs if the
stack is empty.
• top: This returns the top item from the stack.
• isEmpty: This returns true if the stack is empty else false.
• size: This returns the size of the stack.
Q9. What is a queue data structure? What are the applications of queue?
Answer:
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. Queues are commonly used in situations
where the users want to hold items for a long period of time, such as during a checkout
fi
ff
fi
ff
fi
fl
fl
process. A good example of a queue is any queue of customers for a resource where the
rst consumer is served rst.
some applications of queue data structure:
• Breadth- rst search algorithm in graphs
• Operating system: job scheduling operations, Disk scheduling, CPU scheduling
etc.
• Call management in call centres
Q10. What are di erent operations available in queue data structure?
Answer:
• enqueue: This adds an element to the rear end of the queue. Over ow conditions
occur if the queue is full.
• dequeue: This removes an element from the front end of the queue. Under ow
conditions occur if the queue is empty.
• isEmpty: This returns true if the queue is empty or else false.
• rear: This returns the rear end element without removing it.
• front: This returns the front-end element without removing it.
• size: This returns the size of the queue.
Q11. Di erentiate between stack and queue data structure.
Answer:
Stack Queue
Stack is a linearQueue is a
data structure linear data
where data is structure where
added and data is ended at
removed from the rear end and
the top. removed from
the front.
Stack is based Queue is based
on LIFO(Last In on FIFO(First In
First Out) First Out)
principle principle
Insertion Insertion
operation in operation in
Stack is known Queue is known
as push. as eneque.
Delete operation Delete operation
in Stack is in Queue is
known as pop. known as pop.
fi
ff
fi
ff
fi
fl
fl
Only one pointer Two pointers are
is available for available for
both addition addition and
and deletion: deletion: front()
top() and rear()
Used in solving Used in solving
recursion sequential
problems processing
problems
Q12. How to implement a queue using stack?
Answer:
A queue can be implemented using two stacks. Let q be the queue and
stack1 and stack2 be the 2 stacks for implementing q. We know that stack supports
push, pop, and peek operations and using these operations, we need to emulate the
operations of the queue - enqueue and dequeue. Hence, queue q can be implemented in
two methods (Both the methods use auxiliary space complexity of O(n)).
Q13. How do you implement stack using queues?
Answer:
A stack can be implemented using two queues. We know that a queue supports enqueue
and dequeue operations. Using these operations, we need to develop push, pop
operations.
Q14. What is array data structure? What are the applications of arrays?
Answer:
An array data structure is a data structure that is used to store data in a way that is
e cient and easy to access. It is similar to a list in that it stores data in a sequence.
However, an array data structure di ers from a list in that it can hold much more data than
a list can. An array data structure is created by combining several arrays together. Each
array is then given a unique identi er, and each array’s data is stored in the order in which
they are created.
Array data structures are commonly used in databases and other computer systems to
store large amounts of data e ciently. They are also useful for storing information that is
frequently accessed, such as large amounts of text or images.
Q15. 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.
ffi
ffi
fi
ff
• Dynamic management for Operating System memory.
• Round robin scheduling for operating system tasks.
• Forward and backward operation in the browser.
Q16. Di erence between Array and Linked List.
Answer:
Arrays Linked Lists
An array is a A linked list is a
collection of collection of
data elements entities known
of the same as nodes. The
type. node is divided
into two
sections: data
and address.
It keeps the It stores
data elements in elements at
a single random, or
memory. anywhere in the
memory.
The memory The memory
size of an array size of a linked
is xed and list is allocated
cannot be during runtime.
changed during
runtime.
An array's Linked List
elements are elements are
not dependent dependent on
on one another. one another.
It is easier and In the linked list,
faster to access it takes time to
an element in an access an
array. element.
Memory Memory
utilization is utilization is
ine ective in the e ective in the
case of an array. case of an array.
Operations like Operations like
insertion and insertion and
deletion take deletion are
longer time in faster in the
an array. linked list.
Q17. What is an asymptotic analysis of an algorithm?
ff
fi
ff
ff
Answer:
Asymptotic analysis of an algorithm de nes the run-time performance as per its
mathematical boundations. Asymptotic analysis helps us articulate the best case(Omega
Notation, Ω), average case(Theta Notation, θ), and worst case(Big Oh Notation, Ο)
performance of an algorithm.
Q18. What is hashmap in data structure?
Answer:
Hashmap is a data structure that uses an implementation of a hash table data structure
which allows access to data in constant time (O(1)) complexity if you have the key.
Q19. What is the requirement for an object to be used as key or value in HashMap?
Answer:
• The key or value object that gets used in the hashmap must
implement equals() and hashcode() method.
• The hash code is used when inserting the key object into the map and the equals
method is used when trying to retrieve a value from the map.
Q20. How does HashMap handle collisions in Java?
Answer:
• The java.util.HashMap class in Java uses the approach of chaining to handle
collisions. In chaining, if the new values with the same key are attempted to be
pushed, then these values are stored in a linked list stored in a bucket of the key as
a chain along with the existing value.
• In the worst-case scenario, it can happen that all keys 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.
Q21. What is the time complexity of basic operations get() and put() in HashMap
class?
Answer:
The time complexity is O(1) assuming that the hash function used in the hash map
distributes elements uniformly among the buckets.
Q22. What is binary tree data structure?
Answer:
A binary tree is a data structure that is used to organize data in a way that allows for
e cient 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
ffi
fi
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.
Q23. What are the applications for binary trees?
Answer:
• It's widely used in computer networks for storing routing table information.
• Decision Trees.
• Expression Evaluation.
• Database indices.
Q24. What is binary search tree data structure?
Answer:
A binary search tree is a data structure that stores items in sorted order. In a binary search
tree, each node stores a key and a value. The key is used to access the item and the
value is used to determine whether the item is present or not. The key can be any type of
value such as an integer, oating point number, character string, or even a combination of
these types. The value can be any type of items such as an integer, oating point number,
character string, or even a combination of these types. When a node is added to the tree,
its key is used to access the item stored at that node. When a node is removed from the
tree, its key is used to access the item stored at that node.
Q25. What are the applications for binary search trees?
Answer:
• It is used for indexing and multi-level indexing.
• It is used for implementing various search algorithms.
• It is helpful in organizing a sorted stream of data.
Q26. What are tree traversals?
Answer:
Tree traversal is the process of visiting all the nodes of a tree.
Q27. What is a dequeue data structure and its types?
Answer:
A dequeue can be thought of as an array of items, but with one important di erence:
Instead of pushing and popping items o the end to make room, dequeues are designed
to allow items to be inserted at either end. This property makes dequeues well-suited for
performing tasks such as keeping track of inventory, scheduling tasks, or handling large
amounts of data.
There are two types of dequeue:
• Input Restricted Dequeue: Insertion operations are performed at only one end
while deletion is performed at both ends in the input restricted queue.
fl
ff
fl
ff
• Output Restricted Dequeue: Deletion operations are performed at only one end
while insertion is performed at both ends in the output restricted queue.
Q28. What are some key operations performed on the Dequeue data structure?
Answer:
• insertFront(): This adds an element to the front of the Dequeue.
• insertLast(): This adds an element to the rear of the Dequeue.
• deleteFront(): This deletes an element from the front of the Dequeue.
• deleteLast():This deletes an element from the front of the Dequeue.
• getFront(): This gets an element from the front of the Dequeue.
• getRear(): This gets an element from the rear of the Dequeue.
• isEmpty(): This checks whether Dequeue is empty or not.
• isFull(): This checks whether Dequeue is full or not.
Q29. What is a priority queue?
answer:
Priority Queue is an abstract data type that is similar to a queue in that each element is
assigned a priority value. The order in which elements in a priority queue are served is
determined by their priority (i.e., the order in which they are removed). If the elements
have the same priority, they are served in the order they appear in the queue.
Q30. What are the applications for priority queue?
Answer:
• Used in graph algorithms like Dijkstra, Prim’s Minimum spanning tree etc.
• Hu man code for data compression
• Finding Kth Largest/Smallest element
Q31. Compare di erent implementations of priority queue.
Answer:
The following table contains an asymptotic analysis of di erent implementations of a
priority queue:
Operations peek insert delete
Linked List O(1) O(n) O(1)
Binary Heap O(1) O(log n) O(log n)
Binary Search O(1) O(log n) O(log n)
Tree
Q32. What is graph data structure?
Answer:
ff
ff
ff
A graph is a type of non-linear data structure made up of nodes and edges. The nodes
are also known as vertices, and edges are lines or arcs that connect any two nodes in the
graph.
Q33. What is the di erence between the Breadth First Search (BFS) and Depth First
Search (DFS)?
Answer:
Breadth First Depth First
Search (BFS) Search (DFS)
It stands for It stands for
“Breadth First “Depth First
Search” Search”
BFS (Breadth DFS (Depth First
First Search) Search) nds
nds the the shortest
shortest path path using the
using the Queue Stack data
data structure. structure.
We walk DFS begins at
through all the root node
nodes on the and proceeds
same level as far as
before passing possible
to the next level through the
in BFS. nodes until we
reach the node
with no
unvisited nearby
nodes.
When compared When compared
to DFS, BFS is to BFS, DFS is
slower. faster.
BFS performs DFS performs
better when the better when the
target is close to target is far from
the source. the source.
BFS DFS
necessitates necessitates
more memory. less memory.
Nodes that have When there are
been traversed no more nodes
multiple times to visit, the
are removed visited nodes
from the queue. are added to the
stack and then
removed.
fi
fi
ff
Backtracking is The DFS
not an option in algorithm is a
BFS. recursive
algorithm that
employs the
concept of
backtracking.
It is based on It is based on
the FIFO the LIFO
principle (First In principle (Last In
First Out). First Out).
Q34. What is AVL tree data structure?
Answer:
AVL trees are height balancing binary search trees named after their inventors Adelson,
Velski, and Landis. The AVL tree compares the heights of the left and right subtrees and
ensures that the di erence is less than one. This distinction is known as the Balance
Factor.
BalanceFactor = height(left-subtree) − height(right-subtree)
Q35. What is a B-tree data structure?
Answer:
The B Tree is a type of m-way tree that is commonly used for disc access. A B-Tree with
order m can only have m-1 keys and m children. One of the primary reasons for using a B
tree is its ability to store a large number of keys in a single node as well as large key
values while keeping the tree's height relatively small.
Q36. De ne Segment Tree data structure.
Answer:
A segment Tree is a binary tree that is used to store intervals or segments. The Segment
Tree is made up of nodes that represent intervals. Segment Tree is used when there are
multiple range queries on an array and changes to array elements.
Q37. What are the applications of trie data structure?
Answer:
some real-time applications for Trie data structure:
• Auto-Complete and Search for Search Engines
• Genome Analysis
• Data Analytics
• Browser History
• Spell Checker
fi
ff
Q38. De ne Red-Black Tree .
Answer:
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.
Q39. Which data structures are used for implementing LRU cache?
Answer:
LRU cache or Least Recently Used cache allows quick identi cation 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:
• Queue – This is implemented using a doubly-linked list. The maximum size of the
queue is determined by the cache size, i.e by the total number of available frames.
The least recently used pages will be near the front end of the queue whereas the
most recently used pages will be towards the rear end of the queue.
• Hashmap – Hashmap stores the page number as the key along with the address of
the corresponding queue node as the value.
Q40. What is a heap data structure?
Answer:
Heap is a special tree-based non-linear data structure in which the tree is a complete
binary tree. A binary tree is said to be complete if all levels are completely lled except
possibly the last level and the last level has all elements as left as possible. Heaps are of
two types:
1. Max-Heap
2. Min-Heap
—————————————————————
Test yourself
Q1. Which of the following is a non-linear data structure?
1. Array
2. Linked list
3. Queue
4. Tree
Q2. Which of the following data structures is linear?
1. Graph
2. AVL tree
3. Red-black tree
4. Stack
Q3. Which data structure is used for implementing recursion?
fi
fi
fi
1. Array
2. Stack
3. Queue
4. Linked list
Q4. Which of the following data structures is preferred in the implementation of a
database system?
1. B tree
2. Binary Search Tree
3. AVL tree
4. B+- tree
Q5. What is the worst-case time complexity of inserting n elements into an empty linked
list that must be kept in sorted order?
1. O(n)
2. O(log n)
3. O(n log n)
4. O(n^2)
Q6. A directed graph is _______ if there is a path from each vertex to every other vertex in
the graph.
1. Weakly connected
2. Strongly connected
3. Tightly connected
4. Linearly connected
Q7. In circular queue, the value of REAR would be?
1. REAR = REAR + 1
2. REAR = (REAR + 1) % (QUEUE_SIZE+1)
3. REAR = (REAR + 1) % (QUEUE_SIZE)
4. REAR = (REAR - 1) % (QUEUE_SIZE-1)
Q8. Which of the following statement is true?
i) Using singly linked lists and circular list, it is not possible to traverse the list backwards.
ii) To nd the predecessor, it is required to traverse the list from the rst node in case of
singly linked list.
1. Only (i)
2. Only (ii)
3. Both (i) and (ii)
4. None of the above
Q9. The binary search method needs no more than ________ comparisons.
1. (Log2 n)+1
2. Log n
3. (Log n)+1
4. Log2n
fi
fi
Q10. What will be the nal elements on the stack if the following sequence of operations
are executed?
* Push(a,s);
* Push(b,s);
* Pop(s);
* Push(c,s);
- where a, b, c are the data elements and s is the stack.
1. abc
2. ac
3. acb
4. b
————————————————————
ANSWER:
1. Tree
2. Stack
3. Stack
4. B+- Tree
5. O(n^2)
6. Strongly connected
7. REAR = (REAR + 1) % (QUEUE_SIZE)
8. Both (i) and (ii)
9. (Log2 n)+1
10. ac
fi