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

Given a file and assume that you can only read the file using a given method read4, implement a method read to read n characters. Your method read may be called multiple times.

Method read4:

The API read4 reads four consecutive characters from file, then writes those characters into the buffer array buf4.

The return value is the number of actual characters read.

Note that read4() has its own file pointer, much like FILE *fp in C.

Definition of read4:

Parameter: char[] buf4
Returns: int

buf4[] is a destination, not a source. The results from read4 will be copied to buf4[].

Method read:

By using the read4 method, implement the method read that reads n characters from file and store it in the buffer array buf.

Consider that you cannot manipulate the file directly. The file is only accessible for read4 but not for read.

The read function may be called multiple times.

Definition of read:

Parameters: char[] buf, int n
Returns: int

buf[] is a destination, not a source. You will need to write the results to buf[].

Note:

  • Consider that you cannot manipulate the file directly. The file is only accessible for read4 but not for read.
  • The read function may be called multiple times.
  • Please remember to RESET your class variables declared in Solution, as static/class variables are persisted across multiple test cases.
  • You may assume the destination buffer array, buf, is guaranteed to have enough space for storing n characters.
  • It is guaranteed that in a given test case the same buffer buf is called by read.

Input & Output

Example 1 — Multiple Read Calls
$ Input: File: "abc", Operations: [read(1), read(2), read(1)]
Output: ["a", "bc", ""]
💡 Note: First call reads 1 character "a". Second call reads 2 characters "bc". Third call has no more characters, returns empty string.
Example 2 — Buffer Management
$ Input: File: "abcde", Operations: [read(4), read(1)]
Output: ["abcd", "e"]
💡 Note: First read4 call gets "abcd", all used. Second call needs 1 more character, read4 gets "e" (last character).
Example 3 — Excess Buffering
$ Input: File: "abcdefg", Operations: [read(3), read(2)]
Output: ["abc", "de"]
💡 Note: First call: read4 gets "abcd", uses "abc", buffers "d". Second call: uses buffered "d" + reads "e" from next read4.

Constraints

  • 1 ≤ file.length ≤ 500
  • file consists of only lowercase English letters and digits
  • 1 ≤ queries.length ≤ 10
  • 1 ≤ queries[i] ≤ 500

Visualization

Tap to expand
Read N Characters Given read4 II INPUT File Content: 'a' 'b' 'c' EOF Operations: read(1) read(2) read(1) read4 API: Reads up to 4 chars from file at a time buf4[4] buffer ALGORITHM STEPS 1 Maintain State Keep buf4 + bufPtr + bufCnt between read() calls 2 Check Buffer First Use leftover chars from previous read4 calls 3 Call read4 if needed When buffer empty and more chars needed 4 Copy to Output Fill buf until n chars or EOF reached Internal State: bufPtr = 0 bufCnt = 0 buf4[4] FINAL RESULT Call 1: read(1) read4 --> "abc" "a" OK Call 2: read(2) Use buffered "bc" "b" "c" OK Call 3: read(1) EOF reached "" OK Output: ["a", "bc", ""] Key Insight: Buffered State Management Unlike single read(), multiple calls require persistent state. Store leftover characters from read4() in an internal buffer between calls. Track buffer pointer and count to efficiently serve future reads without re-reading from file. This transforms a stateless API into stateful sequential access. TutorialsPoint - Read N Characters Given read4 II - Call Multiple Times | Buffered State Management
Asked in
Google 45 Facebook 38 Microsoft 25 Amazon 20
28.5K Views
Medium-High Frequency
~35 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