Wrap a Fixed-Chunk Stream Reader with Arbitrary-Length Reads
Quick Overview
This Hudson River Trading software engineering question asks candidates to wrap a fixed-chunk stream reader with arbitrary-length reads. It is useful for practicing buffering, state carried across calls, and clean handling of end-of-stream behavior.
Wrap a Fixed-Chunk Stream Reader with Arbitrary-Length Reads
Company: Hudson River Trading
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Technical Screen
You are given a low-level stream API that reads exactly 4096 bytes at a time. Design a wrapper API `read(n)` that returns any requested number of bytes, including values smaller than 4096 or not divisible by 4096. Preserve unused bytes for future reads.
### Constraints & Assumptions
- The underlying stream returns up to or exactly 4096 bytes depending on EOF behavior.
- The wrapper may be called repeatedly.
- Unused bytes from a chunk must be buffered.
- The API should handle end of stream.
### Clarifying Questions to Ask
- Does the underlying API block until 4096 bytes or return fewer at EOF?
- Should `read(n)` return fewer than n bytes at EOF or raise?
- Can n be zero?
- Is the wrapper single-threaded?
- Should bytes or strings be returned?
### What a Strong Answer Covers
```premium-lock What a Strong Answer Covers
```
### Follow-up Questions
- How would you make it thread-safe?
- How would you support peek?
- How would buffering affect memory usage?
- How would you adapt this to async I/O?
Quick Answer: This Hudson River Trading software engineering question asks candidates to wrap a fixed-chunk stream reader with arbitrary-length reads. It is useful for practicing buffering, state carried across calls, and clean handling of end-of-stream behavior.
|Home/Software Engineering Fundamentals/Hudson River Trading
Wrap a Fixed-Chunk Stream Reader with Arbitrary-Length Reads
You are given a low-level stream API that reads exactly 4096 bytes at a time. Design a wrapper API read(n) that returns any requested number of bytes, including values smaller than 4096 or not divisible by 4096. Preserve unused bytes for future reads.
Constraints & Assumptions
The underlying stream returns up to or exactly 4096 bytes depending on EOF behavior.
The wrapper may be called repeatedly.
Unused bytes from a chunk must be buffered.
The API should handle end of stream.
Clarifying Questions to Ask Guidance
Does the underlying API block until 4096 bytes or return fewer at EOF?
Should
read(n)
return fewer than n bytes at EOF or raise?