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

Two Sum - Spot the Bug (Rust)

Find and fix the bug in this Rust 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: "let diff = target + n;". Keep the same role, but make it match the invariant.

  3. Wrong operator: should be subtraction not addition

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 Rustref

Rust std::collections provides HashMap<K,V> and HashSet<T>. Use entry() API for elegant insert-or-update. The borrow checker ensures safe concurrent access. or_insert(default) and or_insert_with(|| ...) handle missing keys.

-HashMap::new() — O(1) average insert/get/remove
-entry(key).or_insert(default) — insert if missing
-get(&key) returns Option<&V>
-HashSet — insert/contains/remove O(1)
use std::collections::{HashMap, HashSet};

let mut map = HashMap::new();
map.insert("a", 1);
*map.entry("b").or_insert(0) += 1;

let mut set = HashSet::new();
set.insert(42);
set.contains(&42);  // true
Official docs →
HashMap & HashSet in Rustref

Rust std::collections provides HashMap<K,V> and HashSet<T>. Use entry() API for elegant insert-or-update. The borrow checker ensures safe concurrent access. or_insert(default) and or_insert_with(|| ...) handle missing keys.

-HashMap::new() — O(1) average insert/get/remove
-entry(key).or_insert(default) — insert if missing
-get(&key) returns Option<&V>
-HashSet — insert/contains/remove O(1)
use std::collections::{HashMap, HashSet};

let mut map = HashMap::new();
map.insert("a", 1);
*map.entry("b").or_insert(0) += 1;

let mut set = HashSet::new();
set.insert(42);
set.contains(&42);  // 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
1impl Solution {
2 pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
3 let mut map = HashMap::new();
4 for (i, n) in nums.into_iter().enumerate(){
5 let diff = target + n;
6 if let Some(&j) = map.get(&diff){
7 return vec![i as i32, j as i32];
8 }else{
9 map.insert(n, i);
10 }
11 }
12 unreachable!()
13 }
14}