您的位置:首页 > 职场人生

【面试】微信支付一面

2016-03-23 18:26 831 查看
今天微信支付一面,在线面试。

在collabedit上面进行的,一开始面试官给我打了个电话,说在collabedit上面出好了题目,让做完之后给他回个短信。然后没有多余的废话,就上了collabedit开始做题了。

两个题目,二选一

第一题:在一个二叉树中找到所有节点的内容包含某子串的节点,并使用快速排序方法找到顺序为第n位的节点值。排序规则如下:子串出现的次数,如果次数一样则按字符数排序,如果字符数一样则按ascii排序。

第二题:一个公司有很多员工,也有主管,每天员工和主管都需要签到,但主管可以用签到机确认有多少人上班,也可以按员工ID顺序或签到顺序打印出上班的员工,还可以找出倒数第n个上班的员工是谁。
要求:
请用OO的方法分析和实现
所有操作的时间消耗和空间消耗越低越好,其中排序算法时间复杂度不能超过O(nlogn),极端情况下也不可以退化为n^2

考虑到第二题还得设计什么的,直接选了更为直接的第一题-算法题。

第一题给了TreeNode的定义和函数接口

truct TreeNode
{
char* value;
struct TreeNode * left, * right;
}; struct TreeNode * findNode(struct TreeNode * head,char * substr, int n)
{
//to do
}


其实这个题考查的还是挺多的,字符串匹配(KMP),树的遍历(前中后),前k大元素(堆,快排思想),其实在线面试最看重的是代码风格,然后是思路,最后只要保证人眼bug free就好了。

下面是我做题时实现的代码(不保证无bug,因为没跑过)

struct TreeNode
{
char* value;
struct TreeNode * left, * right;
};

vector<pair<TreeNode*, int>> nodes;

struct TreeNode * findNode(struct TreeNode * head,char * substr, int n)
{
fill_nodes(head, substr);
TreeNode* result = quick_find(nodes, 0, nodes.size()-1, n);
}

TreeNode* quick_find(vector<pair<TreeNode*, int>>& nodes, int left, int right, int n){
if(left == right) return nodes[left].first;
pair<TreeNode*, int> tmp = nodes[0];
int r = right, l = left;
while(l < r){
while(l < r && is_small(tmp, nodes[r])) r--;
if(l < r){
nodes[l] = nodes[r];
l++;
}
while(l < r && is_small(nodes[l], tmp)) l++;
if(l < r){
nodes[r] = nodes[l];
r--;
}
}
nodes[l] = tmp;
if(l == n) return nodes[l].first;
if(l < n) return quick_find(nodes, l+1, right, n);
else return quick(nodes, left, l-1, n);
}

bool is_small(pair<TreeNode*, int> node_count1, pair<TreeNode*, int> node_count2){
if(node_count1.second < node_count2.second) return true;
if(node_count1.second > node_count2.second) return false;
int str_len1 = 0, str_len2 = 0;
char* str1 = (node_count1.first)->value;
char* str2 = (node_count2.first)->value;
for(int i = 0; str1[i] != '\0'; i++) str_len1++;
for(int i = 0; str2[i] != '\0'; i++) str_len2++;
if(str_len1 < str_len2) return ture;
if(str_len1 > str_len2) return false;
for(int i = 0; str1[i] != '\0'; i++){
if(str2[i] > str1[i]) return false;
}
return true;
}

void fill_nodes(TreeNode* head, char* substr){
if(head == nullptr) return;
int count = strstr(head->value, substr);
if(count > 0) nodes.push_back(make_pair(head, count));
fill_nodes(head->left, substr);
fill_nodes(head->right, substr);
}

int strstr(char* str, char* substr){
if(str == nullptr || substr == nullptr) return 0;
int sub_len = 0;
for(int i = 0; substr[i] != '\0'; i++) sub_len++;
int* next = new int[sub_len];
fill_next(substr, next);
int i = -1, j = -1;
while(str[i+1] != '\0' && substr[j+1] != '\0'){
if(str[i+1] == substr[j+1]){
i++; j++;
}else{
while(j > -1 && str[i+1] != substr[j+1]) j = next[j];
if(str[i+1] == substr[j+1]){
i++; j++;
}else{
i++;
}
}
}
if(substr[j+1] != '\0') return 0;
else return 1 + strstr(str+i-sub_len+2);
}

void fill_next(char* substr, int* next){
next[0] = -1;
int t = -1; int i = 1;
for(;substr[i] != '\0';){
if(substr[t+1] == substr[i]){
next[i] = t+1;
t++; i++;
}else{
while(t > -1 && substr[t+1] != substr[i]) t = next[t];
if(substr[t+1] == substr[i]){
next[i] = t+1;
t++; i++;
}else{
next[i] = -1;
i++;
}
}
}
}


这道题做了一小时吧,很蛋碎。写完给面试官发了个短信,面试官给我回来电话,为了下大体的思考过程,然后技术面就结束了,竟然只让做了一道题。之后问了下项目经历,实习经历什么的就完了。等二面中,希望能过吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: