搜狗桌面研究部笔试和面试
笔试题目:
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);
}
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
代码如下:
- #include <iostream>
- using namespace std;
- int sum=0;
- void total(int m,int n,int start,int last) {
- if(start==n) {
- sum++;
- return;
- }
- for(int i=last;i<=m;i++) {
- if(m-i>=i) total(m-i,n,start+1,i);
- }
- }
- int main(){
- total(7,3,1,0);
- cout<<sum<<endl;
- return 0;
- }
4. 求解最长序列,输出最长公共序列
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- vector<int> res;
- int getLong(int input[],int n) {
- if(n==0) return 0;
- if(n==1) return input[0];
- vector<int> base(n,0);
- vector<int> pos(n,0);
- int maxIndex = 0;
- int longest = 0;
- for(int i=0;i<n;i++) {
- for(int j=i;j>=0;j--) {
- if(input[j] <= input[i]) {
- if(base[j]+1 > base[i]) {
- base[i] = base[j]+1;
- pos[i] = j;
- }
- }
- }
- if(base[i] > longest) {
- longest = base[i];
- maxIndex = i;
- }
- }
- while(maxIndex!=pos[maxIndex]) {
- res.push_back(input[maxIndex]);
- maxIndex = pos[maxIndex];
- }
- res.push_back(input[maxIndex]);
- return longest;
- }
- int main(){
- int input[]={2,1};
- int result = getLong(input,2);
- cout<<result<<endl;
- reverse(res.begin(),res.end());
- for(int i=0;i<res.size();i++)
- cout<<res[i]<<"\t"<<endl;
- return 0;
- }
不足: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 不能对传入的参数进行修改
- def sortFile(file1,file2):
- fp1 = open(file1,"r")
- fp2 = open(file2,"w")
- lines = []
- for line in fp1.readlines():
- line = line.rstrip("\n")
- line = line.strip()
- if line == "":
- continue
- line = line.split()
- lines.append(line)
- fp1.close()
- lines = sorted(lines,key=lambda lin:int(lin[1])) //python中按值传递,需要返回重新赋值,并且按照数字排序int(str)
- for line in lines:
- fp2.write(' '.join(str(v) for v in line)) //连接需要保证每一个都是str
- fp2.write("\n")
- fp2.close()
- file1 = "text1"
- file2 = "text2"
- sortFile(file1,file2)