Skip to Content
Course content

161: Mock Coding Interview Walkthrough in C#

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When you're in a coding interview, the pressure is high. You're thinking about Big O notation, trying to remember the exact syntax for a Dictionary, and feeling the interviewer's gaze while you stare at a blank screen. It's easy to write code that looks correct but fails on a critical edge case. Let's look at a classic: the "Two Sum" problem. The goal is to find two numbers in an array that add up to a specific target and return their indices.

public int[] TwoSum(int[] nums, int target) 
{
    var map = new Dictionary<int, int>();
    
    // First pass: Put everything in the map
    for (int i = 0; i < nums.Length; i++) 
    {
        map[nums[i]] = i;
    }

    // Second pass: Find the complement
    for (int i = 0; i < nums.Length; i++) 
    {
        int complement = target - nums[i];
        if (map.ContainsKey(complement)) 
        {
            return new int[] { i, map[complement] };
        }
    }
    return null;
}

The Double-Counting Glitch

At first glance, this looks like a solid O(n) approach. We're using a hash map to avoid the nested loop (which would be O(n²)). But if you ran this with nums = [3, 2, 4] and target = 6, you'd run into a problem. The code would see the first element (3), calculate the complement (6 - 3 = 3), and check the map. Since we already populated the map in the first loop, it finds 3 at index 0 and returns [0, 0].

The problem statement for Two Sum almost always specifies that you cannot use the same element twice. This is a common "gotcha" in interviews. You solved the time complexity problem, but you missed the logical constraint. I've seen plenty of junior devs freeze up here because they're so focused on the "optimal" algorithm that they forget to validate the basic rules of the prompt.

The Single-Pass Optimization

The fix is actually simpler than the broken version. Instead of two separate loops, we do everything in one. We check for the complement before we add the current number to the dictionary. If the complement is already in there, we've found our pair. If not, we add the current number and move on.

public int[] TwoSum(int[] nums, int target) 
{
    var map = new Dictionary<int, int>();

    for (int i = 0; i < nums.Length; i++) 
    {
        int complement = target - nums[i];
        
        // Check if the complement was already encountered
        if (map.TryGetValue(complement, out int index)) 
        {
            return new int[] { index, i };
        }

        // Only add the current number AFTER checking
        // This prevents using the same element twice
        if (!map.ContainsKey(nums[i])) 
        {
            map.Add(nums[i], i);
        }
    }
    return null;
}

I switched to TryGetValue here because it's more idiomatic C#. It performs the lookup once instead of twice (which ContainsKey followed by the indexer would do). In a high-performance environment—or a very picky interview—that small detail shows you actually know the framework, not just the general theory of hash maps.

Thinking Out Loud During the Interview

The code is only half the battle. If you just sit in silence for ten minutes and then produce the perfect solution, the interviewer has no idea how you think. They aren't just looking for the answer; they're looking for your process.

  • Clarify the constraints: Ask "Can the array contain negative numbers?" or "Will there always be exactly one solution?" This shows you're thinking about edge cases before you write a single line.
  • Discuss the trade-offs: Mention that you're choosing a Dictionary to trade space (O(n) memory) for time (O(n) speed). This signals that you understand the Space-Time Complexity trade-off.
  • Dry run your code: Before saying "I'm done," manually trace a small example (like the [3, 2, 4] example we used) through your logic. You'll often catch your own bugs before the interviewer has to point them out, which actually makes you look better than if you had gotten it right the first time.



📋 Practical Task

Implement a First Non-Repeating Character Finder

To practice the "single-pass" or "frequency mapping" pattern we used in the Two Sum walkthrough, write a method called FindFirstUniqueChar. This method should take a string as input and return the index of the first character that does not repeat anywhere else in the string. If every character repeats, return -1.

Requirements:

  • Use a Dictionary<char, int> to track the frequency of each character.
  • Ensure your time complexity is O(n).
  • Handle cases with empty strings or strings where no unique character exists.

Example:

  • Input: "leetcode" → Output: 0 ( 'l' is the first unique)
  • Input: "loveleetcode" → Output: 2 ( 'v' is the first unique)
  • Input: "aabb" → Output: -1
Rating
0 0

There are no comments for now.

to be the first to leave a comment.