Reddit interview question
Implement a simplified memcached-style TCP key-value server with get and set
- Company:
- Type:
- Coding
- Difficulty:
- Medium
- Reported:
- October 19, 2026
Problem
Implement a TCP server that stores key-value pairs in memory and speaks a simplified version of the memcached text protocol. This is a practical backend coding round rather than an algorithm puzzle: the core of the task is parsing a byte stream correctly.
The prompt specifies line-based get and set commands, values framed by a byte count, and a multi-key get as a bonus; it does not spell out the exact syntax. A natural choice, used below, is memcached's own text protocol without its flags and expiry fields; the interviewer may adjust it. Commands are text lines ending in \r\n, keys contain no spaces or control characters, and values are arbitrary bytes.
- Set: the client sends
set <key> <bytes>\r\n, followed by exactly<bytes>bytes of data and then\r\n. The server stores the value and repliesSTORED\r\n. - Get: the client sends
get <key>\r\n. If the key exists, the server repliesVALUE <key> <bytes>\r\n<data>\r\n; in every case the reply ends withEND\r\n. - Bonus, multi-key get:
get <key1> <key2> ...\r\nreturns oneVALUEblock for each requested key that exists, in request order, followed by a singleEND\r\n.
How to answer malformed input is not specified, and any documented behavior is accepted. This solution follows memcached: an unknown or malformed command line gets ERROR\r\n, and a data block that is not followed by \r\n gets CLIENT_ERROR bad data chunk\r\n and is not stored.
TCP delivers a byte stream, not messages: a single read may contain half a command, several commands, or a value that itself contains \r\n. The store is shared by all client connections.
Example 1
Input: client sends "set greeting 5\r\nhello\r\n" then "get greeting missing\r\n"
Output: server sends "STORED\r\n" then "VALUE greeting 5\r\nhello\r\nEND\r\n"
The missing key produces no VALUE block; the reply still ends with END.
Example 2
Input: one connection receives "se" and then "t x 2\r\nhi\r\n" in two separate reads
Output: "STORED\r\n" after the second read
The server must buffer the incomplete line until the rest arrives.
Constraints
- Lines end with
\r\n; values are framed by their byte count, so they may contain any bytes, including\r\n. - Several clients may be connected at the same time.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern