放c code求老师傅指教# JobHunting - 待字闺中
s*e
1 楼
update:
谢谢板上各位老师傅,现在我知道是我的问题了,“without using a second string
”我没有理解好,感谢大家赐教,我继续努力!
刚刚解决身份问题,男,找工作和leetcode都有一段时间了,最近碰到这家公司的一个
面试
http://stackoverflow.com/jobs/115265/software-engineer-networking-schweitzer-engineering?searchTerm=SEL&offset=3&sort=s
问的问题和glassdoor一样,所以我也准备了一下那个unions的答案和例子,以前没用
这个:
Interview Questions
1) What is in the software requirements?
2) What is mutex and semaphore?
3) When to use unions?
4) What are the pros and cons of using assembly in embedded systems?
5) Programming test: Write a function that will operate on a C-string and
convert all instances of "AB" to "C" without using a second string for
temporary storage.
Use the following function signature:
void translate(char *str)
So for example:
char astring[] = "helloABworld";
translate(astring);
// Now astring holds "helloCworld";
Your function should handle strings of arbitrary length. If you have any
questions about the problem, make reasonable assumptions and state your
assumptions in your reply.
正好我最近在用C的IPC 写一个server,client和cache的file transfer,主要用message
queue和named pipe做server和cache的sync, shared memory来communicate file,
所以30 分钟写这个题目,我以为无压力。
下面是我发回去的code:
#include
#include
#include
void helper(char *src, char *str, char *rep) {
int count = 0;
char *fre = strdup(src);
char *new = fre;
while (*new) {
count++;
new ++;
}
free(fre);
char buf[count];
char *p = strstr(src, str);
do {
if (p) {
memset(buf, '\0', count * sizeof(char));
if (src == p) {
strcpy(buf, rep);
strcat(buf, p + strlen(str));
} else {
strncpy(buf, src, strlen(src) - strlen(p));
strcat(buf, rep);
strcat(buf, p + strlen(str));
}
memset(src, '\0', strlen(src));
strcpy(src, buf);
}
} while (p && (p = strstr(src, str)));
}
void translate(char *str) { helper(str, "AB", "C"); }
int main(void) {
// assuming replacing all the "AB" with 'C',
// it could be replacing the first ,or Nth occurrence of "AB" with C only
char astring[] = "helloABworld";
// helper(astring, "AB", "C");
translate(astring);
puts(astring);
// char astring1[] = "helloABddABworld";
// helper(astring, "AB", "C");
// translate(astring1);
// puts(astring1);
return 0;
}
这个是我用来检查的script,可以跑,没看到有memory leak:
gcc -Wall --std=gnu99 -g3 -Wno-format-security -fno-omit-frame-pointer -o
sel sel.c
valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./sel
就是这样,我也被rej,周四下午发的code,今天周一10点收到rej。那天面试我的是一
个刚WSU毕业的小白HR intern,当时就觉得不对劲。打电话过来的时候还迟到20分钟,
没想到我就这样被她拿来练手了.
我以前在数学系写数值模拟的code,fortran,bash python用得比较多,c也用一点,做
的东西太偏向Government和NASA的基础研究需要,不好找工作就转行了
。
毫无疑问我只是一个新手,写出来的c估计有不合规范的地方,可是就SEL这个code 和
position requirement,不至于被这样据吧?
以前也不是没有收到rej,rej不给feedback也很正常是可以接受的,但是这次被这样一
个小白HR intern耍了,真的很不爽。
新手虚心请教板上各位老师傅,我写这个code是不是哪里有大问题我没有找出来?外国
人是不是投小公司的职位一般都没戏,应该专心刷leetcode?
谢谢板上各位老师傅,现在我知道是我的问题了,“without using a second string
”我没有理解好,感谢大家赐教,我继续努力!
刚刚解决身份问题,男,找工作和leetcode都有一段时间了,最近碰到这家公司的一个
面试
http://stackoverflow.com/jobs/115265/software-engineer-networking-schweitzer-engineering?searchTerm=SEL&offset=3&sort=s
问的问题和glassdoor一样,所以我也准备了一下那个unions的答案和例子,以前没用
这个:
Interview Questions
1) What is in the software requirements?
2) What is mutex and semaphore?
3) When to use unions?
4) What are the pros and cons of using assembly in embedded systems?
5) Programming test: Write a function that will operate on a C-string and
convert all instances of "AB" to "C" without using a second string for
temporary storage.
Use the following function signature:
void translate(char *str)
So for example:
char astring[] = "helloABworld";
translate(astring);
// Now astring holds "helloCworld";
Your function should handle strings of arbitrary length. If you have any
questions about the problem, make reasonable assumptions and state your
assumptions in your reply.
正好我最近在用C的IPC 写一个server,client和cache的file transfer,主要用message
queue和named pipe做server和cache的sync, shared memory来communicate file,
所以30 分钟写这个题目,我以为无压力。
下面是我发回去的code:
#include
#include
#include
void helper(char *src, char *str, char *rep) {
int count = 0;
char *fre = strdup(src);
char *new = fre;
while (*new) {
count++;
new ++;
}
free(fre);
char buf[count];
char *p = strstr(src, str);
do {
if (p) {
memset(buf, '\0', count * sizeof(char));
if (src == p) {
strcpy(buf, rep);
strcat(buf, p + strlen(str));
} else {
strncpy(buf, src, strlen(src) - strlen(p));
strcat(buf, rep);
strcat(buf, p + strlen(str));
}
memset(src, '\0', strlen(src));
strcpy(src, buf);
}
} while (p && (p = strstr(src, str)));
}
void translate(char *str) { helper(str, "AB", "C"); }
int main(void) {
// assuming replacing all the "AB" with 'C',
// it could be replacing the first ,or Nth occurrence of "AB" with C only
char astring[] = "helloABworld";
// helper(astring, "AB", "C");
translate(astring);
puts(astring);
// char astring1[] = "helloABddABworld";
// helper(astring, "AB", "C");
// translate(astring1);
// puts(astring1);
return 0;
}
这个是我用来检查的script,可以跑,没看到有memory leak:
gcc -Wall --std=gnu99 -g3 -Wno-format-security -fno-omit-frame-pointer -o
sel sel.c
valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./sel
就是这样,我也被rej,周四下午发的code,今天周一10点收到rej。那天面试我的是一
个刚WSU毕业的小白HR intern,当时就觉得不对劲。打电话过来的时候还迟到20分钟,
没想到我就这样被她拿来练手了.
我以前在数学系写数值模拟的code,fortran,bash python用得比较多,c也用一点,做
的东西太偏向Government和NASA的基础研究需要,不好找工作就转行了
。
毫无疑问我只是一个新手,写出来的c估计有不合规范的地方,可是就SEL这个code 和
position requirement,不至于被这样据吧?
以前也不是没有收到rej,rej不给feedback也很正常是可以接受的,但是这次被这样一
个小白HR intern耍了,真的很不爽。
新手虚心请教板上各位老师傅,我写这个code是不是哪里有大问题我没有找出来?外国
人是不是投小公司的职位一般都没戏,应该专心刷leetcode?