Hash distribution skewed across shards
Review this C++ string hash used to shard keys across N backends.
Keys are arbitrary UTF-8 byte strings. Reports say some keys map to shard indices that look uneven and that two different keys collide in surprising ways across platforms.
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 22 min
Mark a line and say what kind of problem it is.0 findings
1class Router {
2public:
3 explicit Router(uint32_t shards) : n_(shards) {}
4
5 uint32_t shardFor(const std::string& key) const {
6 int h = 0;
7 for (char c : key) {
8 h = h * 31 + c;
9 }
10 return static_cast<uint32_t>(h) % n_;
11 }
12private:
13 uint32_t n_;
14};
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.