PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/System Design/Apple

Implement a robust socket message reader

Last updated: May 10, 2026

Quick Overview

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.

  • medium
  • Apple
  • System Design
  • Software Engineer

Implement a robust socket message reader

Company: Apple

Role: Software Engineer

Category: System Design

Difficulty: medium

Interview Round: Technical Screen

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.

Quick Answer: 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.

Related Interview Questions

  • Design a smartwatch sensor subsystem - Apple (hard)
  • Design CI/CD for AI Services - Apple (medium)
  • Design TikTok Data Engineering Systems - Apple (medium)
  • Design ad click aggregator and file sync service - Apple (medium)
  • Design an Accurate Click Aggregator - Apple (medium)
Apple logo
Apple
Sep 6, 2025, 12:00 AM
Software Engineer
Technical Screen
System Design
2
0

Implement Message Framing Readers for TCP Sockets

Context

You are given a TCP socket. Each call to recv(k) may return any number of bytes up to k; in blocking mode, a return of 0 indicates EOF (peer closed). You must not assume a recv will return the full amount requested. Messages may arrive in pieces or back-to-back. Implement a reader that maintains an internal buffer across calls and extracts exactly one message per API call.

Use any mainstream language; Python 3 is acceptable for clarity and unit-testing.

Requirements

Build two variants backed by an internal buffer that persists across calls:

  1. Length-prefixed messages
    • The protocol is: a 4-byte big-endian unsigned integer N, followed by exactly N bytes of payload.
    • API: readMessage() returns exactly one payload as bytes (or string, if you choose), blocking until complete or raising on EOF/error.
  2. Newline-delimited messages
    • Messages are delimited by '\n'. Treat an optional preceding '\r' (CRLF) as part of the delimiter and return the line without it.
    • API: readLine() returns exactly one line (without the delimiter), blocking until complete or raising on EOF/error.

Both variants must:

  • Maintain an internal buffer across calls.
  • Correctly handle partial reads, back-to-back messages, and EOF.
  • Provide reasonable guardrails (e.g., a max message/line size to avoid unbounded memory growth) and clear error signaling when constraints are violated.

Also provide:

  • Unit tests covering partial reads, back-to-back messages, empty message/line, EOF during header/body, and size limit violations.
  • Time and space complexity analysis, including any amortization arguments.

Solution

Show

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More System Design•More Apple•More Software Engineer•Apple Software Engineer•Apple System Design•Software Engineer System Design
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.