bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Two Sum
šŸ›Spot the BugtypescriptEasyArrays & Hashing

Two Sum - Spot the Bug (TypeScript)

Find and fix the bug in this TypeScript solution

Problem Brief

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Puzzle Hints
  1. Test the line against the actual promise of Two Sum, not just whether the syntax looks valid.

  2. The repair changes only this statement shape: "for (let i = 1; i < nums.length; i++) {". Keep the same role, but make it match the invariant.

  3. Wrong initial value

Asked at 72 companies
AdobeAetionAffirm
Two Sum — HashMap Lookup
hashmap
nums271115
1
5

Input: [2,7,11,15], target=9. Use HashMap: complement → index

Map & Set in TypeScriptref

TypeScript adds type safety to JavaScript Map<K,V> and Set<T>. Generic parameters enforce key/value types at compile time. The API is identical to JavaScript but with stricter typing.

-Map<string, number> — typed key-value pairs
-Set<number> — typed unique values
-map.get(key) returns V | undefined
-Use Map over Record<> when keys are dynamic
const map = new Map<string, number>();
map.set('a', 1);
const val: number | undefined = map.get('a');

const set = new Set<number>([1, 2, 3]);
set.has(2);  // true (type-safe)
Official docs →
Map & Set in TypeScriptref

TypeScript adds type safety to JavaScript Map<K,V> and Set<T>. Generic parameters enforce key/value types at compile time. The API is identical to JavaScript but with stricter typing.

-Map<string, number> — typed key-value pairs
-Set<number> — typed unique values
-map.get(key) returns V | undefined
-Use Map over Record<> when keys are dynamic
const map = new Map<string, number>();
map.set('a', 1);
const val: number | undefined = map.get('a');

const set = new Set<number>([1, 2, 3]);
set.has(2);  // true (type-safe)
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
Inspect the code — click the line with the bug
1function twoSum(nums: number[], target: number): number[] {
2 let hash: { [key: number]: number } = {};
3 for (let i = 1; i < nums.length; i++) {
4 let diff = target - nums[i];
5 if (diff in hash) {
6 return [hash[diff], i];
7 } else {
8 hash[nums[i]] = i;
9 }
10 }
11}