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

1Sliding Window2 Easy2 Medium

A contiguous run in an array or string, and you want the longest, shortest or best one.

2Two Pointers2 Easy2 Medium

A sorted array, and you need a pair, a triplet, or an in-place compaction.

3Fast and Slow Pointers3 Easy1 Medium

A linked list or an implicit sequence, and you suspect a cycle or need the midpoint.

Part II — Ordering and Rearranging

Time ranges or numeric ranges that overlap, and you need them merged, counted or trimmed.

5Cyclic Sort2 Easy1 Medium1 Hard

Numbers drawn from a bounded range, and you must find what is missing or duplicated in O(1) space.

Pointer surgery on a linked list with no extra memory allowed.

Part III — Trees and Graphs

Level-by-level processing, or the shortest path in an unweighted graph.

Explore every path, compute something bottom-up, or flood-fill a region.

Part IV — Search, Selection and Optimisation

The K best, worst or most frequent items, from a large or streaming dataset.

10Subsets (Backtracking)3 Medium1 Hard

Enumerate every combination, permutation or arrangement, pruning the dead ends.

11Modified Binary Search1 Easy3 Medium

Anything with a monotone yes/no boundary, sorted or rotated, in O(log n).

12Dynamic Programming1 Easy6 Medium

An optimal value or a count of ways, built from overlapping subproblems.

Part V — Aggregates, Stacks and Graphs

Range sums where a sliding window fails: negative values, exact targets, or divisibility.

14Monotonic Stack2 Medium2 Hard

For each element, the nearest larger or smaller one, and how far it reaches.

15Topological Sort2 Medium1 Hard

Ordering under dependencies, and detecting when no valid order exists.

16Union-Find3 Medium1 Hard

Connectivity on a graph whose edges keep arriving, where re-running DFS is too slow.

Part VI — More Patterns

Seven shapes that interviews ask often enough to learn on their own.

17Greedy4 Medium

One pass, one local choice at each step, and an exchange argument that proves it safe.

18Trie (Prefix Tree)3 Medium1 Hard

Many words, shared prefixes, and lookups by prefix or with wildcards.

19Bit Manipulation3 Easy1 Medium

XOR cancels pairs, x & (x - 1) drops a bit, and an int can act as a set.

Merge K sorted inputs, or keep a running median with a heap on each side.

21Weighted Shortest Paths3 Medium1 Hard

Shortest path when edges cost different amounts: Dijkstra, 0-1 BFS, Bellman-Ford.

22Monotonic Deque1 Medium3 Hard

The best value in a moving window, in O(n) total.

Build a class where every operation is O(1) by pairing two structures.

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.

C# costs you should be able to quote

The short list. The full list, with the traps, is in Guide 1.