Hint: Use new int[] { ... } to initialize arrays in C# [OK]
Common Mistakes:
Using parentheses instead of braces
Assigning List to array variable
Missing type declaration
3. What is the output of the following C# code snippet that finds all unique triplets summing to zero?
int[] nums = { -1, 0, 1, 2, -1, -4 };
var result = ThreeSum(nums);
foreach(var triplet in result) {
Console.WriteLine(string.Join(", ", triplet));
}
// Assume ThreeSum returns List of unique triplets
medium
A. [0, 1, 2]
[-1, -1, -4]
B. [-1, 0, 1]
[-1, -1, 2]
C. [-4, 2, 2]
[-1, 0, 1]
D. [-1, 1, 2]
[0, 0, 0]
Solution
Step 1: Sort and find triplets summing to zero
Sorted array: [-4, -1, -1, 0, 1, 2]. Triplets summing to zero are [-1, -1, 2] and [-1, 0, 1].
Step 2: Match output with options
[-1, 0, 1]
[-1, -1, 2] lists these two triplets correctly. Others contain invalid or impossible triplets.
Final Answer:
[-1, 0, 1]
[-1, -1, 2] -> Option B
Quick Check:
Unique triplets = [-1, 0, 1]
[-1, -1, 2] [OK]
Hint: Sort array first, then find triplets with two-pointer [OK]
Common Mistakes:
Not sorting before searching
Including duplicate triplets
Wrong triplet sums
4. Identify the bug in this C# code snippet for the Three Sum problem:
public IList ThreeSum(int[] nums) {
var result = new List();
Array.Sort(nums);
for (int i = 0; i < nums.Length - 2; i++) {
int left = i + 1, right = nums.Length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
result.Add(new List<int> { nums[i], nums[left], nums[right] });
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
return result;
}
medium
A. No check to skip duplicate values for i, left, and right
B. Array is not sorted before processing
C. Incorrect sum calculation formula
D. Using List instead of Array for result
Solution
Step 1: Analyze duplicate handling in loops
The code does not skip duplicate values for i, left, or right, which can cause duplicate triplets in the result.
Step 2: Verify other parts
Array is sorted, sum calculation is correct, and List usage is valid. So only duplicate skipping is missing.
Final Answer:
No check to skip duplicate values for i, left, and right -> Option A
Quick Check:
Missing duplicate skip = No check to skip duplicate values for i, left, and right [OK]
Hint: Always skip duplicates after finding a valid triplet [OK]
Common Mistakes:
Not skipping duplicates causing repeated triplets
Forgetting to sort array
Wrong sum calculation
5. Given the input array nums = [-2, 0, 1, 2, -1, -1, 2], which of the following lists contains all unique triplets that sum to zero?
hard
A. [[-2, 0, 1], [-1, -1, 1], [0, 1, 2]]
B. [[-2, 1, 1], [-1, 0, 1], [0, 0, 0]]
C. [[-2, -1, 3], [-1, 1, 0], [1, 1, -2]]
D. [[-2, 0, 2], [-1, -1, 2], [-1, 0, 1]]
Solution
Step 1: Sort the array and find triplets
Sorted nums: [-2, -1, -1, 0, 1, 2, 2]. Valid unique triplets summing to zero are [-2, 0, 2], [-1, -1, 2], and [-1, 0, 1].
Step 2: Compare with options
[[-2, 0, 2], [-1, -1, 2], [-1, 0, 1]] matches these triplets exactly. Others contain invalid or duplicate triplets.