Bird
Raised Fist0
DSA C#programming~20 mins

Why Trees Exist and What Linked Lists and Arrays Cannot Do in DSA C# - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tree Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Tree Traversal vs Linked List Traversal
Consider the following C# code that creates a simple binary tree and a linked list with the same values. What will be the printed output after traversing both structures?
DSA C#
using System;

class Node {
    public int Value;
    public Node Left, Right;
    public Node(int val) { Value = val; Left = Right = null; }
}

class ListNode {
    public int Value;
    public ListNode Next;
    public ListNode(int val) { Value = val; Next = null; }
}

class Program {
    static void InOrder(Node root) {
        if (root == null) return;
        InOrder(root.Left);
        Console.Write(root.Value + " ");
        InOrder(root.Right);
    }

    static void TraverseList(ListNode head) {
        var current = head;
        while (current != null) {
            Console.Write(current.Value + " ");
            current = current.Next;
        }
    }

    static void Main() {
        // Binary tree:
        //      2
        //     / \
        //    1   3
        Node root = new Node(2);
        root.Left = new Node(1);
        root.Right = new Node(3);

        // Linked list: 1 -> 2 -> 3
        ListNode head = new ListNode(1);
        head.Next = new ListNode(2);
        head.Next.Next = new ListNode(3);

        InOrder(root);
        Console.WriteLine();
        TraverseList(head);
    }
}
A
1 2 3 
1 2 3 
B
1 3 2 
1 2 3 
C
2 1 3 
1 2 3 
D
1 2 3 
3 2 1 
Attempts:
2 left
💡 Hint
Think about how in-order traversal of a binary search tree prints values in sorted order, and how linked list traversal prints values in insertion order.
🧠 Conceptual
intermediate
1:30remaining
Why Trees Are Better for Hierarchical Data
Why are trees preferred over arrays or linked lists for representing hierarchical data like file systems?
ABecause trees use less memory than arrays and linked lists.
BBecause trees allow multiple branches from one node, representing parent-child relationships naturally.
CBecause linked lists cannot store data, only pointers.
DBecause arrays can only store numbers, not hierarchical data.
Attempts:
2 left
💡 Hint
Think about how many children a node can have in each data structure.
🔧 Debug
advanced
2:00remaining
Identify the Problem in Tree Search Code
What error or issue will occur when running the following C# code that searches for a value in a binary tree?
DSA C#
class Node {
    public int Value;
    public Node Left, Right;
    public Node(int val) { Value = val; Left = Right = null; }
}

bool Search(Node root, int target) {
    if (root == null) return false;
    if (root.Value == target) return true;
    if (target < root.Value) return Search(root.Left, target);
    else return Search(root.Right, target);
}
AThe function returns true for all inputs regardless of the tree.
BThe function causes a stack overflow due to infinite recursion.
CThe function always returns false unless the root node matches the target.
DThe function throws a null reference exception.
Attempts:
2 left
💡 Hint
Check if recursive calls' results are returned properly.
❓ Predict Output
advanced
1:30remaining
Output of Tree Height Calculation
What is the output of the following C# code that calculates the height of a binary tree?
DSA C#
using System;

class Node {
    public int Value;
    public Node Left, Right;
    public Node(int val) { Value = val; Left = Right = null; }
}

class Program {
    static int Height(Node root) {
        if (root == null) return 0;
        int leftHeight = Height(root.Left);
        int rightHeight = Height(root.Right);
        return Math.Max(leftHeight, rightHeight) + 1;
    }

    static void Main() {
        Node root = new Node(10);
        root.Left = new Node(5);
        root.Right = new Node(15);
        root.Left.Left = new Node(3);
        root.Left.Left.Left = new Node(1);
        Console.WriteLine(Height(root));
    }
}
A2
B3
C5
D4
Attempts:
2 left
💡 Hint
Count the longest path from root to leaf node.
🧠 Conceptual
expert
2:30remaining
Why Arrays and Linked Lists Cannot Efficiently Represent Hierarchies
Which of the following best explains why arrays and linked lists are inefficient for representing hierarchical data compared to trees?
AArrays and linked lists are linear structures, so they cannot naturally represent multiple branches from a single node without complex indexing or pointers.
BArrays and linked lists cannot store data types other than integers, limiting their use for hierarchies.
CTrees use less memory than arrays and linked lists, making them more efficient for all data.
DArrays and linked lists do not support recursion, which is needed for hierarchical data.
Attempts:
2 left
💡 Hint
Think about the shape and connections of data in hierarchies.

Practice

(1/5)
1. Why do trees exist as a data structure when we already have arrays and linked lists?
easy
A. Because trees are easier to implement than arrays and linked lists
B. Because trees use less memory than arrays and linked lists
C. Because trees always provide faster access to elements than arrays and linked lists
D. Because trees allow hierarchical data representation which arrays and linked lists cannot efficiently handle

Solution

  1. Step 1: Understand the limitation of arrays and linked lists

    Arrays and linked lists store data linearly, which makes representing hierarchical relationships difficult and inefficient.
  2. Step 2: Recognize the advantage of trees

    Trees naturally represent hierarchical data, such as file systems or organizational charts, allowing efficient traversal and management of such structures.
  3. Final Answer:

    Trees allow hierarchical data representation which arrays and linked lists cannot efficiently handle -> Option D
  4. Quick Check:

    Hierarchical data needs trees [OK]
Hint: Trees handle hierarchy; arrays and lists are linear [OK]
Common Mistakes:
  • Thinking trees always use less memory
  • Assuming trees are simpler to implement
  • Believing trees always have faster access
2. Which of the following is the correct way to declare a simple binary tree node class in C#?
easy
A. class TreeNode { public int Value; public TreeNode Left, Right; private TreeNode Parent; }
B. public class TreeNode { int Value; int Left; int Right; }
C. public class TreeNode { public int Value; public TreeNode Left; public TreeNode Right; }
D. public struct TreeNode { public int Value; public TreeNode Left; public TreeNode Right; }

Solution

  1. Step 1: Check for correct access modifiers and types

    public class TreeNode { public int Value; public TreeNode Left; public TreeNode Right; } uses public fields and correct types for left and right child nodes, which is standard for a binary tree node.
  2. Step 2: Identify issues in other options

    public class TreeNode { int Value; int Left; int Right; } uses int for Left and Right which is incorrect; class TreeNode { public int Value; public TreeNode Left, Right; private TreeNode Parent; } has a private Parent which is not necessary here; public struct TreeNode { public int Value; public TreeNode Left; public TreeNode Right; } uses struct which is possible but less common and can cause issues with references.
  3. Final Answer:

    public class TreeNode { public int Value; public TreeNode Left; public TreeNode Right; } -> Option C
  4. Quick Check:

    Correct class with public TreeNode references [OK]
Hint: Tree nodes link to other nodes, not ints [OK]
Common Mistakes:
  • Using int instead of TreeNode for children
  • Making fields private without accessors
  • Using struct instead of class for nodes
3. What will be the output of the following C# code snippet?
class TreeNode {
  public int Value;
  public TreeNode Left, Right;
  public TreeNode(int val) { Value = val; }
}

var root = new TreeNode(10);
root.Left = new TreeNode(5);
root.Right = new TreeNode(15);
Console.WriteLine(root.Left.Value + ", " + root.Right.Value);
medium
A. 5, 15
B. 10, 15
C. 5, 10
D. Compilation error

Solution

  1. Step 1: Understand the tree structure created

    The root node has value 10, with left child 5 and right child 15.
  2. Step 2: Trace the Console.WriteLine statement

    It prints the left child's value (5) and right child's value (15) separated by a comma.
  3. Final Answer:

    5, 15 -> Option A
  4. Quick Check:

    Print left and right child values [OK]
Hint: Print left and right node values directly [OK]
Common Mistakes:
  • Confusing root value with child values
  • Expecting output order as right then left
  • Assuming code has errors
4. The following code attempts to create a binary tree node but has an error. What is the error?
public class TreeNode {
  public int Value;
  public TreeNode Left, Right;
  public TreeNode(int val) {
    Value = val;
    Left = null;
    Right = null;
  }
}

var node = new TreeNode();
medium
A. No error, code runs fine
B. Constructor requires an int parameter but none provided
C. Left and Right should be initialized outside constructor
D. TreeNode cannot have null children

Solution

  1. Step 1: Check the constructor definition

    The constructor requires an int parameter 'val' to initialize Value.
  2. Step 2: Analyze the object creation

    The code calls new TreeNode() without any arguments, causing a compilation error.
  3. Final Answer:

    Constructor requires an int parameter but none provided -> Option B
  4. Quick Check:

    Constructor parameters must match call [OK]
Hint: Match constructor parameters when creating objects [OK]
Common Mistakes:
  • Ignoring constructor parameters
  • Thinking null children cause error
  • Assuming default constructor exists
5. You want to represent a company's organizational chart where each employee can have multiple subordinates. Which data structure is best suited and why?
hard
A. Tree, because it naturally models hierarchical relationships with multiple children
B. Array, because it stores elements sequentially
C. Linked list, because it allows dynamic insertion
D. Stack, because it follows last-in-first-out order

Solution

  1. Step 1: Understand the problem context

    An organizational chart is hierarchical, with employees having multiple subordinates (children).
  2. Step 2: Match data structure to hierarchy

    Trees can represent hierarchical data with nodes having multiple children, unlike arrays or linked lists which are linear.
  3. Step 3: Eliminate unsuitable options

    Arrays and linked lists do not model hierarchy well; stacks are for LIFO order, not hierarchy.
  4. Final Answer:

    Tree, because it naturally models hierarchical relationships with multiple children -> Option A
  5. Quick Check:

    Hierarchy needs trees [OK]
Hint: Hierarchy with multiple children means tree [OK]
Common Mistakes:
  • Choosing arrays or lists for hierarchy
  • Confusing stack usage
  • Ignoring multiple children requirement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard