Advanced Data Structures MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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:

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. What is the main advantage of a trie over a balanced binary search tree for storing a dictionary of strings?

    1. It always uses less memory than a binary search tree
    2. Lookup time depends on the length of the key, not on the number of stored keys
    3. It keeps keys sorted by length rather than alphabetically
    4. It supports deleting a key in O(1) time regardless of key length
    Show answer

    Answer: B. Lookup time depends on the length of the key, not on the number of stored keys

    A trie walks one node per character, so searching for a key of length L costs O(L) no matter how many keys are stored. A balanced BST needs O(log n) string comparisons, each of which can itself cost up to O(L). Tries often use more memory, not less.

  2. Which two operations define the disjoint-set (union-find) data structure?

    1. Push and Pop, which add and remove the most recent element
    2. Insert and ExtractMin, which maintain the smallest key
    3. Enqueue and Dequeue, which process elements in arrival order
    4. Find, which returns a set's representative, and Union, which merges two sets
    Show answer

    Answer: D. Find, which returns a set's representative, and Union, which merges two sets

    A disjoint-set structure keeps a collection of non-overlapping sets. Find(x) returns the representative (root) of the set containing x, and Union(a, b) merges the sets containing a and b. Push/Pop describe a stack, Insert/ExtractMin a priority queue and Enqueue/Dequeue a queue.

  3. For a segment tree built over n array elements, what are the time complexities of a range-sum query and a single-element update?

    1. O(log n) for the query and O(log n) for the update
    2. O(1) for the query and O(n) for the update
    3. O(n) for the query and O(1) for the update
    4. O(n log n) for the query and O(log n) for the update
    Show answer

    Answer: A. O(log n) for the query and O(log n) for the update

    A segment tree has height about log2 n. A range query combines O(log n) stored segments, and a point update changes one leaf and then each ancestor on the path to the root, which is also O(log n). O(1) query with O(n) update describes a prefix-sum array.

  4. Why does each trie node usually carry an end-of-word flag?

    1. To mark the node where the alphabet array runs out of space
    2. To record how many times the node has been visited during searches
    3. To show that the node has no children and can be deleted
    4. To tell a stored word apart from a prefix of a longer stored word, such as "car" inside "cart"
    Show answer

    Answer: D. To tell a stored word apart from a prefix of a longer stored word, such as "car" inside "cart"

    If both "car" and "cart" are stored, the path c-a-r is shared. Without a flag on the r node you could not tell whether "car" itself was inserted or is only a prefix of "cart". A node can have children and still be a word end, so the flag is not the same as being a leaf.

  5. With both union by rank and path compression, what is the amortised time per operation on a disjoint-set forest of n elements, as proved by Tarjan?

    1. O(log n), the height bound of a rank-balanced tree
    2. O(1) in the worst case for every single operation
    3. O(sqrt(n)), from splitting the forest into blocks
    4. O(alpha(n)), where alpha is the inverse Ackermann function
    Show answer

    Answer: D. O(alpha(n)), where alpha is the inverse Ackermann function

    Tarjan showed that m operations on n elements take O(m alpha(n)) time with both heuristics, so the amortised cost is O(alpha(n)). The inverse Ackermann function grows so slowly that it is at most 4 for any practical n, but it is not a constant worst-case bound per operation. O(log n) is the bound with union by rank alone.

  6. An array never changes after it is loaded, and the program must answer millions of range-sum queries. Which structure gives O(1) per query with the least code?

    1. A segment tree with lazy propagation
    2. A trie keyed on the array indices
    3. A prefix-sum array, where sum(l..r) = P[r+1] - P[l]
    4. A disjoint-set forest over the indices
    Show answer

    Answer: C. A prefix-sum array, where sum(l..r) = P[r+1] - P[l]

    With no updates, precompute P[i] = a[0] + ... + a[i-1] once in O(n). Any range sum is then P[r+1] - P[l] in O(1). A segment tree also works but answers in O(log n) and is only worth it when updates are mixed with queries.

  7. What does lazy propagation add to a segment tree?

    1. It delays building the tree until the first query, so construction costs O(1)
    2. It stores only the leaves and recomputes internal nodes on every query
    3. It postpones pushing a range update into child nodes until they are visited, so a range update costs O(log n)
    4. It lets the tree answer queries in O(1) by caching every possible range
    Show answer

    Answer: C. It postpones pushing a range update into child nodes until they are visited, so a range update costs O(log n)

    Without lazy propagation, adding a value to every element of a range touches every affected leaf, which can be O(n). With it, a node fully inside the range records a pending update tag and stops; the tag is pushed down only when a later operation needs the children. Range updates and range queries both become O(log n).

  8. Why are B-trees (and B+ trees) the standard index structure in databases and file systems?

    1. They store keys in hash buckets, so every search is O(1)
    2. Each node holds many keys and fits a disk block, so the tree is very shallow and a search needs few disk reads
    3. They are binary trees, which keeps each node as small as possible in memory
    4. They never need to split or merge nodes, so inserts are cheap
    Show answer

    Answer: B. Each node holds many keys and fits a disk block, so the tree is very shallow and a search needs few disk reads

    A B-tree node is sized to a disk page and can have hundreds of children. That high branching factor keeps the height tiny, often 3 or 4 levels for millions of keys, and each level costs one disk access. Nodes do split and merge to stay balanced, and B-trees are not hash tables.

  9. In a B-tree of minimum degree t (the CLRS definition), every node except the root has at least t - 1 keys. What is the maximum number of keys a node may hold?

    1. 2t - 1
    2. 2t
    3. t
    4. 2t + 1
    Show answer

    Answer: A. 2t - 1

    CLRS defines a B-tree of minimum degree t >= 2 so that each non-root node has between t - 1 and 2t - 1 keys, and therefore between t and 2t children. A node with 2t - 1 keys is full and must split before another key is inserted. Other books define order m as the maximum number of children instead, so always check which convention a question uses.

  10. Kruskal's minimum spanning tree algorithm needs to check quickly whether adding an edge would create a cycle. Which data structure does it use?

    1. A trie
    2. A segment tree
    3. A stack
    4. A disjoint-set (union-find) structure
    Show answer

    Answer: D. A disjoint-set (union-find) structure

    Kruskal adds edges in increasing weight order and keeps each connected component as a set. An edge (u, v) creates a cycle exactly when Find(u) equals Find(v); otherwise Union merges the two components. Union-find makes each check nearly constant time.

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.