Advanced Data Structures MCQs for NSCT
Advanced data structures are specialised structures built for one job each: tries for prefix search on strings, segment trees and Fenwick trees for range queries with updates, disjoint sets (union-find) for grouping, and B-trees for disk-based indexes. They form topic 10 of the Data Structures & Algorithms area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 40.2% accuracy in this area, per the HEC NSCT report and APP.
Revise Non-Linear Data Structures first, because every structure here is a tree underneath.
Tries
A trie (prefix tree) stores strings one character per edge. Words that share a prefix share the path from the root, and an end-of-word flag marks the nodes where a stored word finishes.
Insert car, cart, care and cat. The path c-a-r is shared by three words, t and e hang under r, and a second t hangs under a. With the root that is 7 nodes, against 15 if nothing were shared.
Search and insert cost O(L) for a key of length L, however many keys are stored. The price is memory: a node with a fixed 26-pointer array wastes most slots in a sparse dictionary.
Exam angle: expect node-count questions and the trap that search("car") is false if only cart was inserted, because r is not flagged.
Segment trees
A segment tree is a full binary tree over an array. Each leaf holds one element and each internal node holds the sum (or minimum, or maximum) of its range. With n leaves it has exactly 2n - 1 nodes, though array implementations allocate 4n slots because heap-style indexing leaves gaps.
For A = [3, 1, 4, 1, 5, 9, 2, 6] the root stores 31 and query(2, 5) returns 4 + 1 + 5 + 9 = 19. Setting A[3] to 10 updates the leaf and its 3 ancestors (4 nodes, one per level), and the root becomes 40.
| Structure | Build | Range query | Point update |
|---|---|---|---|
| Plain array | O(1) | O(n) | O(1) |
| Prefix-sum array | O(n) | O(1) | O(n) |
| Segment tree | O(n) | O(log n) | O(log n) |
| Fenwick tree (BIT) | O(n) | O(log n) prefix | O(log n) |
Lazy propagation adds range updates in O(log n) by storing pending tags on nodes until their children are needed. A Fenwick tree gets the same bounds with less memory by moving through indices with i & (-i), the lowest set bit.
Exam angle: pick the structure from the update and query mix. No updates means prefix sums; interleaved updates and queries mean a segment tree or Fenwick tree.
Disjoint sets (union-find)
A disjoint-set forest keeps non-overlapping sets as trees. Find(x) returns the root of x's tree and Union(a, b) links one root under the other.
The linking rule decides everything, so read it carefully in each question. With parent[find(a)] = find(b) and no heuristics, union(0,1), union(1,2), union(3,4), union(2,4) on six elements gives parent = [1, 2, 4, 4, 4, 5]: two sets, with node 0 three links below root 4.
Two heuristics fix tall trees:
- Union by rank (or size) puts the shorter (smaller) tree under the other root, so height stays at most log2 n and Find is O(log n).
- Path compression re-points every node on a Find path straight at the root.
Together they give O(alpha(n)) amortised per operation (Tarjan), where alpha is the inverse Ackermann function, at most 4 for any realistic n.
Exam angle: Kruskal's algorithm uses union-find to reject an edge whose ends already share a root.
B-trees
A B-tree is a balanced search tree whose nodes hold many keys and match a disk block, so the tree stays only a few levels deep. Textbooks define its size differently. CLRS uses the minimum degree t: non-root nodes hold t - 1 to 2t - 1 keys. Knuth-style books use the order m, the maximum number of children, so a node holds at most m - 1 keys. Check which convention a question uses.
Common traps
- Forgetting the root when counting trie nodes, or counting shared prefixes twice.
- Treating a prefix path as a stored word when its end-of-word flag is false.
- Reading 0-indexed segment-tree ranges as 1-indexed.
- Returning 0 for "no overlap" in a minimum segment tree; the neutral value is +infinity.
- Saying union by rank alone gives O(alpha(n)); that bound needs path compression too.
- Mixing up B-tree order m with CLRS minimum degree t.
Worked examples
1. Understanding. Which union-find heuristic attaches the shallower tree under the root of the deeper one? Answer: union by rank. Path compression acts during Find, not Union, by flattening the path it walks.
2. Application. How many nodes does a trie holding sun, sung, sunny and so contain, including the root? Answer: 8. Root, s, u, n are shared; g ends sung; n and y make sunny; o under s makes so. That is 1 + 3 + 1 + 2 + 1 = 8.
3. Analysis. A sum segment tree is built on [2, 7, 1, 8, 2, 8]. query(1, 4) returns 18. After A[2] is set to 5, what do query(1, 4) and the root return? Answer: 22 and 32. The update adds a difference of 4 along one leaf-to-root path, so both values rise by 4 from 18 and 28.
Practise
The quiz below has 100 Advanced Data Structures questions, filterable by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between a trie and a hash table?
Both look up a key of length L in about O(L) time, since a hash table must hash every character. A trie also supports prefix queries, autocomplete and sorted output, which a hash table cannot. A hash table usually uses less memory, so choose it when you only need exact membership tests.
When should I use a Fenwick tree instead of a segment tree?
Use a Fenwick tree for prefix sums, or any invertible operation, with point updates. It needs only n + 1 cells and very short code. Use a segment tree for minimum or maximum queries, for operations without an inverse, or when you need range updates with lazy propagation.
Why does union-find need both union by rank and path compression?
Either one alone helps: union by rank caps height at log2 n, and path compression flattens paths as they are used. Together they give the O(alpha(n)) amortised bound, which is effectively constant. Without either, a run of unions can build a chain and make each Find O(n).
Next step
Continue with String Algorithms MCQs, go back to the DSA MCQ hub, or test yourself across every area in an NSCT mock test.