Code RoomSame key computes multiple times
MediumPrep Room Coding #2201

Same key computes multiple times

Code reviewConcurrencyMid–Senior~28 min

Review this Java in-memory cache with lazy compute.

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 28 min
Mark a line and say what kind of problem it is.0 findings
1public class ResultCache {
2 private final Map<String, BigInteger> cache = new ConcurrentHashMap<>();
3 
4 public BigInteger get(String key) {
5 if (!cache.containsKey(key)) {
6 BigInteger v = expensiveCompute(key); // seconds long
7 cache.put(key, v);
8 }
9 return cache.get(key);
10 }
11}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.