由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Facebook phone screen
相关主题
wordbreak in C?how to access a const char array in a function
题目: string pattern matching w/ wildcard (.*)请问这样写程序错了吗?
分享A公司面经a MS interview question about C++
请教一道c/c++题 (转载)M面完焦急等待中。。。。。大家看看我是个什么水平?
请问strcpy()和memcpy()的写法问题谁能给个hashset实现的例子么?
leetcode上wild match请教一个字符串比较排序的问题 (转载)
也说两个面试题问到算法题和一道c++题
1道brianbench 的题 c++问一个memory allocate/release的问题
相关话题的讨论汇总
话题: buf话题: words话题: printf话题: ismember
进入JobHunting版参与讨论
1 (共1页)
J******8
发帖数: 132
1
The question is not hard. But I missed two key points.
The details below.
==========================================================
/*
example
char *words[] = {"foo", "bar", "baz", NULL};
setup(words);
1) First step:
isMember("foo") -> true
isMember("garply") -> false
2) Second step:
isMember("f.o") -> true
isMember("..") -> false
*/
#include
#include
#include
char *words[] = {"foo", "bar", "baz", "caz","cat",NULL};
int num=0;
void print_words(void)
{
int i=0;
while(words[i]){
printf("%s\n",words[i]);
i++;
}
}
/* arbitrary preprocessing. May be slow */
void setup(char *words[])
{
int i=0,j;
char temp[16];
while(words[i]){
i++;
}
num=i;

printf("num=%d\n",num);
//Brute force sorting
for(i=0;i for(j=i+1;j if(strcmp(words[i],words[j])>0){
strcpy(temp,words[i]);
strcpy(words[i],words[j]);
strcpy(words[j],temp);
}
}
}
//Missed: Can't do qsort() lib call here. !!!!
printf("sorted.\n");
}
//Missed: in this case, it does NOT make sense to compare as strcmp
//only matched or not.
int matchdot(const char *s1, const char *s2)
{
if(!s1||!s2)
return 0;
while(*s1&&*s2&&((*s1==*s2)||(*s2=='.'))){
s1++;
s2++;
}

if(*s1||*s2)
return 0;

return 1;
}
/* returns whether word is a member of words given in setup() as fast as
possible */
int isMember(char *word) {
int low=0,high=num-1;
int mid;

while(high>=low){
mid=low+(high-low)/2;
if(!strcmp(words[mid],word))
return 1;
else if(strcmp(words[mid],word)>0)
high=mid-1;
else
low=mid+1;
}

return 0;
}
int isMemberdot(char *word) {
int i=0;
while(words[i]){
if(matchdot(words[i],word))
return 1;
i++;
}

return 0;
}
int main (int argc, char *argv[])
{
char buf[16];

print_words();
setup(words);
print_words();

//1) test cases
strcpy(buf,"cat");
if(isMember(buf))
printf("isMember(%s)=Yes\n",buf);
else
printf("isMember(%s)=No\n",buf);

strcpy(buf,"cot");
if(isMember(buf))
printf("isMember(%s)=Yes\n",buf);
else
printf("isMember(%s)=No\n",buf);


//2) test cases
strcpy(buf,"cat");
if(isMemberdot(buf))
printf("isMemberdot(%s)=Yes\n",buf);
else
printf("isMemberdot(%s)=No\n",buf);

strcpy(buf,"cot");
if(isMemberdot(buf))
printf("isMemberdot(%s)=Yes\n",buf);
else
printf("isMemberdot(%s)=No\n",buf);

strcpy(buf,"c.t");
if(isMemberdot(buf))
printf("isMemberdot(%s)=Yes\n",buf);
else
printf("isMemberdot(%s)=No\n",buf);

strcpy(buf,"..");
if(isMemberdot(buf))
printf("isMemberdot(%s)=Yes\n",buf);
else
printf("isMemberdot(%s)=No\n",buf);

return 0;
}
===================
running results
l*****v
发帖数: 498
2
what is the question?

【在 J******8 的大作中提到】
: The question is not hard. But I missed two key points.
: The details below.
: ==========================================================
: /*
: example
: char *words[] = {"foo", "bar", "baz", NULL};
: setup(words);
: 1) First step:
: isMember("foo") -> true
: isMember("garply") -> false

l*********r
发帖数: 674
3
我觉得是写程序实现开头那个example的功能把。

【在 l*****v 的大作中提到】
: what is the question?
l*****v
发帖数: 498
4
这就是带wildcard的string match。没有*应该不难吧。
l*****v
发帖数: 498
5
面你的是不是个中国人

【在 J******8 的大作中提到】
: The question is not hard. But I missed two key points.
: The details below.
: ==========================================================
: /*
: example
: char *words[] = {"foo", "bar", "baz", NULL};
: setup(words);
: 1) First step:
: isMember("foo") -> true
: isMember("garply") -> false

n******n
发帖数: 215
6
问一下,他们用电话问问题阿?
那么多,难道用笔记下来吗?

【在 J******8 的大作中提到】
: The question is not hard. But I missed two key points.
: The details below.
: ==========================================================
: /*
: example
: char *words[] = {"foo", "bar", "baz", NULL};
: setup(words);
: 1) First step:
: isMember("foo") -> true
: isMember("garply") -> false

L******h
发帖数: 1191
7
同问

【在 n******n 的大作中提到】
: 问一下,他们用电话问问题阿?
: 那么多,难道用笔记下来吗?

J******8
发帖数: 132
8
No. A mostly white guy, fresh graduate.
Not friendly: *interrogated* me about
1) why want to leave current company
2) why choose facebook.
A bit bullshit. :-)
http://www.linkedin.com/pub/daniel-peek/7/571/b02

【在 l*****v 的大作中提到】
: 面你的是不是个中国人
J******8
发帖数: 132
9
http://collabedit.com/

【在 n******n 的大作中提到】
: 问一下,他们用电话问问题阿?
: 那么多,难道用笔记下来吗?

J******8
发帖数: 132
10
Not hard at all. But I focused on how to do wildcard matching, missed the
point to do linear lookup in dictionary in that case (binary search not
applicable any more).

【在 l*****v 的大作中提到】
: 这就是带wildcard的string match。没有*应该不难吧。
j*****u
发帖数: 1133
11
did he ask to use additional space to speed up lookup? ( is the source must
be an array)
otherwise the O(n) match is not interesting at all.
J******8
发帖数: 132
12
No, But I mentioned the best way is to build a trie, but that's too complex.
So let's do the binary search and/or linear search... He agrees. :-)
Do you have any better solution than trie?

must

【在 j*****u 的大作中提到】
: did he ask to use additional space to speed up lookup? ( is the source must
: be an array)
: otherwise the O(n) match is not interesting at all.

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一个memory allocate/release的问题请问strcpy()和memcpy()的写法问题
请教大家一道关于c++的面试题leetcode上wild match
H1B 概率最低事多少(对于adv degree)也说两个面试题
G家电面,这回肯定挂了。附面经。1道brianbench 的题 c++
wordbreak in C?how to access a const char array in a function
题目: string pattern matching w/ wildcard (.*)请问这样写程序错了吗?
分享A公司面经a MS interview question about C++
请教一道c/c++题 (转载)M面完焦急等待中。。。。。大家看看我是个什么水平?
相关话题的讨论汇总
话题: buf话题: words话题: printf话题: ismember