Read N Characters Given Read4
Solution ApproachWas this helpful?
Easy•os-flavor•Expected: O(n) time, O(1) space
Problem
Given a method read4 that reads 4 characters at a time, implement read(buf, n) that reads n characters from a file.
Example 1:
Input: file = "abc", n = 4
Output: 3
Explanation: After calling read4, "abc" is read. Since only 3 characters exist, read returns 3.
Example 2:
Input: file = "abcde", n = 5
Output: 5
Explanation: read4 returns "abcd" first, then "e". Total 5 characters read.
Reference solution unlocks after your first submission
Loading...
