Data Structures and Algorithms • C# examples
A Repeatable DSA Problem-Solving Method
Turn an unfamiliar programming problem into testable cases, an algorithm, and a complexity claim.
Concept
A reliable solution starts with constraints and edge cases, not code. Clarify whether inputs can be empty, duplicated, negative, sorted, or too large for a particular numeric type. Build a simple baseline, prove its behavior, then optimize only when the constraints justify a more complex approach.
How to approach it
Restate the goal, work a small example by hand, identify invariants, write pseudocode, test boundaries, and state time and auxiliary-space costs.
Complexity
The cost depends on the selected algorithm and the actual input dimensions.
C# example
static int CountMatches(IReadOnlyList<int> values, int target)
{
int count = 0;
foreach (int value in values)
if (value == target) count++;
return count;
}
Expected result or behavior
The method counts every occurrence, including duplicates.
Practice exercise
Apply the same checklist to finding the first repeated number; include empty and one-element inputs.
Key takeaway: Correctness, constraints, and complexity belong in the solution before optimization.