What is the main difference between a tree and an array when representing data?
A tree shows hierarchical relationships with parent and child nodes, while an array stores elements in a flat, linear order without hierarchy.
Click to reveal answer
beginner
Why might a linked list be less suitable than a tree for hierarchical data?
A linked list is a linear structure where each element points to the next, so it cannot naturally represent branching or multiple levels like a tree can.
Click to reveal answer
beginner
In what scenario is an array preferred over a tree or linked list?
When data is simple, ordered, and hierarchy does not matter, arrays are preferred for fast access by index and simple storage.
Click to reveal answer
intermediate
How does a tree structure help when hierarchy matters in data?
A tree allows each node to have multiple children, representing complex relationships and levels, making it ideal for hierarchical data like file systems or organization charts.
Click to reveal answer
intermediate
What is a key limitation of arrays when trying to represent hierarchical data?
Arrays cannot directly represent parent-child relationships or branching; they only store elements in a sequence, so extra logic is needed to simulate hierarchy.
Click to reveal answer
Which data structure naturally represents hierarchical relationships?
AArray
BTree
CLinked List
DStack
✗ Incorrect
Trees have nodes with parent-child links, making them ideal for hierarchy.
What is a main disadvantage of using a linked list for hierarchical data?
AIt has slow access by index
BIt uses too much memory
CIt only supports linear connections
DIt cannot store data
✗ Incorrect
Linked lists connect nodes in a single line, so they can't represent branches.
When is an array the best choice?
AWhen data is hierarchical
BWhen data changes frequently
CWhen data has many branches
DWhen fast indexed access is needed
✗ Incorrect
Arrays provide fast access by index but do not represent hierarchy.
Which structure allows nodes to have multiple children?
ATree
BLinked List
CArray
DQueue
✗ Incorrect
Trees allow each node to have multiple child nodes.
What extra work is needed to represent hierarchy in an array?
AUse indexes or pointers to simulate parent-child links
BNo extra work needed
CConvert array to linked list
DSort the array
✗ Incorrect
Arrays need additional logic like indexes or pointers to simulate hierarchy.
Explain how trees, arrays, and linked lists differ when representing hierarchical data.
Think about how each structure connects elements.
You got /5 concepts.
Describe a real-life example where a tree is better than an array or linked list.
Consider structures with branching paths.
You got /4 concepts.
Practice
(1/5)
1. Which data structure is best to represent a hierarchy where each element can have multiple children, like a company organizational chart?
easy
A. Stack
B. Array
C. Linked List
D. Tree
Solution
Step 1: Understand hierarchy representation
A hierarchy with multiple children per element needs a structure that can link one parent to many children.
Step 2: Compare data structures
Arrays and linked lists are linear and do not naturally represent multiple children. Trees allow each node to have multiple children, fitting the hierarchy.
Final Answer:
Tree -> Option D
Quick Check:
Hierarchy with multiple children = Tree [OK]
Hint: Use trees for multi-child hierarchies, arrays for flat data [OK]
Common Mistakes:
Choosing array for hierarchical data
Using linked list which is linear
Confusing stack with hierarchy
2. Which of the following Rust code snippets correctly declares a linked list node struct with a value and a next pointer?
easy
A. struct Node { value: i32, next: Option }
B. struct Node { value: i32, next: Node }
C. struct Node { value: i32, next: &Node }
D. struct Node { value: i32, next: Vec<Node> }
Solution
Step 1: Understand Rust linked list node
Linked list nodes need a pointer to the next node. Rust uses Option for nullable heap pointers.
Step 2: Check each option
A uses direct Node which causes infinite size error. C uses reference without lifetime. D uses Vec which is for multiple children, not linked list.
Recursive types in Rust must use Option to allow end of list (None).
Step 2: Check code for missing Option
Code uses Box<Node> without Option, so no way to represent end (None), causing compile error.
Final Answer:
Missing Option for next pointer to allow None -> Option B
Quick Check:
Recursive next pointer needs Option [OK]
Hint: Use Option for nullable next in linked list [OK]
Common Mistakes:
Thinking Box alone handles recursion
Blaming i32 type
Assuming syntax error in struct
5. You want to represent a file system hierarchy in Rust where folders can contain files and other folders. Which data structure and Rust type combination is best suited for this?
hard
A. Array of strings
B. Linked list of file names
C. Tree with enum Node { File(String), Folder(Vec<Node>) }
D. HashMap with file paths as keys
Solution
Step 1: Understand file system hierarchy
Folders can contain files and other folders, so a recursive structure is needed.
Step 2: Evaluate data structure options
Array or linked list can't represent nested folders well. HashMap stores flat keys, not hierarchy. Tree with enum Node allows files and folders recursively.
Final Answer:
Tree with enum Node { File(String), Folder(Vec<Node>) } -> Option C
Quick Check:
Recursive enum tree fits file system hierarchy [OK]
Hint: Use recursive enum tree for nested folders and files [OK]
Common Mistakes:
Using flat arrays or lists for nested folders
Using HashMap which is flat key-value
Not using enum to distinguish files and folders
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.