← Back to CatZep
C# Coding Interview
Twenty-three patterns and ninety-two worked problems, all in C#. Then eleven pages of advanced C#, up to C# 14.
This is the C# twin of the Python Coding Interview . The patterns are the same. The code, the costs and the traps are C#. Every code block compiles on .NET 10 and is checked by a test.
How to use this. Read a pattern's When to use and Core idea first. Then close the page and try the first problem yourself. Come back for the solution.
Assumed: you know basic C#. Classes, methods, generics at the List<T> level, and LINQ basics. The advanced part starts from there.
23 patterns · 92 problems · 6 guides · 11 advanced pages · one-page cheat sheet
Start here — the six guides
Not patterns. The mechanics around them.
The collections that matter, what each call costs, and the traps that turn O(n) into O(n²).
Read the input size as a hint. It tells you the complexity the interviewer expects.
Seven phases, a time budget, what to say when stuck, and a worked transcript.
Dry runs, five boundary checks, edge cases by input type, and quick tests in C#.
LINQ for shaping data, streaming big files, hand-coded statistics, sampling and SIMD.
The base class library calls you use daily, grouped by job, with the traps.
Part I — Arrays, Strings and Pointers
A contiguous run in an array or string, and you want the longest, shortest or best one.
Maximum Subarray of Size K
Longest Substring Without Repeating Characters
Minimum Size Subarray Sum
Minimum Window Substring
A sorted array, and you need a pair, a triplet, or an in-place compaction.
Two Sum II — Input Array Is Sorted
Remove Duplicates from Sorted Array
3Sum
Container With Most Water
A linked list or an implicit sequence, and you suspect a cycle or need the midpoint.
Linked List Cycle
Middle of the Linked List
Happy Number
Find the Duplicate Number
Part II — Ordering and Rearranging
Time ranges or numeric ranges that overlap, and you need them merged, counted or trimmed.
Merge Intervals
Insert Interval
Non-overlapping Intervals
Meeting Rooms II
Numbers drawn from a bounded range, and you must find what is missing or duplicated in O(1) space.
Missing Number
Find All Numbers Disappeared in an Array
Find the Duplicate Number
First Missing Positive
Pointer surgery on a linked list with no extra memory allowed.
Reverse Linked List
Reverse Linked List II
Reverse Nodes in k-Group
Part III — Trees and Graphs
Level-by-level processing, or the shortest path in an unweighted graph.
Binary Tree Level Order Traversal
Binary Tree Zigzag Level Order Traversal
Rotting Oranges
Word Ladder
Explore every path, compute something bottom-up, or flood-fill a region.
Maximum Depth of Binary Tree
Path Sum II
Number of Islands
Clone Graph
Part IV — Search, Selection and Optimisation
The K best, worst or most frequent items, from a large or streaming dataset.
Kth Largest Element in an Array
Top K Frequent Elements
K Closest Points to Origin
Enumerate every combination, permutation or arrangement, pruning the dead ends.
Subsets
Permutations
Combination Sum
N-Queens
Anything with a monotone yes/no boundary, sorted or rotated, in O(log n).
Binary Search
Search in Rotated Sorted Array
Find First and Last Position of Element in Sorted Array
Peak Index in a Mountain Array
An optimal value or a count of ways, built from overlapping subproblems.
Climbing Stairs
Coin Change
Longest Increasing Subsequence
0/1 Knapsack
Maximum Subarray (Kadane)
Longest Common Subsequence
Edit Distance
Part V — Aggregates, Stacks and Graphs
Range sums where a sliding window fails: negative values, exact targets, or divisibility.
Subarray Sum Equals K
Continuous Subarray Sum
Product of Array Except Self
Subarray Sums Divisible by K
For each element, the nearest larger or smaller one, and how far it reaches.
Daily Temperatures
Next Greater Element II
Trapping Rain Water
Largest Rectangle in Histogram
Ordering under dependencies, and detecting when no valid order exists.
Course Schedule
Course Schedule II
Alien Dictionary
Connectivity on a graph whose edges keep arriving, where re-running DFS is too slow.
Number of Connected Components
Redundant Connection
Accounts Merge
Number of Islands II
Part VI — More Patterns
Seven shapes that interviews ask often enough to learn on their own.
One pass, one local choice at each step, and an exchange argument that proves it safe.
Jump Game
Gas Station
Partition Labels
Task Scheduler
Many words, shared prefixes, and lookups by prefix or with wildcards.
Implement Trie
Design Add and Search Words
Word Search II
Replace Words
XOR cancels pairs, x & (x - 1) drops a bit, and an int can act as a set.
Single Number
Number of 1 Bits
Counting Bits
Sum of Two Integers
Merge K sorted inputs, or keep a running median with a heap on each side.
Merge k Sorted Lists
Kth Smallest Element in a Sorted Matrix
Find Median from Data Stream
Smallest Range Covering Elements from K Lists
Shortest path when edges cost different amounts: Dijkstra, 0-1 BFS, Bellman-Ford.
Network Delay Time
Path With Minimum Effort
Cheapest Flights Within K Stops
Swim in Rising Water
The best value in a moving window, in O(n) total.
Sliding Window Maximum
Shortest Subarray with Sum at Least K
Constrained Subsequence Sum
Longest Continuous Subarray With Absolute Diff Limit
Build a class where every operation is O(1) by pairing two structures.
LRU Cache
Min Stack
Insert Delete GetRandom O(1)
LFU Cache
Part VII — Advanced C#
Language features and runtime concepts that senior C# interviews probe. Each page has samples, pitfalls and interview questions.
Value vs reference, boxing, ref and in, ref struct, Span<T>, stackalloc, inline arrays.
Constraints, variance, static abstract members, generic math and generic attributes.
Closures and capture, static lambdas, local functions, events, function pointers.
Deferred execution, yield state machines, custom operators, IQueryable and expression trees.
Task vs ValueTask, deadlocks, cancellation, IAsyncEnumerable, Channels and WhenEach.
Every pattern kind, switch expressions, records, with, required and primary constructors.
Nullable reference types, null operators, checked math and exceptions done well.
The GC, dispose, pools, spans, unsafe code, frozen collections and measuring.
Reflection, attributes, expression trees, source generators, partial members, field.
Locks and the new Lock type, Interlocked, concurrent collections, PLINQ, classic tasks.
Every language version, newest first, with a full section on each C# 14 feature.
The 60-second decision list
What the question says, and the pattern to reach for.
“contiguous subarray”, “substring”, “window of size k” → Sliding Window
“sorted array” plus “pair / triplet / sum to target” → Two Pointers
“cycle”, “middle of the list”, “repeats forever” → Fast and Slow Pointers
“intervals”, “meetings”, “start and end times” → Merge Intervals
“array of n numbers in range 1..n”, “missing”, “duplicate” → Cyclic Sort
“reverse the list”, “in place”, “O(1) extra space” → List Reversal
“level by level”, “minimum number of steps”, “nearest” → BFS
“all paths”, “connected region”, “does a path exist” → DFS
“top K”, “K most frequent”, “K closest”, “median of a stream” → Heaps
“all subsets”, “all permutations”, “place N things without conflict” → Backtracking
“sorted”, “rotated”, “first index where…”, “minimise the maximum” → Binary Search
“how many ways”, “minimum cost”, “longest subsequence” → Dynamic Programming
“subarray sums to exactly k”, “values may be negative”, “divisible by k” → Prefix Sum
“next greater”, “span”, “histogram”, “how far until something taller” → Monotonic Stack
“prerequisites”, “build order”, “can all tasks be finished” → Topological Sort
“connected components”, “edges arrive one at a time”, “merge groups” → Union-Find
“can you reach the end”, “minimum number of…” with a provable local choice → Greedy
“prefix”, “dictionary of words”, “autocomplete”, “wildcard search” → Trie
“appears once, others twice”, “without + or -”, “count set bits” → Bit Manipulation
“K sorted lists”, “median of a stream”, “smallest range across lists” → K-way Merge / Two Heaps
“weighted edges”, “cheapest”, “minimum time to reach all” → Dijkstra
“maximum in every window”, “sum at least K” with negatives → Monotonic Deque
“design a class”, “O(1) get and put”, “evict least recently used” → Design
C# costs you should be able to quote
The short list. The full list, with the traps, is in Guide 1 .
List<T> index, Add: O(1). Add is amortised.
List<T>.Insert(0, x), RemoveAt(0): O(n). Use Queue<T> or LinkedList<T>.
Dictionary and HashSet lookup, add, remove: O(1) on average.
PriorityQueue Enqueue, Dequeue: O(log n). It is a min-heap. Pass a reversed comparer for a max-heap.
SortedSet and SortedDictionary: O(log n) per call. Red-black trees.
Array.Sort, List.Sort: O(n log n). Introsort, so not stable. Use OrderBy for a stable sort.
string + in a loop: O(n²). Use StringBuilder.
list.Contains(x): O(n). set.Contains(x): O(1).
Substring and a[i..j] on arrays: copy, O(j - i). AsSpan()[i..j] does not copy.
An original study companion for C# coding interviews. Part of CatZep.