Code RoomInventory goes negative
HardPrep Room Coding #2168

Inventory goes negative

Code reviewConcurrencySenior–Staff~20 min

Review this Rust inventory decrementer shared across worker threads via Arc<Mutex<...>>.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 20 min
Mark a line and say what kind of problem it is.0 findings
1fn reserve(stock: &Arc<Mutex<HashMap<u64, i32>>>, sku: u64, n: i32) -> bool {
2 let available = {
3 let map = stock.lock().unwrap();
4 *map.get(&sku).unwrap_or(&0)
5 };
6 if available < n {
7 return false;
8 }
9 let mut map = stock.lock().unwrap();
10 *map.entry(sku).or_insert(0) -= n;
11 true
12}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.