Modulo negative hash code
Review this Java bucketing helper that maps an object to one of `n` buckets.
`hashCode()` can return any 32-bit int, and `n` is the bucket count (e.g. 16).
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 16 min
Mark a line and say what kind of problem it is.0 findings
1class BucketMap<V> {
2 private final List<List<V>> buckets;
3 private final int n;
4
5 BucketMap(int n) {
6 this.n = n;
7 this.buckets = new ArrayList<>(n);
8 for (int i = 0; i < n; i++) buckets.add(new ArrayList<>());
9 }
10
11 int bucket(Object key) {
12 return key.hashCode() % n;
13 }
14
15 void put(Object key, V value) {
16 buckets.get(bucket(key)).add(value);
17 }
18}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.