Dsa Mock Insem Question Bank
Dsa Mock Insem Question Bank
Explain concept
of inverted files.
What is a File?
In computing, a file is a collection of related data stored on a non-volatile
storage device like a hard drive or SSD. In C++, files are used for permanent
storage of data — unlike variables which lose their values when a program
ends.
In C++, files are handled using file streams, provided by the <fstream>
header.
Mode Description
🛠 Example:
cpp
CopyEdit
fstream file;
file.open("data.txt", ios::in | ios::out);
Inverted Files
Key Concept:
Instead of storing documents with their contents, it stores a dictionary of
words and for each word, a list of documents (or positions) where it
appears.
Example:
Documents:
vbnet
CopyEdit
Doc1: "apple banana mango"
Doc2: "banana apple"
Doc3: "mango fruit"
Inverted File:
Word Document(s)
fruit Doc3
Q2 Define external sort with eaxmple.
External sorting is a class of algorithms used to sort data that is too large
to fit into main memory (RAM) and must reside in external storage (like a
hard drive).
It is typically used when dealing with huge files, like in database systems or
big data processing.
Main File:
[1001, 1002, ..., 1049], [1050, ..., 1099], ...
Advantages:
• Faster than pure sequential search
• Easier to update than fully indexed structures
Example:
Let's say you are storing student records sorted by Roll Number:
CopyEdit
101 John
102 Alice
103 Bob
104 Zara
If you want to find Roll No. 103, the system reads:
• 101 → 102 → 103 (stop here)
Advantages of Sequential File Organization
Advantage Description
Efficient for Full Read Fast when accessing all records in order
Minimal Storage
No indexing or extra data structures needed
Overhead
Good for Batch Ideal for tasks like payroll, billing, report
Processing generation
Disadvantage Description
Slow Random
Must search from start → slow for large files
Access
Use Cases:
• Payroll systems
• Monthly billing systems
• Bank transaction logs
• Historical data processing
Q5 What is B+ tree? Give structure of it’s internal note. What is the difference
between B and B+ tree.
What is a B+ Tree?
A B+ Tree is a balanced search tree used for storing large amounts of
sorted data and allows efficient searching, insertion, and deletion.
It is widely used in:
• Databases
• File systems
• Indexing structures
Search
Slower range search Faster due to linked leaves
Efficiency
Example:
Suppose we insert keys: 10, 20, 30, 40, 50
B Tree (Order 3):
css
CopyEdit
[30]
/ \
[10,20] [40,50]
B+ Tree (Order 3):
css
CopyEdit
[30]
/ \
[10,20] [30,40,50]
↔
• All data is in leaf nodes
• Leaves are linked
Q6 Build B+ tree of order 3 for the following data: F, S, Q, K, C, L, H, T, V, W, M,
R
Given Data:
F, S, Q, K, C, L, H, T, V, W, M, R
Step 1: Insert C, F
makefile
CopyEdit
Leaf: [C, F]
Continue Inserting: Q, R, S
• Q goes to [L, M] → [L, M, Q] → Split → [L], [M, Q] → promote M
• Insert R → goes to [M, Q] → becomes [M, Q, R] → Split → [M], [Q, R] →
promote Q
• Insert S → goes to [Q, R] → becomes [Q, R, S] → Split → [Q], [R, S] →
promote R
Update internal nodes accordingly.
Final Inserts: T, V, W
• Continue inserting with same logic into rightmost leaf
• Manage splits and promotion as needed
Input:
1, 4, 7, 10, 17, 21, 31, 25, 19, 20, 28, 42
B+ Tree of Order 4:
• Max 3 keys in an internal node
• Max 4 children per internal node
• Max 3 keys per leaf node
• All actual data is stored in leaves
• Leaf nodes are linked
Step-by-Step Construction
We'll insert the keys in the given order and split as needed.
Step 1: Insert 1, 4, 7
Leaf node: [1, 4, 7] — no split needed
Step 2: Insert 10
Leaf: [1, 4, 7, 10] → 4 keys → Split
• Split into: [1, 4] and [7, 10]
• Promote 7 to parent
css
CopyEdit
[7]
/ \
[1, 4] [7, 10]
Step 3: Insert 17
Goes to [7, 10] → becomes [7, 10, 17] → OK
Step 4: Insert 21
Leaf: [7, 10, 17, 21] → split into [7, 10] and [17, 21]
Promote 17
Update root to: [7, 17]
less
CopyEdit
[7, 17]
/ | \
[1, 4] [7,10] [17, 21]
Step 5: Insert 31
Goes to [17, 21] → becomes [17, 21, 31] → OK
Step 6: Insert 25
Leaf [17, 21, 31] → becomes [17, 21, 25, 31] → split
→ [17, 21], [25, 31] → promote 25 to parent
Now parent [7, 17] becomes [7, 17, 25]
less
CopyEdit
[7, 17, 25]
/ | | \
[1,4] [7,10] [17,21] [25,31]
Step 7: Insert 19
Goes to [17, 21] → becomes [17, 19, 21] — OK
Step 8: Insert 20
Leaf [17, 19, 21] → becomes [17, 19, 20, 21] → split
→ [17, 19], [20, 21] → promote 20
Now parent [7, 17, 25] is full → needs split
Split [7, 17, 25] → [7], [20] → promote 17 to new root
css
CopyEdit
[17]
/ \
[7] [20]
/ \ / \
[1,4][7,10] [17,19][20,21][25,31]
Step 9: Insert 28
Goes to [25, 31] → becomes [25, 28, 31] → OK