Hard · System Design

Read N Characters Given Read4 II - Call Multiple Times

Given read4(buf4), which reads up to 4 characters from a file into buf4 and returns how many it read (0 at the end of the file), implement read(buf, n) that copies up to n characters into buf and returns how many it copied; read may be called multiple times on the same file.

Examples

Example 1

{
  "chunks": ["abc"],
  "reads": [1, 3, 1]
}

Output: [ {"count": 1, "text": "a"}, {"count": 2, "text": "bc"}, {"count": 0, "text": ""} ]

Example 2

{
  "chunks": ["abcd"],
  "reads": [3, 2]
}

Output: [ {"count": 3, "text": "abc"}, {"count": 1, "text": "d"} ]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems