Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Tree vs Array vs Linked List When Hierarchy Matters in DSA Typescript - Compare & Choose

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hierarchy Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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');
A1 -> 3 -> 2 -> 4 -> null
B1 -> 2 -> 4 -> 3 -> null
C4 -> 2 -> 3 -> 1 -> null
D1 -> 2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Preorder traversal visits the current node before its children, and children are visited in the order they appear.
🧠 Conceptual
intermediate
1: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?
ATree, because it naturally represents hierarchical relationships with parent and multiple children
BArray, because it stores elements in order and allows fast access by index
CLinked List, because it allows easy insertion and deletion of employees in a sequence
DStack, because it manages elements in last-in-first-out order
Attempts:
2 left
💡 Hint
Think about how to represent multiple levels and branches of employees.
🔧 Debug
advanced
2: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);
A20
Bundefined
C10
DTypeError: Cannot read property 'value' of null
Attempts:
2 left
💡 Hint
Trace the insertions and what head.next points to after two insertions.
❓ Predict Output
advanced
2: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));
A[1, 2, 4, 3]
B[1, 3, 2, 4]
C[4, 2, 3, 1]
D[2, 4, 1, 3]
Attempts:
2 left
💡 Hint
Depth-first search visits node before children recursively.
🧠 Conceptual
expert
1: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?
AArrays cannot store multiple data types, so they are unsuitable for hierarchy
BArrays automatically sort elements, which breaks hierarchical order
CArrays are slower than trees for all operations, so they are inefficient
DArrays do not naturally represent parent-child relationships and require complex indexing to simulate hierarchy
Attempts:
2 left
💡 Hint
Think about how hierarchy needs explicit parent-child links.

Practice

(1/5)
1. Which data structure is best suited to represent hierarchical relationships like a family tree or company organization?
easy
A. Tree
B. Array
C. Linked List
D. Stack

Solution

  1. Step 1: Understand hierarchy representation

    Hierarchies have parent-child relationships, which are naturally modeled by trees.
  2. Step 2: Compare data structures

    Arrays and linked lists are linear and do not represent branching well, unlike trees.
  3. Final Answer:

    Tree -> Option A
  4. Quick Check:

    Hierarchy = Tree [OK]
Hint: Hierarchy means branching, so choose Tree [OK]
Common Mistakes:
  • Choosing Array because it's simple
  • Choosing Linked List thinking it can represent hierarchy
  • Confusing Stack with hierarchical structure
2. Which of the following is the correct TypeScript syntax to declare a simple linked list node with a value and a next pointer?
easy
A. class Node { value: number; next: Node; constructor(value: number) { this.value = value; this.next = null; } }
B. class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } }
C. class Node { value: number; next: null; constructor(value: number) { this.value = value; this.next = null; } }
D. class Node { value: number; next: any; constructor(value: number) { this.value = value; this.next = undefined; } }

Solution

  1. Step 1: Check next pointer type

    Next pointer should allow either another Node or null to mark list end.
  2. Step 2: Verify constructor initialization

    Constructor sets value and initializes next to null correctly.
  3. Final Answer:

    class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } -> Option B
  4. Quick Check:

    Next pointer = Node or null [OK]
Hint: Next pointer must allow null for list end [OK]
Common Mistakes:
  • Not allowing null for next pointer
  • Initializing next as undefined instead of null
  • Using any type which loses type safety
3. Given the following TypeScript code, what will be the printed output?
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);
console.log(root.children.map(c => c.value));
medium
A. [1, 2, 3]
B. Error: children is not iterable
C. [[2], [3]]
D. [2, 3]

Solution

  1. Step 1: Understand tree structure

    Root node has value 1 and two children with values 2 and 3.
  2. Step 2: Analyze console.log output

    Mapping children array extracts values [2, 3], root's value 1 is not included.
  3. Final Answer:

    [2, 3] -> Option D
  4. Quick Check:

    Children values = [2, 3] [OK]
Hint: Mapping children prints their values, not root's [OK]
Common Mistakes:
  • Including root value in output
  • Expecting nested arrays instead of flat array
  • Assuming children is not iterable
4. Identify the bug in this TypeScript code that tries to create a linked list and print its values:
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);

let current = head;
while (current !== null) {
  console.log(current.value);
  current = current.next.next;
}
medium
A. Using console.log inside loop is invalid
B. Incorrect constructor syntax
C. Accessing next.next without checking if next is null
D. Node class missing a previous pointer

Solution

  1. Step 1: Trace the while loop iteration

    Loop moves current by current.next.next, skipping nodes and risking null access.
  2. Step 2: Identify null pointer risk

    When current.next is null, accessing current.next.next causes runtime error.
  3. Final Answer:

    Accessing next.next without checking if next is null -> Option C
  4. Quick Check:

    Null check missing before next.next access [OK]
Hint: Always check next before accessing next.next [OK]
Common Mistakes:
  • Ignoring null checks causing runtime errors
  • Thinking constructor syntax is wrong
  • Believing console.log causes errors
5. You need to represent a company's organizational chart where each employee can have multiple direct reports. Which data structure should you use and why? Choose the best option and explain your reasoning.
hard
A. Tree, because it naturally models hierarchical relationships with multiple children
B. Linked List, because it allows dynamic insertion and deletion of employees
C. Stack, because it supports last-in-first-out operations suitable for reporting
D. Array, because it stores elements in order and is easy to access by index

Solution

  1. Step 1: Analyze the problem requirements

    Organizational chart has hierarchy with employees having multiple direct reports (children).
  2. Step 2: Match data structure to hierarchy

    Tree structure supports nodes with multiple children, representing managers and their reports well.
  3. Step 3: Evaluate other options

    Array and linked list are linear and do not represent branching; stack is for LIFO, not hierarchy.
  4. Final Answer:

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

    Hierarchy with multiple children = Tree [OK]
Hint: Hierarchy with multiple children means Tree is best [OK]
Common Mistakes:
  • Choosing Array for hierarchy
  • Thinking Linked List handles multiple children well
  • Confusing Stack with hierarchical data
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