DSA mock
DSA mock
Q) Explain following terms w.r.t. height balance tree LL, RR, LR, RL
1. LL (Left-Left) Rotation
Example:
30
20
10
---
2. RR (Right-Right) Rotation
Example:
10
20
30
---
3. LR (Left-Right) Rotation
Solution: Perform a left rotation on left child, then right rotation on node.
Example:
30
10
20
Unbalanced at 30.
---
4. RL (Right-Left) Rotation
Solution: Perform a right rotation on right child, then left rotation on node.
Example:
10
30
20
Unbalanced at 10.
Step-by-Step Example:
30
/ \
20 40
10
1. Delete Node 40
20
10
20
/ \
10 30
---
3. Check balance factor at each node from the deleted node up to root.
i) Red-Black Tree
A Red-Black Tree is a type of self-balancing binary search tree where each node has an extra bit for
color (either red or black). The tree is balanced by applying color rules, not strict height balancing like
AVL.
3. No two red nodes can be adjacent (i.e., a red node cannot have a red parent or child).
4. Every path from a node to its descendant NULL nodes must have the same number of black nodes.
Example:
Steps:
Final Tree:
20(B)
/ \
10(R) 30(R)
A Splay Tree is a self-adjusting binary search tree. When you access (search/insert/delete) a node, it
is moved to the root using rotations. This keeps frequently accessed nodes near the top.
Zig-Zig: Node and parent are both left or both right children → double rotation.
Zig-Zag: Node is left child of right parent or vice versa → two different rotations.
Example:
30
20
10
-----------------------UNIT 5 ------------------------------
3. If K is in an internal node:
a. If left child has ≥ t keys → replace K with predecessor and delete it.
b. Else if right child has ≥ t keys → replace K with successor and delete it.
c. Else → merge both children with K and delete from merged node.
4. If K is not found:
5. After deletion, if the root is empty, make its child the new root.
5. If the root becomes empty, make the first child the new root.
Q) Explain with example trie tree. Give advantage and applications of trie tree
A Trie (pronounced as "try") is a tree-based data structure that stores strings (or sequences of
characters). It’s often used to store a dynamic set of strings, where each node represents a character
in a string. Tries are especially useful for tasks like searching, auto-completion, and dictionary
lookups.
Let’s say we want to insert the following words into a Trie: "cat", "bat", "ball", "balloon".
Trie Structure:
(Root)
/ \
c b
/ \
a a
/\ \
t l l
/ \
o o
/ \
o n
Insert "cat": Start at the root and create nodes for 'c', 'a', and 't'.
Insert "bat": Reuse the 'b' and 'a' nodes from "bat", create a 't' node.
Insert "ball": Reuse 'b' and 'a' nodes, create 'l' nodes.
Insert "balloon": Reuse 'b', 'a', 'l', and 'l' nodes, create 'o' nodes.
2. Prefix-based Searching:
3. No Comparisons of Keys:
Application:
Spell Checker
Auto-complete systems
Dictionary implementation
--------------------------UNIT 6 ------------------------------------
What is a File?
A file is a collection of data stored on a storage device (like a hard disk), used to store input/output
permanently. In C++, files are used to read and write data using file streams (ifstream, ofstream,
fstream).
3. ios::app – Opens the file in append mode, data is added at the end.
4. ios::ate – Opens the file and moves the pointer to the end.
---
An inverted file is a data structure used to index and quickly retrieve information.
Q) Explain any 3 operations carried out on sequentail file and its pseudo code.
1. Insertion
Since records are stored in a sequence, inserting in the middle requires rewriting the file.
---
2. Deletion
---
3. Searching
Sequential file organization is a method where records are stored in a sequence, one after another, in
the order they were added or sorted by a key field (e.g., roll number, ID, etc.). Records are accessed
sequentially, starting from the beginning of the file.
Advantages:
2. Efficient for processing large volumes of data (like reading all records).
3. Good for applications where records are processed in order (like payroll).
---
Disadvantages:
2. Insertion and deletion are time-consuming (may require rewriting the file).
3. Wastes space if many records are marked for deletion and not physically removed.