Implement a robust socket message reader
AppleSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of socket programming, stream message framing, internal buffer management, EOF and error handling, input size limits, unit testing, and time/space complexity analysis.
Implement an interface to read complete messages from a TCP socket where each recv (k) call may return any number of bytes (including 0). Build two variants: (A) length-prefixed messages with a 4-byte big-endian header exposed via readMessage() that returns exactly one message per call, and (B) newline-delimited messages exposed via readLine(). Maintain an internal buffer across calls, correctly handle partial reads, back-to-back messages, and EOF, and provide unit tests and complexity analysis.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.