Strong algorithm solutions come from a process: understand inputs/outputs, evaluate constraints, choose the best pattern/data structure, then build a clear step-by-step plan.
Example implementation of the framework using a Two Sum helper.
function solveTwoSum(nums: number[], target: number): number[] {
// Pattern chosen from constraints: hash map for O(n)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const needed = target - nums[i];
if (seen.has(needed)) {
return [seen.get(needed) as number, i];
}
seen.set(nums[i], i);
}
return [];
}