Sub topic 17
GATE problem patterns and worked strategies
This chapter assumes you know blocking factor, sparse and dense indexes, multilevel indexes, and B and B+ tree structure from the earlier chapters. It covers how GATE questions on this topic are built, what to write down first, and which conventions cause lost marks.
The five recurring patterns
Almost every question falls into one of five patterns. Name the pattern first, then choose the formula.
| Pattern | Given | Ask for | Core formula |
|---|---|---|---|
| Blocking | Block size B, record size R, record count r | Blocks in the file | bfr = floor(B/R), b = ceil(r/bfr) |
| Search cost | b, file organisation, index levels | Block accesses | Heap b/2 average, sorted ceil(log2 b), index levels + 1 |
| Order | B, key K, block pointer P, record pointer Pr | Order or leaf capacity | Inequalities on node size |
| Capacity and height | Order, leaf capacity, levels | Max or min keys, or height | Powers of the fan-out |
| Operations | Small tree and a key sequence | Splits, final shape | Split and merge rules |
Pattern 1: blocking and search cost
Unspanned records are the default unless the question says "spanned". With unspanned records, a block holds floor(B/R) records and the leftover space is wasted. Taking ceiling instead of floor here is the most common error.
The program below follows the usual chain: records per block, data blocks, index blocks, then search costs.
import math
def block_costs(B, R, r, V, P):
bfr = B // R
b = math.ceil(r / bfr)
bfi = B // (V + P)
bi = math.ceil(b / bfi)
return {
"bfr": bfr,
"data_blocks": b,
"heap_avg_search": b / 2,
"sorted_binary_search": math.ceil(math.log2(b)),
"primary_index_search": math.ceil(math.log2(bi)) + 1,
}
print(block_costs(B=1024, R=100, r=30000, V=9, P=6))The output has bfr = 10 and 3000 data blocks. The index has 1024 // 15 = 68 entries per block, so a sparse primary index needs ceil(3000/68) = 45 blocks. A heap search averages 1500 accesses. Binary search on the sorted file takes 12. The primary index takes ceil(log2 45) + 1 = 7.
Check these points on every search-cost question:
- A sparse primary index has one entry per data block. A dense index has one entry per record.
- A clustering index has one entry per distinct value of the clustering field.
- A secondary index on a key field is dense, with one entry per record.
- A secondary index on a non-key field usually needs an extra level of pointer blocks.
- A multilevel index takes one access per level, plus one access for the data block.
Pattern 2: order of a tree node
Write the node-size inequality before substituting numbers.
- Internal node of a B+ tree, with p block pointers and p − 1 keys: p·P + (p − 1)·K ≤ B.
- Leaf node of a B+ tree, with pleaf (key, record pointer) pairs and one next-leaf pointer: pleaf·(K + Pr) + P ≤ B.
- B-tree node, with p tree pointers, p − 1 keys and p − 1 record pointers: p·P + (p − 1)·(K + Pr) ≤ B.
def bplus_orders(B, K, P, Pr):
internal_order = (B + K) // (K + P)
leaf_order = (B - P) // (K + Pr)
return internal_order, leaf_order
print(bplus_orders(B=512, K=8, P=6, Pr=7))This prints (37, 33). Check the internal result: 37·6 + 36·8 = 510, which is at most 512, and order 38 needs 516 bytes. The leaf check is 33·15 + 6 = 501, and 34 pairs would need 516.
Pattern 3: capacity and height
Count levels from the root, so a tree with levels = 3 has a root, one internal level and a leaf level. Let m be the maximum number of children of an internal node and L the maximum number of keys in a leaf.
- Maximum records: m^(levels − 1) · L.
- Minimum records, for levels ≥ 2: 2 · ceil(m/2)^(levels − 2) · ceil(L/2). The root has at least 2 children, other internal nodes have at least ceil(m/2) children, and leaves have at least ceil(L/2) keys.
def bplus_capacity(levels, order, leaf_keys):
max_keys = order ** (levels - 1) * leaf_keys
min_children = -(-order // 2)
min_leaf = -(-leaf_keys // 2)
min_keys = 2 * min_children ** (levels - 2) * min_leaf
return max_keys, min_keys
print(bplus_capacity(3, 37, 33))The result is (45177, 646). To find the height for n records, use the minimum-capacity formula for the worst-case height. Use the maximum-capacity formula for the best-case height. A search then costs one access per level, plus one access for the data block.
For a plain B-tree, every node holds records, so you must add the keys of all levels. A root with m children holds m − 1 keys, and each level holds (m − 1) times the number of nodes on it. The total for a full B-tree of order m with h levels is m^h − 1.
Pattern 4: insertion and deletion sequences
Do these by hand and redraw the tree after each key. Keep these rules in view.
- A B+ tree leaf splits when it exceeds its capacity. The smallest key of the new right leaf is copied up into the parent, and it also stays in the leaf.
- An internal node splits when it exceeds m − 1 keys. The middle key moves up and does not stay below.
- A B-tree split moves the median key up in every case, because there are no separate leaf copies.
- Deletion that leaves a node under the minimum is fixed by borrowing from a sibling if it has a spare key. Otherwise the nodes merge and the parent loses a key.
- In a B+ tree, deleting a key that appears as a separator does not require changing the separator. It is still a valid guide.
A split of an odd-sized overflow can go either way: left-heavy or right-heavy. The question usually fixes the rule. If it does not, state your choice and keep it consistent.
Pattern 5: conceptual statements
True/false and multiple-select questions repeat a small set of facts.
- B+ trees store records or record pointers only in leaves, so internal nodes hold more keys and the tree is shorter.
- All B+ tree leaves are at the same depth and are linked, so range queries scan the leaf chain after one descent.
- B-tree search can end at an internal node, so its cost varies. B+ tree search always reaches a leaf.
- A hashed file gives about one access for equality search and no help for range queries.
- A sorted file makes insertion costly because records must shift, so a heap file or overflow area is used.
Strategy checklist
- Write the given sizes and their units at the top of your working.
- Name the pattern and the formula.
- Round at the right steps. Use floor for records per block and for entries per node. Use ceiling for block counts and for logarithms of block counts.
- Sanity-check the result by substituting back into the inequality.
- Add the final data-block access if the question counts accesses to the record.
Exercise
A B+ tree stores keys of 10 bytes, with block pointers of 8 bytes, in blocks of 1024 bytes. What is the largest number of children an internal node can have?
Show answer
Solve p·8 + (p − 1)·10 ≤ 1024, which gives 18p ≤ 1034, so p ≤ 57.4 and p = 57. Check: 57·8 + 56·10 = 1016 ≤ 1024, while p = 58 needs 1034.
Exercise
A B+ tree has internal nodes with at most 4 children, leaves with at most 3 keys, and 3 levels. What are the maximum and minimum numbers of keys it can hold, and how many block accesses does a search need if each key points to a data block?
Show answer
Maximum = 4^2 · 3 = 48. Minimum = 2 · ceil(4/2)^1 · ceil(3/2) = 2 · 2 · 2 = 8. A search reads 3 tree nodes and then 1 data block, so 4 accesses.