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

Two Sum - Spot the Bug (Java)

Find and fix the bug in this Java 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 (int 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

HashMap & HashSet in Javaref

Java HashMap<K,V> provides O(1) average put/get/containsKey. HashSet<E> wraps HashMap internally. Use getOrDefault() to avoid null checks. For thread safety, use ConcurrentHashMap.

-HashMap<K,V> — put(), get(), containsKey() all O(1)
-HashSet<E> — add(), contains(), remove() O(1)
-getOrDefault(key, defaultValue) simplifies counting
-entrySet() to iterate key-value pairs
HashMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.getOrDefault("b", 0);  // 0

HashSet<Integer> set = new HashSet<>();
set.add(1);
set.contains(1);  // true
Official docs →
HashMap & HashSet in Javaref

Java HashMap<K,V> provides O(1) average put/get/containsKey. HashSet<E> wraps HashMap internally. Use getOrDefault() to avoid null checks. For thread safety, use ConcurrentHashMap.

-HashMap<K,V> — put(), get(), containsKey() all O(1)
-HashSet<E> — add(), contains(), remove() O(1)
-getOrDefault(key, defaultValue) simplifies counting
-entrySet() to iterate key-value pairs
HashMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.getOrDefault("b", 0);  // 0

HashSet<Integer> set = new HashSet<>();
set.add(1);
set.contains(1);  // true
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
1class Solution {
2 public int[] twoSum(int[] nums, int target) {
3 HashMap<Integer, Integer> prevMap = new HashMap<>();
4 for (int i = 1; i < nums.length; i++) {
5 int num = nums[i];
6 int diff = target - num;
7 if (prevMap.containsKey(diff)) {
8 return new int[] { prevMap.get(diff), i };
9 }
10 prevMap.put(num, i);
11 }
12 return new int[] {};
13 }
14}