Given read4k, implement readanysize
Given API: int Read4096(char* buf);
It reads data from a file and records the position so that the next time when it is called it read the next 4k chars (or the rest of the file, whichever is smaller) from the file. The return is the number of chars read.
Use above API to Implement API “int Read(char* buf, int n)” which reads any number of chars from the file.
http://www.careercup.com/question?id=14424684
Given read4k, implement readanysize | Hello World
The key here is to maintain a buffer of 4k by your self, and read from this buffer.
subsequent call should starts from where the last one ends.
https://github.com/mission-peace/interview/blob/master/src/com/interview/misc/Read4Function.java
public class Read4Function extends Read4{
class Queue {
int start;
int end;
int count;
char[] data;
int size;
Queue(int size) {
data = new char[size];
this.size = size;
}
boolean isEmpty() {
return count == 0;
}
void offer(char b) {
data[start] = b;
start = (start + 1)%size;
count++;
}
char poll() {
char d = data[end];
end = (end + 1)%size;
count--;
return d;
}
}
private final Queue queue;
public Read4Function() {
queue = new Queue(4);
}
public int read(char[] buf, int n) {
int r = 0;
while (!queue.isEmpty() && r < n) {
buf[r++] = queue.poll();
}
if (r == n) {
return r;
}
int index = 0;
int readSize = 0;
char[] input = null;
do {
input = new char[4];
readSize = read4(input);
index = 0;
while(r < n && index < readSize) {
buf[r++] = input[index++];
}
} while (readSize == 4 && r < n);
while (index < readSize) {
queue.offer(input[index++]);
}
return r;
}
Read full article from Given read4k, implement readanysize | Hello World
Given API: int Read4096(char* buf);
It reads data from a file and records the position so that the next time when it is called it read the next 4k chars (or the rest of the file, whichever is smaller) from the file. The return is the number of chars read.
Use above API to Implement API “int Read(char* buf, int n)” which reads any number of chars from the file.
http://www.careercup.com/question?id=14424684
Given read4k, implement readanysize | Hello World
The key here is to maintain a buffer of 4k by your self, and read from this buffer.
public
class
AnysizeRead {
private
final
int
SIZE =
4096
;
//assume you are given a method that reads 4k data, implement readanysize method using this method.
int
curr =
0
;
List<Integer> buff =
new
ArrayList<Integer>();
public
List<Integer> readAnySize(
int
size) {
List<Integer> result =
new
ArrayList<Integer>();
while
(size >
0
) {
//keep reading until finished reading all the size
if
(curr + size < SIZE) {
result.addAll(buff.subList(curr, curr + size));
curr += size;
size =
0
;
}
else
{
result.addAll(buff.subList(curr, buff.size()));
curr =
0
;
size -= SIZE - curr;
buff = read4k();
}
}
return
result;
}
}
String buffer = null;
int p = 0;
public String read(int n) {
if (n < 0) {
return null;
} else if (n == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
while (n > 0) {
// there is (LENGTH - p) chars left in the local buffer
if (buffer == null || p == buffer.length()) {
// no char left in buffer, update buffer
buffer = GoogleApi.read4096();
p = 0;
if (buffer.length() == 0) {
// finish reading the file (no more input chars)
break;
}
} else {
int numChars = buffer.length() - p;
if (numChars >= n) {
sb.append(buffer.substring(p, p + n));
p = p + n;
n = 0;
} else {
sb.append(buffer.substring(p));
p = buffer.length();
n -= numChars;
}
}
}
return sb.toString();
}
Read4K problem, followup, how to make it work with multiple calls, subsequent call should starts from where the last one ends.
https://github.com/mission-peace/interview/blob/master/src/com/interview/misc/Read4Function.java
public class Read4Function extends Read4{
class Queue {
int start;
int end;
int count;
char[] data;
int size;
Queue(int size) {
data = new char[size];
this.size = size;
}
boolean isEmpty() {
return count == 0;
}
void offer(char b) {
data[start] = b;
start = (start + 1)%size;
count++;
}
char poll() {
char d = data[end];
end = (end + 1)%size;
count--;
return d;
}
}
private final Queue queue;
public Read4Function() {
queue = new Queue(4);
}
public int read(char[] buf, int n) {
int r = 0;
while (!queue.isEmpty() && r < n) {
buf[r++] = queue.poll();
}
if (r == n) {
return r;
}
int index = 0;
int readSize = 0;
char[] input = null;
do {
input = new char[4];
readSize = read4(input);
index = 0;
while(r < n && index < readSize) {
buf[r++] = input[index++];
}
} while (readSize == 4 && r < n);
while (index < readSize) {
queue.offer(input[index++]);
}
return r;
}
Read full article from Given read4k, implement readanysize | Hello World