Challenge - 5 Problems
Hierarchy Mastery Badge
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
What is the output of the following TypeScript code that traverses a simple tree and prints node values in preorder?
DSA Typescript
class TreeNode { value: number; children: TreeNode[]; constructor(value: number) { this.value = value; this.children = []; } } const root = new TreeNode(1); const child1 = new TreeNode(2); const child2 = new TreeNode(3); root.children.push(child1, child2); child1.children.push(new TreeNode(4)); function preorder(node: TreeNode | null, result: number[] = []): number[] { if (!node) return result; result.push(node.value); for (const child of node.children) { preorder(child, result); } return result; } console.log(preorder(root).join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Preorder traversal visits the current node before its children, and children are visited in the order they appear.
✗ Incorrect
The preorder traversal visits root first (1), then its first child (2), then 2's child (4), then the second child of root (3).
🧠 Conceptual
intermediate1:30remaining
Choosing Data Structure for Hierarchical Data
Which data structure is best suited to represent a company's organizational hierarchy where each employee can have multiple subordinates?
Attempts:
2 left
💡 Hint
Think about how to represent multiple levels and branches of employees.
✗ Incorrect
A tree structure is ideal for hierarchical data because each node can have multiple children representing subordinates.
🔧 Debug
advanced2:00remaining
Identify the Error in Linked List Node Insertion
What error will occur when running this TypeScript code that tries to insert a node at the head of a singly linked list?
DSA Typescript
class ListNode { value: number; next: ListNode | null; constructor(value: number) { this.value = value; this.next = null; } } let head: ListNode | null = null; function insertAtHead(value: number) { const newNode = new ListNode(value); newNode.next = head; head = newNode; } insertAtHead(10); insertAtHead(20); console.log(head.next.value);
Attempts:
2 left
💡 Hint
Trace the insertions and what head.next points to after two insertions.
✗ Incorrect
After inserting 10 then 20 at head, head points to 20, head.next points to 10, so head.next.value is 10.
❓ Predict Output
advanced2:00remaining
Output of Flattening a Tree into an Array
What is the output of this TypeScript code that flattens a tree into an array using depth-first search?
DSA Typescript
class TreeNode { value: number; children: TreeNode[]; constructor(value: number) { this.value = value; this.children = []; } } const root = new TreeNode(1); const c1 = new TreeNode(2); const c2 = new TreeNode(3); const c3 = new TreeNode(4); root.children.push(c1, c2); c1.children.push(c3); function flattenTree(node: TreeNode | null, arr: number[] = []): number[] { if (!node) return arr; arr.push(node.value); for (const child of node.children) { flattenTree(child, arr); } return arr; } console.log(flattenTree(root));
Attempts:
2 left
💡 Hint
Depth-first search visits node before children recursively.
✗ Incorrect
The flattenTree function visits root (1), then child 2, then 2's child 4, then child 3.
🧠 Conceptual
expert1:30remaining
Why Not Use Array for Deep Hierarchical Data?
Why is using a simple array a poor choice to represent deeply nested hierarchical data compared to a tree or linked list?
Attempts:
2 left
💡 Hint
Think about how hierarchy needs explicit parent-child links.
✗ Incorrect
Arrays store elements linearly and lack direct links to represent nested parent-child relationships, making hierarchy representation complex.