C# LeetCode Challenges You’ll See in Real Interviews
2025-08-21
If you are prepping in C#, a small set of patterns will cover most interview questions. Below are the challenges you are most likely to face, with clean C# solutions you can run, plus quick talking points you can say out loud.
1) Two Sum - Hash map lookup
Why it appears: tests hash tables and constant time lookups.
using System;
using System.Collections.Generic;
public class Solution {
public int[] TwoSum(int[] nums, int target) {
var seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++) {
int need = target - nums[i];
if (seen.TryGetValue(need, out int j)) return new[] { j, i };
seen[nums[i]] = i;
}
return Array.Empty<int>();
}
}
Talking point: O(n) time, O(n) space. Check before insert so you never reuse the same index.
2) Longest Substring Without Repeating Characters - Sliding window
using System;
using System.Collections.Generic;
public class Solution {
public int LengthOfLongestSubstring(string s) {
var last = new Dictionary<char, int>();
int left = 0, best = 0;
for (int right = 0; right < s.Length; right++) {
char c = s[right];
if (last.TryGetValue(c, out int pos) && pos >= left) left = pos + 1;
last[c] = right;
best = Math.Max(best, right - left + 1);
}
return best;
}
}
Talking point: grow right, shrink left to restore validity. O(n) time.
3) Product of Array Except Self - Prefix and suffix
using System;
public class Solution {
public int[] ProductExceptSelf(int[] nums) {
int n = nums.Length;
var res = new int[n];
int pref = 1;
for (int i = 0; i < n; i++) {
res[i] = pref;
pref *= nums[i];
}
int suff = 1;
for (int i = n - 1; i >= 0; i--) {
res[i] *= suff;
suff *= nums[i];
}
return res;
}
}
Talking point: no division, O(n) time, O(1) extra space if you ignore output.
4) Valid Anagram - Counting
using System;
public class Solution {
public bool IsAnagram(string s, string t) {
if (s.Length != t.Length) return false;
var cnt = new int[26];
foreach (char c in s) cnt[c - 'a']++;
foreach (char c in t) if (--cnt[c - 'a'] < 0) return false;
return true;
}
}
Talking point: fixed alphabet gives O(n) time and O(1) space.
5) Merge Intervals - Sort then sweep
using System;
using System.Collections.Generic;
public class Solution {
public int[][] Merge(int[][] intervals) {
Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0]));
var res = new List<int[]>();
var cur = new int[] { intervals[0][0], intervals[0][1] };
for (int i = 1; i < intervals.Length; i++) {
if (intervals[i][0] <= cur[1]) cur[1] = Math.Max(cur[1], intervals[i][1]);
else { res.Add(cur); cur = new int[] { intervals[i][0], intervals[i][1] }; }
}
res.Add(cur);
return res.ToArray();
}
}
Talking point: sort by start then either extend or start a new block.
6) Binary Search on Rotated Array - Condition split
using System;
public class Solution {
public int Search(int[] nums, int target) {
int lo = 0, hi = nums.Length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target) return mid;
if (nums[lo] <= nums[mid]) {
if (nums[lo] <= target && target < nums[mid]) hi = mid - 1;
else lo = mid + 1;
} else {
if (nums[mid] < target && target <= nums[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
}
Talking point: one side is sorted. Pick the correct half each loop.
7) Linked List Cycle - Floyd’s two pointers
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
public class Solution {
public bool HasCycle(ListNode head) {
var slow = head; var fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
}
Talking point: O(1) space. If they ask for cycle start, reset one pointer to head and meet again.
8) BFS Number of Islands - Grid traversal
using System;
using System.Collections.Generic;
public class Solution {
public int NumIslands(char[][] grid) {
int m = grid.Length, n = grid[0].Length, count = 0;
int[][] dirs = { new []{1,0}, new []{-1,0}, new []{0,1}, new []{0,-1} };
var q = new Queue<(int r, int c)>();
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (grid[r][c] != '1') continue;
count++;
grid[r][c] = '0';
q.Enqueue((r, c));
while (q.Count > 0) {
var (x, y) = q.Dequeue();
foreach (var d in dirs) {
int nx = x + d[0], ny = y + d[1];
if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
if (grid[nx][ny] != '1') continue;
grid[nx][ny] = '0';
q.Enqueue((nx, ny));
}
}
}
}
return count;
}
}
Talking point: O(mn). Use DFS or BFS. Mark visited in place.
9) Top K Frequent Elements - Dictionary plus heap
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] TopKFrequent(int[] nums, int k) {
var freq = nums.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
var pq = new PriorityQueue<int, int>(); // min heap by frequency
foreach (var kv in freq) {
pq.Enqueue(kv.Key, kv.Value);
if (pq.Count > k) pq.Dequeue();
}
var res = new int[k];
for (int i = k - 1; i >= 0; i--) res[i] = pq.Dequeue();
return res;
}
}
Talking point: O(n log k) with a min heap. In .NET 6 PriorityQueue makes this neat.
10) House Robber - 1D DP
using System;
public class Solution {
public int Rob(int[] nums) {
int prev2 = 0, prev1 = 0;
foreach (int x in nums) {
int take = prev2 + x;
int skip = prev1;
int cur = Math.Max(take, skip);
prev2 = prev1;
prev1 = cur;
}
return prev1;
}
}
Talking point: rolling variables give O(1) space.
11) Meeting Rooms II - Sweep line
using System;
public class Solution {
public int MinMeetingRooms(int[][] intervals) {
int n = intervals.Length;
var starts = new int[n];
var ends = new int[n];
for (int i = 0; i < n; i++) { starts[i] = intervals[i][0]; ends[i] = intervals[i][1]; }
Array.Sort(starts);
Array.Sort(ends);
int rooms = 0, j = 0;
for (int i = 0; i < n; i++) {
if (starts[i] < ends[j]) rooms++;
else j++;
}
return rooms;
}
}
Talking point: sort starts and ends separately, walk both.
12) LRU Cache - Dictionary plus doubly linked list
using System.Collections.Generic;
public class LRUCache {
private readonly int cap;
private readonly Dictionary<int, LinkedListNode<(int key, int val)>> map = new();
private readonly LinkedList<(int key, int val)> list = new();
public LRUCache(int capacity) { cap = capacity; }
public int Get(int key) {
if (!map.TryGetValue(key, out var node)) return -1;
list.Remove(node);
list.AddFirst(node);
return node.Value.val;
}
public void Put(int key, int value) {
if (map.TryGetValue(key, out var node)) {
node.Value = (key, value);
list.Remove(node);
list.AddFirst(node);
return;
}
if (map.Count == cap) {
var lru = list.Last!;
map.Remove(lru.Value.key);
list.RemoveLast();
}
var n = new LinkedListNode<(int key, int val)>((key, value));
list.AddFirst(n);
map[key] = n;
}
}
Talking point: O(1) get and put by combining a map with a list.
13) Binary Search on Answer - Koko Eating Bananas style
using System;
public class Solution {
public int MinEatingSpeed(int[] piles, int h) {
int lo = 1, hi = 0;
foreach (var p in piles) hi = Math.Max(hi, p);
bool Can(int k) {
long hours = 0;
foreach (var p in piles) {
hours += (p + k - 1) / k;
if (hours > h) return false;
}
return true;
}
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (Can(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
}
Talking point: if speed k works, higher speeds also work, so the predicate is monotonic.
14) Backtracking - Generate Parentheses
using System;
using System.Collections.Generic;
using System.Text;
public class Solution {
public IList<string> GenerateParenthesis(int n) {
var res = new List<string>();
var sb = new StringBuilder();
void Dfs(int open, int close) {
if (sb.Length == 2 * n) { res.Add(sb.ToString()); return; }
if (open < n) {
sb.Append('(');
Dfs(open + 1, close);
sb.Length--;
}
if (close < open) {
sb.Append(')');
Dfs(open, close + 1);
sb.Length--;
}
}
Dfs(0, 0);
return res;
}
}
Talking point: choose, explore, unchoose. Prune when close would exceed open.
15) Graph Topological Sort - Course Schedule
using System;
using System.Collections.Generic;
public class Solution {
public bool CanFinish(int n, int[][] prereq) {
var adj = new List<int>[n];
for (int i = 0; i < n; i++) adj[i] = new List<int>();
var indeg = new int[n];
foreach (var e in prereq) { adj[e[0]].Add(e[1]); indeg[e[1]]++; }
var q = new Queue<int>();
for (int i = 0; i < n; i++) if (indeg[i] == 0) q.Enqueue(i);
int seen = 0;
while (q.Count > 0) {
int u = q.Dequeue(); seen++;
foreach (var v in adj[u]) if (--indeg[v] == 0) q.Enqueue(v);
}
return seen == n;
}
}
Talking point: BFS on indegrees. Detects cycles by counting processed nodes.
Interview checklist you can say out loud
- Restate the problem and constraints before coding
- Identify the pattern quickly
- State time and space upfront
- Walk a tiny example to verify invariants
- Write code in one pass when possible
- Mention edge cases and tests
Quick practice set to cover the above
- Two Sum
- Longest Substring Without Repeating Characters
- Product of Array Except Self
- Merge Intervals
- Search in Rotated Sorted Array
- Linked List Cycle
- Number of Islands
- Top K Frequent Elements
- House Robber
- Meeting Rooms II
- LRU Cache
- Koko Eating Bananas
- Generate Parentheses
- Course Schedule
Work through these and you will hit most of what shows up in real C# interviews.
If you want a safety net while you practice that can capture a prompt and show a concise C# solution with a short explanation you can repeat back, take a look at StealthCoder. It stays out of the way and helps when you get stuck.