Coding Tasks in C#
A coding task in an interview rarely tests a clever algorithm. It tests basic reflexes: can you walk an array without an off-by-one; do you understand why concatenating strings in a loop is quadratic; do you notice that a HashSet turns an O(n) lookup into an O(1) one. Mistakes here are cheap on paper, but they are exactly what separates confident code from "seems to work".
This topic is a set of recurring techniques that most simple tasks are built from: how to traverse an array, how to work with a string, when to reach for recursion versus iteration, what hashing buys you, and what "in place" means. Each layer below dissects one technique on a small example with an explicit complexity note.
Topic map
- Array traversal — the index loop, off-by-one, converging two pointers.
- String manipulation —
stringis immutable,char.IsLetter/ToLower, palindrome check with two pointers. - Recursion — the base case, the step, factorial and the overflow trap.
- Iterative algorithms — folding recursion into a loop, a rolling pair, Fibonacci in O(1) space.
- Hashing —
HashSet/Dictionaryfor O(1) lookups, removing duplicates in one pass. - In-place algorithms — O(1) extra space, reversal and swapping without a temporary.
Common traps
| Mistake | Consequence |
|---|---|
| Off-by-one in loop bounds | A skipped or extra element, IndexOutOfRangeException on Length |
| Reversing an array all the way to the end | Every pair is swapped twice — the array returns to its original order |
+ concatenation in a loop for a string | O(n²) work and a stream of throwaway strings for the GC instead of O(n) with StringBuilder |
A factorial base case returning 0 | The whole product is zeroed — n! is always 0 |
Assuming long cannot overflow | 21! already overflows long silently — without checked the error slips by |
| Naive double recursion for Fibonacci | Exponential O(2ⁿ) instead of O(n): the same values are recomputed |
| A nested loop for finding duplicates | O(n²) where a HashSet gives O(n) with O(1) checks |
| XOR or arithmetic swap | Zeroes the element when i == j, and arithmetic can also overflow int |
Interview relevance
These tasks almost always come at the start of an interview as a warm-up and a filter: what matters is not the "right answer" but how you reason about bounds, complexity, and memory. A candidate who says the loop invariant and the O(·) estimate out loud sails through; one who codes blindly trips on the empty input or i == j.
Typical checks:
- A careful array traversal with no off-by-one and correct handling of empty/single-element input.
- Understanding
stringimmutability and the cost of concatenation. - A correct recursion base case and overflow awareness.
- The ability to rewrite recursion as iteration with O(1) space.
- When
HashSet/Dictionaryremove quadratic complexity. - The difference between "in place" (O(1)) and a solution with extra memory.
Common wrong answer: reducing everything to a one-liner with LINQ (s.Reverse(), set.ToArray()) and claiming O(1). That opens the discussion of the real cost — hidden allocations, lost order, and how "in place" differs from "in one line".