搜狗桌面研究部笔试和面试

笔试题目:

1、#include <iostream>
using namespace std;

char * getMemory() {
char p[] = "hello world";//字符串常量
return p;
}

int main(){
char *str=NULL;
str = getMemory();
cout<<str<<endl; //结果不确定,但可以输出*str 为h; 但str输出为空;
return 0;
}

输出结果:不确定,静态存储区可能被释放,也可能没有;

main()
{
char *p = "hello world"; //warning: char *const p converted to char *;  
p[0] = 'H';  //修改字符串常量,运行错误,segmentation fault.
printf("%s", p);
}

A) Compile error
B) “Hello world”
C) Runtime error.
D) “hello world”

 2、编程题目

m个苹果放到n个盘子中的方案数目;注意 1,5,1,1 和 1,1,1,5是重复的,例如7,3 共8种方案;

思考:保持每个盘子里面的数为递增,然后用dfs求解,(方案数很多,考虑指定一种确定顺序) ;当时考虑失误,希望用组合数学的方法直接给出答案,这个思考被局限,其实题目还是蛮简单的。

7 = 0 + 7 = 0 + 0 + 7 (0 + 1 + 6 0+2+5 0+3+4 )

   = 1 + 6 = 1 + 1 + 5 

   = 2 + 5 = 

   = 3 + 4

代码如下:

  1. #include <iostream>
  2. using namespace std;
  3. int sum=0;
  4. void total(int m,int n,int start,int last) {
  5. if(start==n) {
  6. sum++;
  7. return;
  8. }
  9. for(int i=last;i<=m;i++) {
  10. if(m-i>=i) total(m-i,n,start+1,i);
  11. }
  12. }
  13. int main(){
  14. total(7,3,1,0);
  15. cout<<sum<<endl;
  16. return 0;
  17. }

4. 求解最长序列,输出最长公共序列

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. vector<int> res;
  6. int getLong(int input[],int n) {
  7. if(n==0) return 0;
  8. if(n==1) return input[0];
  9. vector<int> base(n,0);
  10. vector<int> pos(n,0);
  11. int maxIndex = 0;
  12. int longest = 0;
  13. for(int i=0;i<n;i++) {
  14. for(int j=i;j>=0;j--) {
  15. if(input[j] <= input[i]) {
  16. if(base[j]+1 > base[i]) {
  17. base[i] = base[j]+1;
  18. pos[i] = j;
  19. }
  20. }
  21. }
  22. if(base[i] > longest) {
  23. longest = base[i];
  24. maxIndex = i;
  25. }
  26. }
  27. while(maxIndex!=pos[maxIndex]) {
  28. res.push_back(input[maxIndex]);
  29. maxIndex = pos[maxIndex];
  30. }
  31. res.push_back(input[maxIndex]);
  32. return longest;
  33. }
  34. int main(){
  35. int input[]={2,1};
  36. int result = getLong(input,2);
  37. cout<<result<<endl;
  38. reverse(res.begin(),res.end());
  39. for(int i=0;i<res.size();i++)
  40. cout<<res[i]<<"\t"<<endl;
  41. return 0;
  42. }

不足:m个苹果放到n个盘子中,没有写出最优解,仅利用dfs实现了基本的组合,并进行了过滤(方法很复杂);另外最后一道题目,没有看清需要输出最长路径,最后进行了添加,时间不足,考虑欠佳,这里做了补充;

面试:

一面:

1、考察最有挑战的项目是什么?

2、算法题目,序列对的子图问题,自己给出了两种解决方案,第一种给出了hash<int,set>的方案,第二种给出了图的遍历问题,可以找出所有是可到达路径;

<A,B> <B,D> {A,B,D}

<C,E> <E,F> {C,E,F} 

保存到A[26][26]的二维数组,则有<A,B>的边,则A[0][1]= 1; 这样最后统计A可达到的所有点,然后这些点能够达到的所有点,遍历一遍即可组成一个集合,并对这个点进行标记;然后接着查找下一个未被标记的点;

二面:

1、实习主要工作是什么?

2、考虑对搜索热点词的理解? 如搜索框中最近搜索如何提示?

我给出了三种方案,第一种根据词典匹配,如近义词匹配 (不常用) 第二种:根据相同点击的query匹配(热点匹配) 第三种:垂直搜索匹配  第四种:最近搜索匹配(晚上想到的)

三面:主要是行为面试?

1.  解释自己为什么在各个公司实习时间那么短? 自己说的有点多了,惹人烦~ 下次注意。

2. python 实现文件排序? 表示当时对python的记忆仅限于基本的语法,完全忘记了如何用python实现结构体,所以就用了shell脚本实现,给出了python来调用shell脚本,很快的实现了。可能没有达到面试者的要求,不过绝对很奇怪,不知道问我这个问题有什么意思~

重写了,还是有点问题 sorted 不能对传入的参数进行修改

 

  1. def sortFile(file1,file2):
  2. fp1 = open(file1,"r")
  3. fp2 = open(file2,"w")
  4. lines = []
  5. for line in fp1.readlines():
  6. line = line.rstrip("\n")
  7. line = line.strip()
  8. if line == "":
  9. continue
  10. line = line.split()
  11. lines.append(line)
  12. fp1.close()
  13. lines = sorted(lines,key=lambda lin:int(lin[1]))  //python中按值传递,需要返回重新赋值,并且按照数字排序int(str)
  14. for line in lines:
  15. fp2.write(' '.join(str(v) for v in line)) //连接需要保证每一个都是str
  16. fp2.write("\n")
  17. fp2.close()
  18. file1 = "text1"
  19. file2 = "text2"
  20. sortFile(file1,file2)
posted @ 2014-09-09 23:38  purejade  阅读(303)  评论(0编辑  收藏  举报