C# Coding Interview Questions – Arrays, Strings & LINQ
1. How do you count character occurrences in a string?
Explanation: Count how many times each character appears in a string using a Dictionary<char, int>.
Example:
string input = "programming";
Dictionary<char, int> counts = new();
foreach (char ch in input)
{
if (counts.ContainsKey(ch))
counts[ch]++;
else
counts[ch] = 1;
}
foreach (var item in counts)
{
Console.WriteLine($"{item.Key} = {item.Value}");
}
Output:
p = 1
r = 2
o = 1
g = 2
a = 1
m = 2
i = 1
n = 1
How it works:
-
The dictionary stores each character as a key.
-
If the character already exists, increment its count.
-
Otherwise, add it with a count of
1.
Key Points:
-
Dictionary<char, int>stores character frequencies. -
ContainsKey()checks whether a character exists. -
Time complexity: O(n) average.
-
Space complexity: O(k), where k is the number of distinct characters.
2. How do you swap two numbers without using a third variable? 🔴
Explanation: Swap two numbers using arithmetic operations without introducing a temporary variable.
Example:
int a = 10;
int b = 20;
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine($"a = {a}");
Console.WriteLine($"b = {b}");
Output:
a = 20
b = 10
How it works:
Initially:
a = 10, b = 20
Step 1: a = 10 + 20 = 30
Step 2: b = 30 - 20 = 10
Step 3: a = 30 - 10 = 20
Finally:
a = 20, b = 10
Key Points:
-
No temporary variable is required.
-
Time complexity: O(1).
-
Space complexity: O(1).
-
Arithmetic swapping can cause integer overflow.
Modern C# alternative (recommended):
(a, b) = (b, a);
This is clearer and avoids arithmetic overflow.
3. How do you find missing numbers in an array? 🔴
Explanation: Find numbers missing from a sequence within a known range.
Example:
Assume the expected numbers are from 1 to 10.
int[] numbers = { 1, 2, 4, 6, 7, 9, 10 };
HashSet<int> existing = new(numbers);
for (int i = 1; i <= 10; i++)
{
if (!existing.Contains(i))
{
Console.WriteLine(i);
}
}
Output:
3
5
8
How it works:
Expected: 1 2 3 4 5 6 7 8 9 10
Actual: 1 2 4 6 7 9 10
Missing: 3, 5, 8
Key Points:
-
HashSet<int>provides fast membership checking. -
Finds multiple missing numbers.
-
Works even when the input array is unsorted.
-
Time complexity: O(n + R) average, where R is the expected range size.
-
Space complexity: O(n).
4. How do you group employees by department using LINQ? 🔴
Explanation: Use LINQ GroupBy() to organize employees based on their department.
Example:
var employees = new[]
{
new { Name = "John", Department = "IT" },
new { Name = "David", Department = "HR" },
new { Name = "Sam", Department = "IT" },
new { Name = "Mary", Department = "HR" }
};
var groups = employees.GroupBy(e => e.Department);
foreach (var group in groups)
{
Console.WriteLine($"Department: {group.Key}");
foreach (var employee in group)
{
Console.WriteLine(employee.Name);
}
}
Output:
Department: IT
John
Sam
Department: HR
David
Mary
How it works:
Employee List
|
v
GroupBy(Department)
|
+---- IT
| John
| Sam
|
+---- HR
David
Mary
Key Points:
-
GroupBy()groups records based on a key. -
group.Keycontains the department name. -
Each group contains employees belonging to that department.
-
GroupBy()returnsIEnumerable<IGrouping<TKey, TElement>>. -
Commonly used in reports and data aggregation.
5. How do you find duplicate records using LINQ? 🔴
Explanation: Use GroupBy() and Where() to identify records that occur more than once based on a selected field.
Example:
Find duplicate employees based on their email address.
var employees = new[]
{
new { Id = 1, Email = "john@gmail.com" },
new { Id = 2, Email = "sam@gmail.com" },
new { Id = 3, Email = "john@gmail.com" },
new { Id = 4, Email = "david@gmail.com" },
new { Id = 5, Email = "sam@gmail.com" }
};
var duplicates = employees
.GroupBy(e => e.Email)
.Where(g => g.Count() > 1);
foreach (var group in duplicates)
{
Console.WriteLine($"{group.Key} = {group.Count()}");
}
Output:
john@gmail.com = 2
sam@gmail.com = 2
How it works:
Employee Records
|
v
GroupBy(Email)
|
v
Count each group
|
v
Where(Count > 1)
|
v
Duplicate Emails
Key Points:
-
GroupBy()groups records by email. -
Count() > 1identifies duplicate groups. -
group.Keyprovides the duplicated value. -
Use
SelectMany()to retrieve all records belonging to duplicate groups. -
Can group by multiple fields:
.GroupBy(e => new { e.Name, e.Department })
This requires those properties to exist in the employee model.
Quick Interview Revision
|
Question |
Technique |
Time Complexity |
|---|---|---|
|
Count character occurrences |
Dictionary |
O(n) average |
|
Swap two numbers |
Arithmetic / Tuple |
O(1) |
|
Find missing numbers |
HashSet |
O(n + R) average |
|
Group employees |
LINQ |
O(n) average |
|
Find duplicate records |
|
O(n) average |
Interview Tip: Remember these three important collection operations:
-
Dictionary – Stores key-value pairs and is useful for counting occurrences.
-
HashSet – Stores unique values and is useful for identifying duplicates or missing elements.
-
LINQ GroupBy – Groups records and is useful for reporting, aggregation, and duplicate detection.