Challenge - 5 Problems
Hierarchy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tree Traversal in Rust
What is the output of this Rust code that performs a preorder traversal on a simple binary tree?
DSA Rust
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn preorder(&self, result: &mut Vec<i32>) {
result.push(self.value);
if let Some(ref left) = self.left {
left.preorder(result);
}
if let Some(ref right) = self.right {
right.preorder(result);
}
}
}
fn main() {
let tree = Node {
value: 1,
left: Some(Box::new(Node {
value: 2,
left: None,
right: None,
})),
right: Some(Box::new(Node {
value: 3,
left: None,
right: None,
})),
};
let mut result = Vec::new();
tree.preorder(&mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Preorder traversal visits the root first, then left subtree, then right subtree.
✗ Incorrect
The preorder traversal visits nodes in the order: root, left child, right child. So the output is [1, 2, 3].
🧠 Conceptual
intermediateHierarchy Representation: Which Data Structure?
You need to represent a company's organizational chart where each employee can have multiple subordinates. Which data structure best models this hierarchy?
Attempts:
2 left
💡 Hint
Think about a structure that naturally represents parent-child relationships with multiple children.
✗ Incorrect
A tree structure naturally models hierarchical data with multiple children per node, like an organizational chart.
🔧 Debug
advancedWhy Does This Linked List Code Cause a Runtime Error?
Consider this Rust code snippet for a singly linked list insertion. Why does it cause a runtime error?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn insert_at_head(head: &mut Option<Box<Node>>, val: i32) {
let new_node = Box::new(Node { value: val, next: head.take() });
*head = Some(new_node);
}
fn main() {
let mut head = None;
insert_at_head(&mut head, 10);
insert_at_head(&mut head, 20);
println!("{}", head.unwrap().next.unwrap().value);
}Attempts:
2 left
💡 Hint
Check what unwrap() is called on and the list structure after insertions.
✗ Incorrect
The list after two insertions has head value 20, and next node value 10. So unwrap().next.unwrap().value prints 10.
❓ Predict Output
advancedOutput of Array vs Linked List Insertions
What is the final state of the data structure after these operations? The data structure starts empty.
DSA Rust
let mut arr = vec![1, 2, 3]; arr.insert(1, 10); let mut list = std::collections::LinkedList::new(); list.push_back(1); list.push_back(2); list.push_back(3); // list.insert_after = None; // (Note: This line is invalid and will cause error) // Instead, assume we insert 10 after first element manually // Final list elements: 1 -> 10 -> 2 -> 3
Attempts:
2 left
💡 Hint
Array insert shifts elements right; linked list insertion requires node reference.
✗ Incorrect
Array insert at index 1 places 10 between 1 and 2. Linked list insertion after first element results in 1 -> 10 -> 2 -> 3.
🧠 Conceptual
expertChoosing Data Structure for Dynamic Hierarchical Data
You must design a system to store a dynamic hierarchy where nodes can be added, removed, and moved frequently, and fast traversal is needed. Which data structure is best suited?
Attempts:
2 left
💡 Hint
Consider flexibility, fast lookup, and dynamic updates.
✗ Incorrect
A HashMap keyed by node IDs with child lists allows fast lookup, easy addition/removal, and flexible hierarchy changes.
