Posted on ::

Protohackers is a set of network programming challenges. Each one asks you to build a server that speaks a specific protocol correctly under load.

I've completed the first six challenges in Go. Each solved README includes its test output as proof. The code is in the protohackers repo.

The challenges

  1. Smoke Test: a TCP echo server that handles several clients at once.
  2. Prime Time: a JSON-over-TCP server that answers whether a number is prime.
  3. Means to an End: a binary protocol for storing prices by timestamp and returning their mean.
  4. Budget Chat: a multi-user chat room over TCP.
  5. Unusual Database Program: a key-value store reached over UDP.
  6. Mob in the Middle: a proxy that rewrites Boguscoin addresses in chat messages.

Prime Time

Non-integers are never prime, and big.ProbablyPrime(0) gives an exact answer for values that fit in 64 bits:

num, _ := request.Number.Get()
var prime bool
if math.Trunc(num) != num {
    // Non-integers are automatically non-prime
    prime = false
} else {
    prime = big.NewInt(int64(num)).ProbablyPrime(0)
}
Table of Contents