Read N Characters Given read4 II - Call Multiple Times - Problem

Imagine you're building a file reader that can only access files through a special API called read4. This API reads exactly 4 characters at a time from a file, but you need to create a flexible read method that can read any number of characters.

Here's the catch: your read method will be called multiple times, and you need to maintain state between calls to continue reading from where you left off.

The read4 API:

  • Reads up to 4 characters from the file
  • Returns the actual number of characters read (0-4)
  • Maintains its own file pointer that advances automatically
  • Once it reaches the end of file, it always returns 0

Your task: Implement a read(buf, n) method that:

  • Reads exactly n characters (or until end of file)
  • Stores the result in the provided buffer buf
  • Returns the actual number of characters read
  • Can be called multiple times and maintains proper state

Example: If file contains "abcdefgh" and you call read(buf, 3) then read(buf, 5), the first call should return "abc" and the second should return "defgh".

Input & Output

Basic Multiple Calls
$ Input: File: "abc", Calls: read(buf, 1), read(buf, 2), read(buf, 1)
โ€บ Output: Returns: 1, 2, 0, Buffers: ["a"], ["b", "c"], []
๐Ÿ’ก Note: First call reads 1 char 'a'. Second call reads remaining 2 chars 'bc'. Third call finds no more characters and returns 0.
Buffer Management Example
$ Input: File: "abcdefg", Calls: read(buf, 3), read(buf, 5)
โ€บ Output: Returns: 3, 4, Buffers: ["a", "b", "c"], ["d", "e", "f", "g"]
๐Ÿ’ก Note: First call gets 3 chars. Second call gets remaining 4 chars (not 5 because file only has 7 total characters).
Large Request Example
$ Input: File: "ab", Calls: read(buf, 10)
โ€บ Output: Returns: 2, Buffer: ["a", "b"]
๐Ÿ’ก Note: Request is larger than file size. Method returns only available characters and indicates actual count read.

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • At most 1000 calls will be made to read
  • 0 โ‰ค file.length โ‰ค 104
  • file contains only lowercase English letters
  • read4 can only be called, cannot be implemented by you

Visualization

Tap to expand
File"abcdefgh"read4() APIInternal Buffer[_, _, _, _]Size: 4Output Bufferresult[]Size: nread4()copy n chars๐Ÿ”ง State persists between multiple read() calls
Understanding the Visualization
1
Setup
Initialize internal buffer to store excess characters
2
Read Request
Check internal buffer first for leftover characters
3
Call read4
If needed, call read4 to get more characters
4
Buffer Management
Store unused characters for next call
Key Takeaway
๐ŸŽฏ Key Insight: Use an internal buffer as temporary storage to maintain perfect state between multiple API calls, ensuring no data loss and optimal performance.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
26.8K Views
High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen