C#
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C#
-
Section 4: Working with Data
-
Section 5: Error Handling
-
Section 6: Delegates and Events
-
Section 7: Async Programming
-
Section 8: More Language Features
-
Section 9: File I/O and Serialization
-
Section 10: Networking in .NET
-
Section 11: The .NET Ecosystem
-
Section 12: Memory and Performance
-
Section 13: Concurrency Beyond Async
-
Section 14: Reflection and Attributes
-
Section 15: Testing and Best Practices
-
Section 16: Design Patterns in C#
-
Section 17: Standard Library Deep Dive
-
Section 18: Data Structures and Algorithms in C#
-
Section 19: GUI and Desktop Development Overview
-
Section 20: Practical Projects
-
Section 21: More Practice Exercises
-
Section 22: More Standard Library and Text Processing
-
Section 23: More Design Patterns
-
Section 24: More Projects
-
Section 25: Interview Practice
-
Section 26: C# Keywords Reference (Modifiers)
-
Section 27: C# Keywords Reference (Statements)
-
Section 28: C# Keywords Reference (Operators)
-
Section 29: BCL: System.Collections.Generic
-
Section 30: BCL: System.Linq
-
Section 31: BCL: System.Threading
-
Section 32: BCL: System.IO
-
Section 33: BCL: System.Text and System.Text.Json
-
Section 34: BCL: System.Net.Http
-
Section 35: C# Language Specification Topics
-
Section 36: More Practice Exercises
-
Section 37: More Async Patterns
-
Section 38: More BCL: System.Reflection and System.Diagnostics
-
Section 39: Nullable Reference Types In Depth
-
Section 40: C# Records and Pattern Matching In Depth
-
Section 41: Dependency Injection Deep Dive
-
Section 42: More Interview and Whiteboard Practice
161: Mock Coding Interview Walkthrough in C#
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
Dictionaryto 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
There are no comments for now.