【面試】微信支付一面


今天微信支付一面,在線面試。

在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++;
            }
        }
    }
}

這道題做了一小時吧,很蛋碎。寫完給面試官發了個短信,面試官給我回來電話,為了下大體的思考過程,然后技術面就結束了,竟然只讓做了一道題。之后問了下項目經歷,實習經歷什么的就完了。等二面中,希望能過吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM