-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem82.js
54 lines (49 loc) · 1.27 KB
/
Problem82.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Problem 82
//
// This problem was asked Microsoft.
//
// Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters.
//
// For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”.
//
// O(1) Time complexity
// O(1) Space complexity
class FileReader {
/**
* Initializs a file reader
*/
constructor(file) {
this.file = file;
this.offset = 0;
this.buffer = '';
}
/**
* Reads 7 characters from the file
* @return {string}
*/
read7() {
const start = this.offset;
const end = Math.min(this.offset + 7, this.file.length);
// update the offset
this.offset = end;
return this.file.substring(start, end);
}
/**
* Reads N characters using read7
* @param {number} n
* @return {string}
*/
readN(n) {
// adds extra characters to the buffer until the buffer's length is greater than or equal to n
while (this.buffer.length < n) {
const extra = this.read7();
if (extra.length === 0) break;
this.buffer += extra;
}
// updates buffer
const readResult = this.buffer.substring(0, n);
this.buffer = this.buffer.substring(n);
return readResult;
}
}
export default FileReader;