0
0
C Sharp (C#)programming~20 mins

LinkedList usage in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LinkedList Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LinkedList AddFirst and AddLast
What is the output of the following C# code?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<int> list = new LinkedList<int>();
        list.AddFirst(10);
        list.AddLast(20);
        list.AddFirst(5);
        foreach (var item in list) {
            Console.Write(item + " ");
        }
    }
}
A10 5 20
B20 10 5
C10 20 5
D5 10 20
Attempts:
2 left
💡 Hint
Remember AddFirst adds to the start, AddLast adds to the end.
Predict Output
intermediate
2:00remaining
LinkedList Remove and Count
What is the output of this C# code snippet?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<string> names = new LinkedList<string>();
        names.AddLast("Anna");
        names.AddLast("Bob");
        names.AddLast("Cara");
        names.Remove("Bob");
        Console.WriteLine(names.Count);
    }
}
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Removing an element decreases the count by one.
🔧 Debug
advanced
2:00remaining
Find the error in LinkedList node insertion
What error will this C# code produce when run?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<int> numbers = new LinkedList<int>();
        numbers.AddLast(1);
        numbers.AddLast(3);
        var node = numbers.Find(2);
        numbers.AddAfter(node, 2);
        foreach (var n in numbers) {
            Console.Write(n + " ");
        }
    }
}
ANullReferenceException
BInvalidOperationException
CNo error, output: 1 3 2
DArgumentNullException
Attempts:
2 left
💡 Hint
Check what happens if Find returns null and you use it as a node.
Predict Output
advanced
2:00remaining
LinkedList Reverse Traversal Output
What is the output of this C# code?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<char> letters = new LinkedList<char>();
        letters.AddLast('a');
        letters.AddLast('b');
        letters.AddLast('c');
        var node = letters.Last;
        while (node != null) {
            Console.Write(node.Value);
            node = node.Previous;
        }
    }
}
Aabc
Bcba
Cbac
Dcab
Attempts:
2 left
💡 Hint
Start from the last node and move backwards.
🧠 Conceptual
expert
2:00remaining
LinkedList Node Removal Impact
Given a LinkedList with elements 1, 2, 3, 4, 5, what is the value of the node after removing the node with value 3?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<int> list = new LinkedList<int>(new int[] {1, 2, 3, 4, 5});
        var node = list.Find(3);
        var nextNode = node.Next;
        list.Remove(node);
        Console.WriteLine(nextNode.Value);
    }
}
A3
B5
C4
DThrows InvalidOperationException
Attempts:
2 left
💡 Hint
Check what happens to the Next property of a node after removal.