Challenge - 5 Problems
Hierarchy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Traversing a Simple Tree Structure
Consider the following JavaScript code that represents a simple tree using nested objects. What will be the output when we print the names in preorder traversal?
DSA Javascript
const tree = {
name: 'root',
children: [
{ name: 'child1', children: [] },
{ name: 'child2', children: [
{ name: 'grandchild1', children: [] }
] }
]
};
function preorder(node) {
let result = [];
result.push(node.name);
for (const child of node.children) {
result = result.concat(preorder(child));
}
return result;
}
console.log(preorder(tree).join(' -> ') + ' -> null');Attempts:
2 left
💡 Hint
Think about visiting the root first, then each child from left to right.
✗ Incorrect
The preorder traversal visits the root node first, then recursively visits each child in order. So the order is root, child1, child2, then grandchild1.
❓ Predict Output
intermediate2:00remaining
Linked List vs Array for Hierarchical Data
Given a linked list and an array representing the same sequence of nodes, which output correctly shows the linked list traversal?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } const head = new Node('A'); head.next = new Node('B'); head.next.next = new Node('C'); let current = head; let output = ''; while (current) { output += current.value + ' -> '; current = current.next; } output += 'null'; console.log(output);
Attempts:
2 left
💡 Hint
Linked list traversal prints nodes followed by arrows ending with null.
✗ Incorrect
The linked list traversal prints each node's value followed by '->' and ends with 'null'. Arrays print differently.
🧠 Conceptual
advanced2:00remaining
Why Use Trees Over Arrays for Hierarchical Data?
Which of the following is the best reason to use a tree data structure instead of a simple array when representing hierarchical data?
Attempts:
2 left
💡 Hint
Think about how hierarchy and relationships are stored.
✗ Incorrect
Trees naturally represent parent-child relationships, which arrays cannot do directly. Arrays are linear and do not capture hierarchy.
🔧 Debug
advanced2:00remaining
Identify the Error in Linked List Insertion Code
What error will the following code produce when trying to insert a new node at the end of a linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function insertAtEnd(head, value) { let newNode = new Node(value); let current = head; while (current.next) { current = current.next; } current.next = newNode; return head; } let head = null; head = insertAtEnd(head, 'A');
Attempts:
2 left
💡 Hint
Check what happens when head is null at the start.
✗ Incorrect
The code assumes head is not null and tries to access current.next, but current is null initially, causing a TypeError.
🚀 Application
expert3:00remaining
Choosing Data Structure for File System Hierarchy
You need to design a data structure to represent a file system hierarchy where folders can contain files and other folders. Which data structure is the best choice and why?
Attempts:
2 left
💡 Hint
Think about how folders contain other folders and files inside them.
✗ Incorrect
A tree structure is best for file systems because it models the nested hierarchy of folders and files with parent-child links.