Bird
Raised Fist0
DSA C#programming~20 mins

Node Structure and Pointer Design in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Node Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Linked List Node Traversal
What is the printed output of the following C# code that creates and traverses a singly linked list?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int val) { Value = val; Next = null; }
}

public class Program {
    public static void Main() {
        Node head = new Node(1);
        head.Next = new Node(2);
        head.Next.Next = new Node(3);

        Node current = head;
        while (current != null) {
            System.Console.Write(current.Value);
            if (current.Next != null) System.Console.Write(" -> ");
            current = current.Next;
        }
        System.Console.WriteLine(" -> null");
    }
}
A1 -> 2 -> 3 -> null
B1 -> 3 -> 2 -> null
C3 -> 2 -> 1 -> null
Dnull -> 1 -> 2 -> 3
Attempts:
2 left
💡 Hint
Trace the nodes starting from head and follow the Next pointers.
❓ Predict Output
intermediate
2:00remaining
Output after Reversing a Singly Linked List
Given the following C# code that reverses a singly linked list, what is the printed output?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int val) { Value = val; Next = null; }
}

public class Program {
    public static Node Reverse(Node head) {
        Node prev = null;
        Node current = head;
        while (current != null) {
            Node nextTemp = current.Next;
            current.Next = prev;
            prev = current;
            current = nextTemp;
        }
        return prev;
    }

    public static void Main() {
        Node head = new Node(1);
        head.Next = new Node(2);
        head.Next.Next = new Node(3);

        Node reversedHead = Reverse(head);

        Node current = reversedHead;
        while (current != null) {
            System.Console.Write(current.Value);
            if (current.Next != null) System.Console.Write(" -> ");
            current = current.Next;
        }
        System.Console.WriteLine(" -> null");
    }
}
A1 -> 2 -> 3 -> null
B3 -> 2 -> 1 -> null
Cnull
D1 -> null
Attempts:
2 left
💡 Hint
Reversing changes the direction of Next pointers.
🔧 Debug
advanced
2:00remaining
Identify the Error in Doubly Linked List Node Insertion
What error will occur when running the following C# code that inserts a new node after the head in a doubly linked list?
DSA C#
public class Node {
    public int Value;
    public Node Prev;
    public Node Next;
    public Node(int val) { Value = val; Prev = null; Next = null; }
}

public class Program {
    public static void Main() {
        Node head = new Node(1);
        Node second = new Node(2);
        head.Next = second;

        Node newNode = new Node(3);
        newNode.Next = head.Next;
        newNode.Prev = head;
        head.Next = newNode;
        // Missing update of newNode.Next.Prev

        System.Console.WriteLine(head.Next.Value);
        System.Console.WriteLine(newNode.Next.Prev.Value);
    }
}
ACompilation error due to missing semicolon
BPrints 3 and 3 without error
CNullReferenceException at newNode.Next.Prev.Value
DPrints 3 and 2 without error
Attempts:
2 left
💡 Hint
Check if all Prev pointers are updated after insertion.
🧠 Conceptual
advanced
1:30remaining
Understanding Circular Linked List Node Connections
In a circular singly linked list with three nodes A, B, and C connected in order, what is the value of the node reached by starting at A and following Next pointers 4 times?
AB
BC
CA
DNull
Attempts:
2 left
💡 Hint
Remember the list loops back to the start after the last node.
🚀 Application
expert
2:30remaining
Determine the Number of Nodes in a Complex Linked Structure
Given the following C# code that creates nodes and links them with Next pointers, how many unique nodes are reachable starting from node1 by following Next pointers until null?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int val) { Value = val; Next = null; }
}

public class Program {
    public static void Main() {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);

        node1.Next = node2;
        node2.Next = node3;
        node3.Next = node2; // cycle here
        node4.Next = null;

        // Count nodes reachable from node1
        int count = 0;
        Node current = node1;
        System.Collections.Generic.HashSet<Node> visited = new System.Collections.Generic.HashSet<Node>();
        while (current != null && !visited.Contains(current)) {
            visited.Add(current);
            count++;
            current = current.Next;
        }

        System.Console.WriteLine(count);
    }
}
A2
BInfinite loop
C4
D3
Attempts:
2 left
💡 Hint
Consider the cycle and how visited nodes prevent infinite counting.

Practice

(1/5)
1. What is the primary purpose of the next pointer in a singly linked list node?
easy
A. To store the data value of the node
B. To point to the previous node in the list
C. To point to the next node in the list
D. To indicate the end of the list

Solution

  1. Step 1: Understand the role of pointers in linked lists

    In a singly linked list, each node contains data and a pointer to the next node to maintain the sequence.
  2. Step 2: Identify the function of the next pointer

    The next pointer specifically points to the next node, allowing traversal through the list.
  3. Final Answer:

    To point to the next node in the list -> Option C
  4. Quick Check:

    next pointer = points to next node [OK]
Hint: Remember: next always moves forward in the list [OK]
Common Mistakes:
  • Confusing next with previous pointer
  • Thinking next stores data
  • Assuming next marks list end
2. Which of the following is the correct way to declare a node class with a pointer to the next node in C#?
easy
A. public class Node { int data; Node next; void Node() {} }
B. public class Node { public int data; public Node next; }
C. class Node { public int data; Node next; }
D. public struct Node { int data; Node next; }

Solution

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

    public class Node { public int data; public Node next; } uses public for both data and next pointer, which is standard for node class fields.
  2. Step 2: Verify class declaration and member types

    public class Node { public int data; public Node next; } correctly declares a class with an integer data and a pointer to the next node of the same type.
  3. Final Answer:

    public class Node { public int data; public Node next; } -> Option B
  4. Quick Check:

    Correct class with public members = public class Node { public int data; public Node next; } [OK]
Hint: Use public class with public fields for simple node design [OK]
Common Mistakes:
  • Missing public keyword causing access issues
  • Using struct instead of class for linked nodes
  • Incorrect constructor syntax
3. Consider the following C# code for a singly linked list node and a small list creation:
public class Node {
  public int data;
  public Node next;
  public Node(int d) { data = d; next = null; }
}

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

Console.WriteLine(head.next.next.data);

What will be the output?
medium
A. 3
B. 2
C. NullReferenceException
D. 1

Solution

  1. Step 1: Trace node creation and linking

    Head node has data 1, its next points to node with data 2, which in turn points to node with data 3.
  2. Step 2: Access head.next.next.data

    This accesses the third node's data, which is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Third node data = 3 [OK]
Hint: Follow next pointers step-by-step to find data [OK]
Common Mistakes:
  • Confusing node positions and accessing wrong data
  • Assuming next.next is null
  • Expecting runtime error incorrectly
4. Given the following node class and code snippet, identify the error:
public class Node {
  public int data;
  public Node next;
  public Node(int d) { data = d; next = null; }
}

Node head = new Node(10);
head.next.next = new Node(20);

What is the problem here?
medium
A. Data type mismatch in node declaration
B. Node class is missing a constructor
C. No error, code runs fine
D. head.next is null, so accessing head.next.next causes an error

Solution

  1. Step 1: Analyze the initial node creation

    Head node is created with data 10, and its next pointer is null by default.
  2. Step 2: Check the assignment head.next.next = new Node(20);

    Since head.next is null, trying to access head.next.next causes a NullReferenceException.
  3. Final Answer:

    head.next is null, so accessing head.next.next causes an error -> Option D
  4. Quick Check:

    Null pointer access causes error [OK]
Hint: Always initialize intermediate nodes before accessing their pointers [OK]
Common Mistakes:
  • Assuming next is auto-initialized
  • Ignoring null pointer exceptions
  • Thinking constructor is missing
5. You want to design a doubly linked list node in C# that can point both to the previous and next nodes. Which of the following class definitions correctly implements this?
hard
A. public class Node { public int data; public Node prev; public Node next; public Node(int d) { data = d; prev = null; next = null; } }
B. public class Node { int data; Node next; Node prev; public Node(int d) { data = d; next = null; } }
C. public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } }
D. public struct Node { public int data; public Node prev; public Node next; }

Solution

  1. Step 1: Identify the need for both prev and next pointers

    A doubly linked list node must have pointers to both previous and next nodes to allow two-way traversal.
  2. Step 2: Check class definition correctness

    public class Node { public int data; public Node prev; public Node next; public Node(int d) { data = d; prev = null; next = null; } } correctly declares public prev and next pointers and initializes them to null in the constructor.
  3. Final Answer:

    public class Node { public int data; public Node prev; public Node next; public Node(int d) { data = d; prev = null; next = null; } } -> Option A
  4. Quick Check:

    Doubly linked node has both prev and next pointers [OK]
Hint: Doubly linked nodes need both prev and next pointers [OK]
Common Mistakes:
  • Omitting the prev pointer
  • Using struct instead of class for nodes
  • Not initializing pointers in constructor
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