2 - Data Structures
2 - Data Structures
Notes:
AVL Trees are not explicitly covered in CLRS 4th edition. You may need to
consult supplemental texts like "Algorithms" by Sedgewick or Weiss's Data
Structures.
The implementation details of stacks, queues, and lists are foundational and
appear in Chapter 10 with examples using arrays and linked lists.
Priority queues are handled via heaps, which are fully explored in Chapter 6.
Graph theory is covered extensively in Chapters 22 through 26, including BFS,
DFS, MSTs, and shortest paths.
1. Stacks
2. Queues
3. Linked Lists
4. Trees (Basic Concepts)
5. Priority Queues & Heaps
6. Hashing
7. Binary Search Trees (BSTs)
8. AVL Trees
9. Red-Black Trees
10. Graphs
✅ Clear Definitions
Operations and Their Complexities
Visual Intuition (where applicable)
Applications
Practice Questions with Answers
1. Stacks
✅ Definition:
A stack is a linear data structure that follows the LIFO (Last In First Out) principle. The
last element inserted is the first to be removed.
Basic Operations:
Applications:
procedure push(stack, x)
if top == capacity - 1 then
error "Stack Overflow"
top ← top + 1
stack[top] ← x
procedure pop(stack)
if top == -1 then
error "Stack Underflow"
top ← top - 1
procedure peek(stack)
return stack[top]
Practice Questions
Q1: Suppose we push the following sequence onto a stack: 5, 10, 15, and then pop
twice. What will be the result of the next pop?
✅ Answer:
✅ Answer:
❌ False — it returns the top element without modifying the stack.
Q3: What is the time complexity of pushing n elements onto an initially empty stack?
✅ Answer:
Each push is 𝑂(1), so for n elements:
Time = ( )
Which values are popped, and what is the final state of the stack?
✅ Answer:
push(1) → [1]
push(2) → [1, 2]
pop() → removes 2 → [1]
push(3) → [1, 3]
peek() → returns 3
pop() → removes 3 → [1]
pop() → removes 1 → []
✅ Answers:
1. True
2. True
3. False
4. False (It’s 𝑂( ) if no direct access)
Topic 2: Queues
✅ Definition:
A queue is a linear data structure that follows the FIFO (First In First Out) principle. The
element that is added first is the one removed first.
Visual Behavior:
Front → [ A B C D ] ← Rear
dequeue → removes A
enqueue(E) → [ B C D E ]
Applications of Queues
procedure enqueue(Q, x)
if rear == capacity - 1 then
error "Queue Overflow"
rear ← rear + 1
Q[rear] ← x
procedure dequeue(Q)
if front > rear then
error "Queue Underflow"
x ← Q[front]
front ← front + 1
return x
Note: To make it more efficient, we often implement queues as circular arrays or linked
lists to avoid unused space.
✅ Answer:
enqueue(10) → [10]
enqueue(20) → [10, 20]
dequeue() → removes 10 → [20]
enqueue(30) → [20, 30]
dequeue() → removes 20 → [30]
✅ Answers:
1. ❌ False
2. ❌ False
3. ✅ True
4. ✅ True
Q3: Which data structure would you choose for each task?
✅ Definition:
A linked list is a linear data structure where each element (called a node) contains:
Data
A reference (pointer) to the next node in the sequence
Basic Operations
class Node
data
next
procedure insertAtBeginning(head, x)
newNode ← new Node(x)
newNode.next ← head
head ← newNode
📘 Example:
insertAtBeginning(5)
insertAtBeginning(10)
insertAtEnd(20)
10 → 5 → 20 → NULL
✅ Answer:
New list: head → 1 → 3 → 7 → 12 → NULL
Q2: Suppose you want to delete node with value 7 in a singly linked list.
What must you know?
✅ Answer:
You need a pointer to the previous node (with value 3) to adjust its next pointer to skip
7.
1. The time complexity of inserting at the head of a singly linked list is ______
2. Searching for an element in a linked list is ______
3. A circular list connects the last node to the ______
✅ Answers:
1. 𝑂(1)
2. 𝑂( )
3. First node / Head
1. Linked lists are better than arrays when insertions are frequent. → ______
2. A doubly linked list uses less memory than a singly linked list. → ______
3. It is easy to access the middle of a linked list in 𝑂(1) time. → ______
4. Each node in a doubly linked list has two pointers. → ______
✅ Answers:
1. ✅ True
2. ❌ False
3. ❌ False
4. ✅ True
Topic 4: Trees
✅ Definition:
Basic Terminology
Term Description
Root Topmost node
Parent A node that has children
Child A node connected below its parent
Leaf A node with no children
Subtree A tree formed by a node and its descendants
Height Longest path from node to a leaf
Depth Number of edges from root to the node
Types of Trees
Type Description
Binary Tree Each node has ≤ 2 children
Binary Search Tree (BST) Binary tree with left < root < right
Full Binary Tree Every node has 0 or 2 children
Complete Binary Tree All levels full except possibly last
Balanced Tree Height of left and right subtrees differ by ≤ 1
10
/ \
5 15
/\ \
2 7 20
Q1: Given the binary tree above, what is the result of a post-order
traversal?
✅ Answer:
Post-order: 2, 7, 5, 20, 15, 10
✅ Answers:
1. Sorted
2. Leaf
3. 2
✅ Answers:
1. ❌ False
2. ✅ True
3. ❌ False (It’s 0)
4. ❌ False (It uses a queue)
✅ What is a Heap?
A heap is a binary tree-based data structure that satisfies the heap property and is
commonly used to implement priority queues.
If node is at index i:
Left child = 2i
Right child = 2i + 1
Parent = ⌊i / 2⌋
Time
Operation Description
Complexity
insert(x) Add new element & restore heap 𝑂(log )
extractMin() or extractMax() Remove root element 𝑂(log )
heapify() Convert array to heap 𝑂( )
buildHeap() Build heap from unordered array 𝑂( )
Applications of Heaps
Priority queues
Heap sort
Graph algorithms (e.g., Dijkstra, Prim)
Median maintenance
Q1: What will the array look like after inserting 60 into this max-heap?
Current heap array: [50, 30, 40, 10, 20, 35, 25]
✅ Answer:
Insert 60 at the end → [50, 30, 40, 10, 20, 35, 25, 60]
Bubble up (swap with 10, then 30, then 50) → [60, 50, 40, 30, 20, 35, 25, 10]
✅ Answers:
1. Minimum
2. 𝑂(log )
3. 𝑂(log )
1. ❌ False
2. ✅ True
3. ✅ True
4. ✅ True
Topic 6: Hashing
✅ Definition:
Hashing is a technique used to map data (keys) to fixed-size values called hash codes,
which are typically used as indexes in a hash table for efficient access.
Hash Table:
Hash Function
Collisions in Hashing
A collision happens when two keys map to the same index.
Strategy Formula
Linear Probing ℎ( , ) = (ℎ( ) + )mod
Quadratic Probing ℎ( , ) = (ℎ( ) + 1 + 2 2 )mod
Double Hashing ℎ( , ) = (ℎ1 ( ) + ⋅ ℎ2 ( ))mod
Q1: Suppose you use a hash function h(k) = k mod 10 and your table size is 10.
✅ Answer:
27mod 10 = 7 → Insert at index 7
1. Uniformly
2. Linked List
3. Fast lookup (or searching)
✅ Answers:
A. Linear probing
B. Quadratic probing
C. Double hashing
D. Chaining
✅ Answer:
C. Double hashing
✅ Definition:
A Binary Search Tree (BST) is a binary tree where each node has the following property:
Worst case occurs when the tree is unbalanced, e.g., resembling a linked list.
15
/ \
10 20
/ \
8 12
In-order traversal (Left → Root → Right) gives: 8, 10, 12, 15, 20 (sorted)
Q1: Insert the following keys into an empty BST: 25, 15, 50, 10, 22, 35, 70
✅ Answer:
In-order: 10, 15, 22, 25, 35, 50, 70
✅ Answers:
1. Sorted
2. 𝑂( )
3. In-order successor or predecessor
✅ Answers:
1. ❌ False
2. ✅ True
3. ✅ True
4. ❌ False
An AVL tree is a self-balancing binary search tree where the difference in height
(balance factor) between left and right subtrees for any node is at most 1.
Single Rotations
Type Description
Left Rotation (LL) Heavy on left-left side
Type Description
Right Rotation (RR) Heavy on right-right side
Double Rotations
Type Description
Left-Right (LR) Heavy on left-right → rotate left then right
Right-Left (RL) Heavy on right-left → rotate right then left
📘 Example
Result:
20
/ \
10 30
✅ Advantages:
Disadvantages:
Q1: Which rotation is required after inserting 30, 20, 10 into an AVL tree?
✅ Answer:
Right Rotation (LL Case)
✅ Answers:
1. Rotations
2. 𝑂(log )
3. −1 and +1
✅ Answers:
1. ❌ False
2. ✅ True
3. ✅ True
A Red-Black Tree is a self-balancing binary search tree where each node contains an
extra bit for color (either Red or Black). It ensures balance through coloring and
rotations, guaranteeing 𝑂(log ) time complexity.
These properties guarantee that the longest path from the root to a leaf is no more than
twice as long as the shortest path.
Left Rotation
Right Rotation
Time Complexities
Operation Time Complexity
Search 𝑂(log )
Insert 𝑂(log )
Delete 𝑂(log )
✅ Answers:
1. ❌ False
2. ❌ False
3. ✅ True
4. ❌ False
✅ Answers:
1. Black
2. 2
3. Flexible / Lazy
✅ Answers:
As listed
✅ What is a Graph?
Graphs are used to model relationships and networks, such as roads, social networks, or
web links.
Types of Graphs
Type Description
Undirected Edges have no direction
Directed (Digraph) Edges have direction (A → B ≠ B → A)
Weighted Edges have weights or costs
Unweighted Edges are equal in value
Cyclic Contains at least one cycle
Acyclic No cycles (e.g., DAG = Directed Acyclic Graph)
Connected Path exists between every pair of nodes
Disconnected Some nodes are unreachable from others
Graph Representations
Time
Algorithm Type Description
Complexity
BFS Breadth-First Search Level-by-level using a queue 𝑂( + )
DFS Depth-First Search Depth-first using a stack (or recursion) 𝑂( + )
Applications of Graphs
Social network analysis
Routing and navigation (e.g., GPS)
Dependency resolution (e.g., build systems)
Web crawling and link analysis
Network flow, scheduling
Graph:
A–B–C
| \
D E
Adjacency List:
A: B, D
B: A, C, E
C: B
D: A
E: B
Q1: What is the time complexity of BFS on a graph with n vertices and e
edges?
✅ Answer:
𝑂( + )
✅ Answers:
1. DAG (Directed Acyclic Graph)
2. Stack
3. Directed
✅ Answers:
1. ✅ True
2. ❌ False
3. ❌ False
4. ✅ True
✅ Answers:
As listed
1. Stacks
MCQ 1:
Which of the following operations can be performed on a stack?
A. enqueue, dequeue
B. push, pop
C. insert, delete at end
D. traverse, peek at middle
TF 1:
In a stack, the last element inserted is the first to be removed.
✅ Answer: True
2. Queues
MCQ 2:
Which data structure allows insertion at one end and deletion from the other?
A. Stack
B. Array
C. Queue
D. Tree
✅ Answer: C. Queue
TF 2:
A circular queue helps in optimizing space usage.
✅ Answer: True
3. Linked Lists
MCQ 3:
Which of the following is true about singly linked lists?
A. They allow backward traversal
B. They waste memory
C. Each node stores data and a pointer to the next node
D. Insertion is always O(n)
✅ Answer: C. Each node stores data and a pointer to the next node
TF 3:
Doubly linked lists are more memory efficient than singly linked lists.
✅ Answer: False
4. Trees
MCQ 4:
What is the in-order traversal of a BST with nodes inserted in the order 10, 5, 15?
A. 10, 5, 15
B. 5, 10, 15
C. 15, 10, 5
D. 5, 15, 10
✅ Answer: B. 5, 10, 15
TF 4:
A binary tree is always a binary search tree.
✅ Answer: False
MCQ 5:
What is the time complexity of inserting into a binary heap?
A. O(1)
B. O(n)
C. O(log n)
D. O(n log n)
✅ Answer: C. O(log n)
TF 5:
In a max-heap, the smallest value is always at the root.
✅ Answer: False
6. Hashing
MCQ 6:
Which of these is a common collision resolution strategy?
A. Merging
B. Probing
C. Sorting
D. Balancing
✅ Answer: B. Probing
TF 6:
Open addressing and chaining are two methods to resolve collisions.
✅ Answer: True
MCQ 7:
What is the worst-case time complexity of search in an unbalanced BST?
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
✅ Answer: C. O(n)
TF 7:
In-order traversal of a BST always gives sorted output.
✅ Answer: True
8. AVL Trees
MCQ 8:
What is the maximum balance factor in an AVL tree?
A. 0
B. 1
C. 2
D. ∞
✅ Answer: B. 1
TF 8:
AVL trees may require multiple rotations after insertion.
✅ Answer: True
9. Red-Black Trees
MCQ 9:
Which of the following is not a red-black tree property?
A. Every node is either red or black
B. The tree is always height-balanced
C. Red nodes must have red children
D. All leaves (NILs) are black
✅ Answer: C. Red nodes must have red children (❌ it's the opposite)
TF 9:
The root of a red-black tree must always be black.
✅ Answer: True
10. Graphs
MCQ 10:
Which representation is best for sparse graphs?
A. Adjacency Matrix
B. Adjacency List
C. Incidence Matrix
D. List of Edges
TF 10:
Depth-first search uses a queue for traversal.
✅ Answer: False (uses stack)
Here's a large set of advanced graduate-level questions on all 10 data structure topics.
These questions are designed to test deep understanding, critical thinking, and
algorithmic application. Each question includes the correct answer or explanation.
Advanced Graduate-Level Questions on Data
Structures
1. Stacks
Q1:
You are implementing a browser with Back and Forward buttons. Which data structures
are most appropriate to manage navigation history efficiently?
A. Queue
B. Stack and Queue
C. Two Stacks
D. Single Deque
Q2:
Given a stack with nnn elements, what is the minimum number of stack operations
(push, pop, peek) required to reverse it using only one additional stack?
✅ Answer: 𝑂( 2
)
2. Queues
Q3:
A circular queue has a front at position 2 and rear at position 6 in a size-10 array. How
many elements are currently in the queue?
✅ Answer: 5
A. Priority Queue
B. Deque
C. Circular Queue
D. FIFO Queue
3. Linked Lists
Q5:
You want to delete the last node of a singly linked list in 𝑂(1) time. What modification is
necessary?
✅ Answer: Maintain a tail pointer and a previous pointer to the last node.
Q6:
Which type of linked list is most efficient for implementing a deque?
4. Trees
Q7:
Prove or disprove: A binary tree with n internal nodes has exactly n + 1 leaves.
Q8:
What is the average depth of a node in a perfectly balanced binary tree of height hhh?
✅ Answer: 𝑂(log )
Q9:
How do you merge two binary heaps (max-heaps) efficiently?
Q10:
When inserting into a min-heap, under what condition is no bubbling required?
6. Hashing
Q11:
In open addressing with linear probing, what is primary clustering, and how can it be
reduced?
✅ Answer:
Primary clustering refers to long runs of occupied slots causing poor performance.
Use quadratic probing or double hashing to mitigate it.
Q12:
Assuming a load factor α=0.75\alpha = 0.75α=0.75, what is the expected number of
probes in successful search under open addressing?
✅ Answer:
1 1 1 1
(1+1 − ) = (1+ ) = 2.5
2 2 0.25
Q13:
Prove that the maximum number of comparisons for an unsuccessful search in a BST
with n nodes is h + 1, where h is the height.
✅ Answer:
True. Every unsuccessful search ends at a NULL pointer after traversing h + 1 nodes at
most.
Q14:
Which sequence of inserts produces the worst-case height in a BST?
A. Sorted ascending
B. Random
C. Balanced
D. Sorted alternating ends
Q15:
Insert 1, 2, 3 into an AVL tree. How many rotations are needed?
Q16:
What is the maximum number of rotations required during a single insertion in an AVL
tree?
✅ Answer: 2 rotations
9. Red-Black Trees
Q17:
What is the maximum height of a Red-Black Tree with n nodes?
✅ Answer:
2log 2 ( + 1)
Q18:
During insertion into a red-black tree, how many nodes may require recoloring?
✅ Answer:
Up to ( )
Q19:
Which of the following statements about BFS and DFS is correct?
Q20:
Which graph representation is most efficient for computing in-degree and out-degree?
Can compute both efficiently; in-degree may need reverse edges or secondary data.
1. Stack
Definition: LIFO (Last-In First-Out) data structure.
Operations: push(x), pop(), peek(), isEmpty()
Time Complexity: All operations 𝑂(1)
Use Cases: Expression evaluation, backtracking, undo functionality
Implementation: Array or Linked List
2. Queue
Definition: FIFO (First-In First-Out) structure.
Types: Simple Queue, Circular Queue, Double-Ended Queue (Deque), Priority
Queue
Operations: enqueue(x), dequeue(), peek(), isEmpty()
Time Complexity: All 𝑂(1)
Use Cases: Scheduling, buffering, BFS traversal
3. Linked List
Types:
o Singly Linked List (SLL)
o Doubly Linked List (DLL)
o Circular Linked List
Operations: Insertion, deletion, traversal
Time Complexity: Insertion/deletion at head 𝑂(1), at tail or middle 𝑂( )
Use Cases: Dynamic memory allocation, implementing stacks, queues
4. Trees
Binary Tree: Each node has up to 2 children
Binary Search Tree (BST):
o Left < Root < Right
o In-order traversal yields sorted order
Time Complexity:
o Insert/Search/Delete: Avg 𝑂(log ), Worst 𝑂( )
8. AVL Trees
Self-balancing BST
Balance Factor ∈ {−1, 0, +1}
Rotations: LL, RR, LR, RL
Time Complexity: All operations 𝑂(log )
Use Cases: Read-intensive apps requiring balanced trees
⚫ 9. Red-Black Trees
Properties:
o Root is black
o Red node → black children
o Uniform black height
Rotations & Recoloring to maintain balance
Time Complexity: 𝑂(log ) for all operations
Use Cases: Write-optimized systems (Linux kernel, databases)
10. Graphs
Components: Nodes (vertices), Edges
Types: Directed/Undirected, Weighted/Unweighted, Cyclic/Acyclic
Representations:
o Adjacency List: 𝑂( + )
o Adjacency Matrix: 𝑂( 2 )
Traversals:
o BFS (Queue): Level-wise
o DFS (Stack/Recursion): Depth-first
Use Cases: Networking, social networks, compilers, scheduling
Let's now explore algorithms applied to data structures, categorized by type. Each
section includes brief theory, example, and complexity analysis. We'll cover:
1. Traversal Algorithms
2. Sorting Algorithms
3. Searching Algorithms
4. Graph Algorithms (Bonus)
1. Traversal Algorithms
✅ A. Tree Traversals
Example (In-order):
10
/ \
5 15
➡ In-order: 5 10 15
Time Complexity: 𝑂( )
Space: 𝑂(ℎ) for recursion stack (h = height)
✅ B. Graph Traversals
Use Cases:
Time Complexity: 𝑂( + )
Space: 𝑂( )
2. Sorting Algorithms
✅ A. Comparison-based
3. Searching Algorithms
✅ A. Array-based
✅ B. Tree-based
✅ C. Hash-based
1. Tree Traversals
2. Sorting
3. Searching
4. Graph Algorithms
1. Tree Traversals
Problem 1:
A
/\
B C
/\ \
D E F
✅ Solution:
Problem 2:
✅ Solution in pseudocode:
function levelOrder(root):
if root == null: return
queue = new Queue()
queue.enqueue(root)
while not queue.isEmpty():
node = queue.dequeue()
print(node.value)
if node.left: queue.enqueue(node.left)
if node.right: queue.enqueue(node.right)
2. Sorting Algorithms
Problem 3:
✅ Solution (steps):
Problem 4:
3. Searching Algorithms
Problem 5:
Time: 𝑂(log )
Problem 6:
✅ Answer:
4. Graph Algorithms
Problem 7:
A -- B
| |
D -- C
✅ Solution:
Queue: A
Visit: A → B → D → C
Traversal order: A, B, D, C
Problem 8:
✅ Steps:
Start from A: dist[A] = 0
Update neighbors: B=1, C=4
B next: update C=3, D=6
C next: update D=4
D next: update E=7
Problem 1:
Post-order: D, E, B, F, C, A
In-order: D, B, E, A, F, C
✅ Solution:
Final Tree:
A
/\
B C
/\ /
D EF
2. Sorting with Constraints
Problem 2:
You are given an array of 10 million integers in the range [0, 1000].
Q: Design an optimal sorting algorithm.
✅ Solution:
Use Counting Sort since the range is small and integers are non-negative.
Time Complexity: 𝑂( + ) = 𝑂(107 + 103 )
Problem 3:
✅ Solution:
Problem 4:
𝑂(log ) insertions
𝑂(log ) range queries
Sorted traversal in 𝑂( )
Problem 5:
You have a hash table with open addressing. What is the expected number of probes in
an unsuccessful search if the load factor is 0.9?
✅ Solution:
Using linear probing:
1 1
[unsuccessful probes] = = = 10
1− 1 − 0.9
4. Graph Algorithms
Problem 6:
Given a directed acyclic graph (DAG), design an algorithm to find all possible
topological sorts.
✅ Solution:
Problem 7:
Prove or disprove: Dijkstra’s algorithm fails when graph has negative edge weights,
even if no negative cycles exist.
✅ Answer: True
Example:
A → B (weight = 2)
A → C (weight = 1)
C → B (weight = -2)
Problem 8:
You need to find the minimum spanning tree of a dense graph with 1000 nodes and
495,000 edges. Choose the best algorithm.
✅ Solution: