📖 Scenario: You are working on a simple inventory system for a small store. The store's products are arranged in a 2D grid where each row represents a category and each column represents a shelf position. You want to find if a specific product ID exists in the store's inventory.
🎯 Goal: Build a program that searches for a given product ID in a 2D matrix and tells if it is found or not.
📋 What You'll Learn
Create a 2D integer array called inventory with exact values
Create an integer variable called targetProduct to hold the product ID to search
Use nested for loops with variables row and col to search the matrix
Print "Found" if the product is in the matrix, otherwise print "Not Found"
💡 Why This Matters
🌍 Real World
Searching in a 2D matrix is common in inventory systems, image processing, and game boards where data is arranged in grids.
💼 Career
Understanding how to traverse and search 2D arrays is essential for software developers working with matrices, tables, or grid-based data.
Progress0 / 4 steps
1
Create the 2D matrix
Create a 2D integer array called inventory with these exact values: {{101, 102, 103}, {201, 202, 203}, {301, 302, 303}}
DSA C#
Hint
Use int[,] to declare a 2D array and initialize it with the given values.
2
Set the target product ID
Create an integer variable called targetProduct and set it to 202
DSA C#
Hint
Declare an integer variable and assign the value 202 to it.
3
Search the matrix for the target product
Use nested for loops with variables row and col to iterate over inventory. Inside the loops, check if inventory[row, col] equals targetProduct. If found, set a boolean variable found to true and break out of the loops.
DSA C#
Hint
Use inventory.GetLength(0) for rows and inventory.GetLength(1) for columns. Use a boolean found to track if the product is found.
4
Print the search result
Print "Found" if found is true, otherwise print "Not Found"
DSA C#
Hint
Use System.Console.WriteLine to print the result based on the found variable.
Practice
(1/5)
1. What is the main idea behind searching in a 2D matrix where each row and column is sorted in ascending order?
easy
A. Start from the top-right corner and move left or down based on comparison
B. Start from the bottom-left corner and move right or up based on comparison
C. Search each row completely using linear search
D. Use binary search on each column independently
Solution
Step 1: Understand matrix sorting property
Each row and column is sorted ascending, so top-right is smallest in column and largest in row.
Step 2: Use top-right start to decide movement
If target is less than current, move left; if greater, move down to find target efficiently.
Final Answer:
Start from the top-right corner and move left or down based on comparison -> Option A
Quick Check:
Top-right corner search = Start from the top-right corner and move left or down based on comparison [OK]
Hint: Start search at top-right corner for sorted 2D matrix [OK]
Common Mistakes:
Starting from bottom-left corner without reason
Searching each row fully ignoring sorting
Applying binary search on columns only
2. Which of the following is the correct C# syntax to declare a 2D integer array named matrix with 3 rows and 4 columns?
easy
A. int matrix = new int[3,4];
B. int[,] matrix = new int[3,4];
C. int matrix[3,4];
D. int[][] matrix = new int[3][4];
Solution
Step 1: Understand C# 2D array declaration
In C#, multidimensional arrays use comma inside brackets: int[,] for 2D arrays.
Step 2: Check syntax correctness
int[,] matrix = new int[3,4]; uses correct syntax. Options A, B, C are invalid syntax.
4. Identify the error in the following C# code that searches for a target in a sorted 2D matrix:
int[,] matrix = {
{1, 4, 7},
{2, 5, 8},
{3, 6, 9}
};
int target = 5;
int row = 0, col = 0;
bool found = false;
while (row < 3 && col < 3) {
if (matrix[row, col] == target) {
found = true;
break;
} else if (matrix[row, col] < target) {
col++;
} else {
row++;
}
}
Console.WriteLine(found);
medium
A. Starting point should be top-right, not top-left
B. Loop condition should use OR instead of AND
C. Should increment row when element is less than target
D. No error, code works correctly
Solution
Step 1: Check starting point for search
Code starts at top-left (0,0), but matrix sorted rows and columns require top-right start for correct search.
Step 2: Understand movement logic
Moving right or down from top-left does not guarantee finding target efficiently or correctly.
Final Answer:
Starting point should be top-right, not top-left -> Option A
Quick Check:
Correct start point = top-right [OK]
Hint: Start search at top-right corner, not top-left [OK]
Common Mistakes:
Starting search at wrong corner
Using wrong loop conditions
Incorrect movement directions
5. Given a 2D matrix where each row and column is sorted ascending, write a method to count how many times a target number appears. Which approach is most efficient?
hard
A. Traverse entire matrix and count matches (O(m*n))
B. Use binary search on each row separately (O(m*log n))
C. Start from top-right and move left/down counting matches (O(m+n))
D. Use hash map to store counts before searching (O(m*n))
Solution
Step 1: Analyze brute force approach
Traversing entire matrix is simple but slow (O(m*n)).
Step 2: Consider optimized search from top-right
Starting at top-right, move left/down to find all occurrences efficiently in O(m+n) time.
Step 3: Compare with binary search per row
Binary search per row is better than brute force but slower than top-right approach.
Final Answer:
Start from top-right and move left/down counting matches (O(m+n)) -> Option C
Quick Check:
Efficient count = top-right search O(m+n) [OK]
Hint: Count matches by top-right search moving left/down [OK]
Common Mistakes:
Using full matrix traversal ignoring sorting
Applying binary search without combining results
Using extra space unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.