Challenge - 5 Problems
Positional Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of positional pattern matching with tuples
What is the output of this C# code using positional patterns with tuples?
C Sharp (C#)
var point = (X: 3, Y: 4); string result = point switch { (0, 0) => "Origin", (var x, 0) => $"X={x}, on X-axis", (0, var y) => $"Y={y}, on Y-axis", (var x, var y) => $"Point at ({x},{y})" }; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Think about which pattern matches the tuple (3,4) first.
✗ Incorrect
The tuple (3,4) does not match (0,0), nor (var x, 0) because Y is 4, not 0, nor (0, var y) because X is 3, not 0. So it matches the last pattern (var x, var y) and outputs "Point at (3,4)".
❓ Predict Output
intermediate2:00remaining
Pattern matching with positional records
Given the record and code below, what is the output?
C Sharp (C#)
record Person(string FirstName, string LastName); var person = new Person("Alice", "Smith"); string greeting = person switch { ("Alice", "Smith") => "Hello Alice Smith!", ("Bob", _) => "Hello Bob!", _ => "Hello stranger!" }; Console.WriteLine(greeting);
Attempts:
2 left
💡 Hint
Check which positional pattern matches the Person record.
✗ Incorrect
The person is Alice Smith, which matches the first pattern exactly, so the output is "Hello Alice Smith!".
🔧 Debug
advanced2:00remaining
Positional pattern matching with guards on records
What is the output of this code?
C Sharp (C#)
record Point(int X, int Y); var p = new Point(1, 2); string result = p switch { (var x, var y) when x > y => "X is greater", (var x, var y) when y > x => "Y is greater", (var x, var y) when x == y => "Equal", _ => "Unknown" }; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Records support positional patterns via generated Deconstruct methods. For (1,2), evaluate the when conditions in order.
✗ Incorrect
Records automatically generate Deconstruct methods, so the code compiles and runs successfully. For Point(1,2), it deconstructs to x=1, y=2. The first guard (1 > 2) is false, the second (2 > 1) is true, so it outputs "Y is greater".
❓ Predict Output
advanced2:00remaining
Output of nested positional patterns
What is the output of this code with nested positional patterns?
C Sharp (C#)
record Rectangle(Point TopLeft, Point BottomRight); record Point(int X, int Y); var rect = new Rectangle(new Point(0, 0), new Point(5, 5)); string description = rect switch { (var (x1, y1), var (x2, y2)) when x1 == 0 && y1 == 0 && x2 == 5 && y2 == 5 => "Standard 5x5 rectangle", _ => "Other rectangle" }; Console.WriteLine(description);
Attempts:
2 left
💡 Hint
Look at how nested positional patterns match the record properties.
✗ Incorrect
The Rectangle has two Points. The pattern matches the nested Points by decomposing them with (var (x1, y1), var (x2, y2)). The when clause checks the coordinates. Since they match exactly, the output is "Standard 5x5 rectangle".
❓ Predict Output
expert3:00remaining
Output of positional pattern with deconstruction and discard
What is the output of this code using positional patterns with discard (_) and deconstruction?
C Sharp (C#)
record Color(int R, int G, int B); Color c = new(255, 0, 0); string result = c switch { (255, 0, 0) => "Red", (0, 255, 0) => "Green", (0, 0, 255) => "Blue", (var r, var g, var b) when r == g && g == b => "Gray", _ => "Unknown color" }; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Check which positional pattern matches the color (255,0,0).
✗ Incorrect
The color is (255,0,0), which matches the first pattern exactly, so the output is "Red".