Code Room
Code reviewHardcr-g264
Subject Integer overflowLevel Senior–Staff~22 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Talk through your review
Code to reviewcpp
class Router {public:    explicit Router(uint32_t shards) : n_(shards) {}     uint32_t shardFor(const std::string& key) const {        int h = 0;        for (char c : key) {            h = h * 31 + c;        }        return static_cast<uint32_t>(h) % n_;    }private:    uint32_t n_;};
Run or narrate your approach, then ask the coach.