Challenge - 5 Problems
Hierarchy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Traversing a Simple Tree Structure
Consider this Java code that builds a simple tree and prints node values in preorder (root, left, right). What is the printed output?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; } } public class Main { public static void preorder(Node root) { if (root == null) return; System.out.print(root.val + " "); preorder(root.left); preorder(root.right); } public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); preorder(root); } }
Attempts:
2 left
💡 Hint
Preorder traversal visits root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal prints the root node first, then recursively visits the left subtree, then the right subtree. So the order is 1 (root), then 2 (left child), then 4 (left-left child), then 5 (left-right child), then 3 (right child).
🧠 Conceptual
intermediateChoosing Data Structure for Hierarchical Data
Which data structure is best suited to represent hierarchical data with parent-child relationships and efficient traversal?
Attempts:
2 left
💡 Hint
Think about how hierarchy and multiple children per node are represented.
✗ Incorrect
Trees naturally represent hierarchical data with nodes connected to multiple children. Arrays and linked lists are linear and do not represent hierarchy well. Stacks are for LIFO order, not hierarchy.
❓ Predict Output
advancedOutput of Linked List vs Array Representation
Given these two Java snippets representing the same sequence of values, what is the output after adding 10 at the end?
DSA Java
/* Linked List version */ class Node { int val; Node next; Node(int v) { val = v; } } public class Main { public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); Node curr = head; while (curr.next != null) curr = curr.next; curr.next = new Node(10); curr = head; while (curr != null) { System.out.print(curr.val + " "); curr = curr.next; } } } /* Array version */ public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3}; int[] newArr = new int[arr.length + 1]; for (int i = 0; i < arr.length; i++) newArr[i] = arr[i]; newArr[newArr.length - 1] = 10; for (int v : newArr) System.out.print(v + " "); } }
Attempts:
2 left
💡 Hint
Both add 10 at the end of the sequence.
✗ Incorrect
Both linked list and array versions add 10 at the end. The output prints the sequence with 10 appended last.
🔧 Debug
advancedIdentify the Error in Tree Node Insertion
This Java code tries to insert a new node as the left child of the root but causes a runtime error. What is the error?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; } } public class Main { public static void main(String[] args) { Node root = null; root.left = new Node(5); System.out.println(root.left.val); } }
Attempts:
2 left
💡 Hint
Check if root is initialized before accessing its fields.
✗ Incorrect
root is null, so accessing root.left causes NullPointerException at runtime.
🧠 Conceptual
expertHierarchy Representation Efficiency
Which data structure provides the most efficient way to represent a hierarchy with frequent insertions and deletions of nodes at arbitrary positions?
Attempts:
2 left
💡 Hint
Consider how hierarchy and arbitrary insertions/deletions are handled.
✗ Incorrect
Trees with parent and child pointers allow efficient insertions and deletions anywhere in the hierarchy, preserving structure. Arrays are fixed size and costly to resize. Linked lists are linear and do not represent hierarchy well.
