由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - fb面试题【转】
相关主题
昨天的F家店面FG题目包子求教--read4096
FB临门一脚挂了,那种郁闷悔恨的感觉.请问strcpy()和memcpy()的写法问题
请教一个fb面试问题a MS interview question about C++
问 Implement readline using read4096fb面试题【转】
讨论一下FB的经典题read和readline吧用 c 实现的字符串 permutation,求批评指点
小公司面经这个拷贝构造函数有什么问题?
求问下面这几行代码是做什么的,非常感谢!IBM高级软件工程师的示例代码,大家看看有多少个bug?
readLine和balanceParanthesis的code谁写了?leetcode 的新题好像不太理解题意
相关话题的讨论汇总
话题: bufsize话题: char话题: output话题: outputsize话题: buf
进入JobHunting版参与讨论
1 (共1页)
h*****g
发帖数: 312
1
Implement a function
char* readLine();
which returns single lines from a buffer. To read the buffer, you can makes
use of a function
int read(char* buf, int len)
which fills buf with upto len chars and returns the actual number of chars
filled in. Function readLine can be called as many times as desired. If
there is no valid data or newline terminated string available, it must block
. In order to block, it can use read function which in turn will block when
it doesn't have anything to fill the buf.
这题得咋整呢?
h********w
发帖数: 221
2
int read(char* buf, int len) returns the number of chars? it's weird, most
time the return value should be same same the len parameter, are you sure
you give the clear explanation about this question?
Anyway, mark and wait answer
c*****n
发帖数: 96
3
char* readLine(){
static char[BUFSIZE] buf;
static char* ptr = buf;
static int bufSize = 0;
int outputSize = 0;
char *output = NULL;

while (1){
int pos = getNewLinePos(ptr, bufSize);
if (pos > 0){
// found new line char in the buffer
output = realloc(output, outputSize+pos+1); // one extra char for '
\0'
// TODO: check realloc return value
memcpy (output + outputSize, ptr, pos);
output[outputSize + pos] = '\0';
ptr += pos;
bufsize -= pos;
return output;
}else{ // no new line char in the buffer, push all data in buffer
// to the output and read from underneath buffer
// TODO: check if bufSize == 0 (?)
output = realloc(output, outputSize + bufSize);
memcpy(output+outputSize, ptr, bufSize);
ptr = buf;
bufSize = 0;
bufSize = read(ptr, BUF_SIZE);
}
}
}
int getNewLinePos(char *p, int len){
int i = 0;
while (i < len){
if (*(p+i) == '\n')
return (i+1);
i++;
}
return 0;
}
n*******s
发帖数: 482
4
readline()内部要有一个buffer来缓存以减少IO operation.
assume 该缓存是
static char buff[MAX]
static int size ; // current bond of the data in buffer
static int index ; // index of current reader pointer in the buffer
static char resultBuff[MAX]
另外 几个case需要考虑到
(1) there is a '\n' between index and size : just copy them out and return.
and update index.
(2) no '\n' between index and size, now things get complicated
no matter what, need to save the current data between index and max to
resultBuffer, set index to 0
(2.1) if size == MAX
size = read(buf, MAX) ; // call read again.
(2.2) if size < MAX
size = read(buf, MAX);
return readline() // then recursively call
h*****g
发帖数: 312
5
多谢你的解答 觉得 //1 处的outputSize 应该时刻更新吧?
char* readLine(){
static char[BUFSIZE] buf;
static char* ptr = buf;
static int bufSize = 0;
int outputSize = 0;
char *output = NULL;

while (1){
int pos = getNewLinePos(ptr, bufSize);
if (pos > 0){
// found new line char in the buffer
output = realloc(output, outputSize+pos+1); // one extra char for '
\0'
// TODO: check realloc return value
memcpy (output + outputSize, ptr, pos);
output[outputSize + pos] = '\0';
ptr += pos;
bufsize -= pos;
return output;
}else{ // no new line char in the buffer, push all data in buffer
// to the output and read from underneath buffer
// TODO: check if bufSize == 0 (?)
output = realloc(output, outputSize + bufSize); //1
memcpy(output+outputSize, ptr, bufSize);
ptr = buf;
bufSize = 0;
bufSize = read(ptr, BUF_SIZE);
}
}
}
int getNewLinePos(char *p, int len){
int i = 0;
while (i < len){
if (*(p+i) == '\n')
return (i+1);
i++;
}
return 0;
}

【在 c*****n 的大作中提到】
: char* readLine(){
: static char[BUFSIZE] buf;
: static char* ptr = buf;
: static int bufSize = 0;
: int outputSize = 0;
: char *output = NULL;
:
: while (1){
: int pos = getNewLinePos(ptr, bufSize);
: if (pos > 0){

c*****n
发帖数: 96
6
Thanks for pointing it out. I updated the code.

【在 h*****g 的大作中提到】
: 多谢你的解答 觉得 //1 处的outputSize 应该时刻更新吧?
: char* readLine(){
: static char[BUFSIZE] buf;
: static char* ptr = buf;
: static int bufSize = 0;
: int outputSize = 0;
: char *output = NULL;
:
: while (1){
: int pos = getNewLinePos(ptr, bufSize);

1 (共1页)
进入JobHunting版参与讨论
相关主题
leetcode 的新题好像不太理解题意讨论一下FB的经典题read和readline吧
请教个LC的新题小公司面经
LC的简单版Read4是这样吗?求问下面这几行代码是做什么的,非常感谢!
C的fscanf的问题 (转载)readLine和balanceParanthesis的code谁写了?
昨天的F家店面FG题目包子求教--read4096
FB临门一脚挂了,那种郁闷悔恨的感觉.请问strcpy()和memcpy()的写法问题
请教一个fb面试问题a MS interview question about C++
问 Implement readline using read4096fb面试题【转】
相关话题的讨论汇总
话题: bufsize话题: char话题: output话题: outputsize话题: buf