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

Recursive pattern matching in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive Pattern Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of recursive pattern matching with nested tuples
What is the output of this C# code using recursive pattern matching on nested tuples?
C Sharp (C#)
using System;

class Program {
    static string Describe((int, (int, int)) data) => data switch {
        (0, (0, 0)) => "All zeros",
        (var x, (var y, var z)) when x == y && y == z => "All equal",
        (var x, (_, var z)) when x > z => "First greater than last",
        _ => "Other"
    };

    static void Main() {
        Console.WriteLine(Describe((0, (0, 0))));
        Console.WriteLine(Describe((3, (3, 3))));
        Console.WriteLine(Describe((5, (1, 2))));
        Console.WriteLine(Describe((1, (2, 3))));
    }
}
A
All zeros
All equal
Other
First greater than last
B
All zeros
All equal
First greater than last
Other
C
Other
All equal
First greater than last
Other
D
All zeros
Other
First greater than last
Other
Attempts:
2 left
💡 Hint
Look carefully at the pattern conditions and the order they are checked.
🧠 Conceptual
intermediate
1:30remaining
Understanding recursive pattern matching with lists
Given this C# recursive pattern matching on a list, what is the value of result after execution?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static int SumList(List<int> list) => list switch {
        [] => 0,
        [var head, .. var tail] => head + SumList(tail)
    };

    static void Main() {
        var numbers = new List<int> {1, 2, 3};
        var result = SumList(numbers);
        Console.WriteLine(result);
    }
}
A3
B0
C6
DCompilation error
Attempts:
2 left
💡 Hint
Think about how the list is split into head and tail recursively.
🔧 Debug
advanced
1:30remaining
Identify the runtime error in recursive pattern matching
What runtime error will this C# code produce when executed?
C Sharp (C#)
using System;

class Program {
    static int Factorial(int n) => n switch {
        0 => 1,
        > 0 => n * Factorial(n - 1),
        _ => throw new ArgumentException("Negative input")
    };

    static void Main() {
        Console.WriteLine(Factorial(-1));
    }
}
ASystem.ArgumentException: Negative input
BStackOverflowException
CNo output, program runs fine
DSystem.InvalidOperationException
Attempts:
2 left
💡 Hint
Check which pattern matches negative input and what it does.
📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in recursive pattern matching?
Which of the following C# code snippets will cause a syntax error due to incorrect recursive pattern matching syntax?
Aint Sum((int head, int tail) data) => data switch { var (h, t) => h + t };
Bint Sum((int head, int tail) data) => data switch { (var h, var t) => h + t };
Cint Sum((int head, (int, int) tail) data) => data switch { (var h, (var t1, var t2)) => h + t1 + t2 };
Dint Sum((int head, int tail) data) => data switch { (var h, var t) => h + t, };
Attempts:
2 left
💡 Hint
Check the pattern syntax for tuples inside switch expressions.
🚀 Application
expert
2:30remaining
Calculate depth of nested tuples using recursive pattern matching
What is the output of this C# program that calculates the depth of nested tuples using recursive pattern matching?
C Sharp (C#)
using System;

class Program {
    static int Depth(object obj) => obj switch {
        (var l, var r) => 1 + Math.Max(Depth(l), Depth(r)),
        _ => 0
    };

    static void Main() {
        var nested = ((1, (2, 3)), (4, 5));
        Console.WriteLine(Depth(nested));
    }
}
ACompilation error
B2
C4
D3
Attempts:
2 left
💡 Hint
Count how many levels of nested tuples are inside the variable.