Challenge - 5 Problems
Two Sum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Two Sum Classic Hash Solution
What is the output of the following C# code that solves the Two Sum problem using a hash map?
DSA C#
using System; using System.Collections.Generic; class Program { static int[] TwoSum(int[] nums, int target) { Dictionary<int, int> map = new Dictionary<int, int>(); for (int i = 0; i < nums.Length; i++) { int complement = target - nums[i]; if (map.ContainsKey(complement)) { return new int[] { map[complement], i }; } map[nums[i]] = i; } return new int[] { -1, -1 }; } static void Main() { int[] nums = {2, 7, 11, 15}; int target = 9; int[] result = TwoSum(nums, target); Console.WriteLine($"[{result[0]}, {result[1]}]"); } }
Attempts:
2 left
💡 Hint
Think about which two numbers add up to the target 9 in the array.
✗ Incorrect
The numbers 2 and 7 at indices 0 and 1 add up to 9. The code returns their indices as [0, 1].
🧠 Conceptual
intermediateUnderstanding Hash Map Usage in Two Sum
Why does the Two Sum classic hash solution use a dictionary (hash map) to store numbers and their indices?
Attempts:
2 left
💡 Hint
Think about how to find a number's complement efficiently.
✗ Incorrect
The dictionary allows checking if the complement exists in constant time, avoiding nested loops and reducing time complexity.
🔧 Debug
advancedIdentify the Error in Two Sum Implementation
What error will the following code produce when run?
DSA C#
using System; using System.Collections.Generic; class Program { static int[] TwoSum(int[] nums, int target) { Dictionary<int, int> map = new Dictionary<int, int>(); for (int i = 0; i < nums.Length; i++) { int complement = target - nums[i]; if (map.ContainsKey(complement)) { return new int[] { map[complement], i }; } map.Add(nums[i], i); } return new int[] { -1, -1 }; } static void Main() { int[] nums = {3, 3}; int target = 7; int[] result = TwoSum(nums, target); Console.WriteLine($"[{result[0]}, {result[1]}]"); } }
Attempts:
2 left
💡 Hint
Look at how the dictionary is updated inside the loop.
✗ Incorrect
The code uses map.Add which throws an exception if the key already exists. Since nums has duplicate 3's, adding the second 3 causes an error.
❓ Predict Output
advancedOutput When No Two Sum Pair Exists
What is the output of this Two Sum code when no two numbers add up to the target?
DSA C#
using System; using System.Collections.Generic; class Program { static int[] TwoSum(int[] nums, int target) { Dictionary<int, int> map = new Dictionary<int, int>(); for (int i = 0; i < nums.Length; i++) { int complement = target - nums[i]; if (map.ContainsKey(complement)) { return new int[] { map[complement], i }; } map[nums[i]] = i; } return new int[] { -1, -1 }; } static void Main() { int[] nums = {1, 2, 3, 4}; int target = 10; int[] result = TwoSum(nums, target); Console.WriteLine($"[{result[0]}, {result[1]}]"); } }
Attempts:
2 left
💡 Hint
Check if any two numbers in the array add up to 10.
✗ Incorrect
No two numbers in the array add up to 10, so the function returns [-1, -1].
🧠 Conceptual
expertTime Complexity of Two Sum Classic Hash Solution
What is the time complexity of the Two Sum classic hash solution and why?
Attempts:
2 left
💡 Hint
Consider how many times the loop runs and the cost of dictionary operations.
✗ Incorrect
The solution loops through the array once and uses a hash map for constant time lookups, resulting in O(n) time complexity.
