2018 Huawei Written Exam

Well, I tested Huawei last night. Still no one passed, I feel that the first question should be passed all, but the result is not satisfactory. But overall, there are certain results.
The first question, leetcode05, inputs a string and outputs the largest palindrome string. The specific algorithm description is written in question 05, but it is not necessary to output the string, only the length of the string. However, the code of question 05 can only pass a part of 40% of the examples. I don’t know why. In theory, there should be no problems. The main exam does not give example questions, so it is impossible to know what examples are outputted. There are problems, so there is no way to optimize the problem. Then I wrote another solution, but only 20% over. The procedure is as follows:
#include <iostream>
#include <string>
using namespace std;

int LargestString(string s){
    if(s.size()==0)
        return 0;
    int i=0;
    int j=s.size()-1;
    int answer=0;
    while(i<j){
        while(s[i]!=s[j]){
            --j;
        }
        while(i<j&&s[i++]==s[j--]){
            answer+=2;
        }
        if(i<j&&s[i]!=s[j]){
            answer=0;
            continue;
        }
    }
    return answer;
}

int main(){
    string s;
    while(cin>>s){
        int i=LargestString(s);
        cout<<i<<endl;
    }
    return 0;
}
该算法的问题,早上想了想可能主要问题出在i没有控制++在循环的内部,当出现将answer置零情况时,未对i和j做出相对应的改变。然后就是05题的算法,具体可以参考 点击打开链接中我所分析的算法,但是调试的时候竟然只通过40%就很奇怪,而且最奇怪的时我改变返回参数,对反参的answer+1的时候依然能够通过40%。但+2的时候则为0%。主要没给测试案例,就很难判断主要问题出在哪里。(难道是有特殊字符?)

第二题

总体算来算是业务代码吧,比较实用,和算法的关系不是很大,但是对于做过前端或者说时有自己搭建系统的同学来说这部分应该会比较熟悉。总体题目比较长,但是其主要实现的是对不同字段的IPV6的地址信息进行一个判断,主函数使用一个switch对不同的情况进行输出。而功能函数则是对不同输入的字符串进行判断,代码如下:

#include <iostream>
#include <string>
using namespace std;

int DifferentType(string s){
    if(s.size()==0||s.size()>39)
        return 0;
    if(s=="::")
        return 1;
    if(s=="::1")
        return 2;
    string tmp=s.substr(0,4);
    if(tmp=="FE80")
        return 3;
    if(tmp=="FEC0")
        return 4;
    if(tmp=="FF00")
      return 6;
    else 
        return 5;
}

int main(){
    string s;
    while(cin>>s){
        switch(DifferentType(s)){
            case 1:
                cout<<"Unspecified"<<endl;
            case 2:
                cout<<"Lookback"<<endl;
            case 3:
                cout<<"LinkLocal"<<endl;
            case 4:
                cout<<"SiteLocal"<<endl;
            case 5:
                cout<<"GlobalUnicast"<<endl;
            case 6:
                cout<<"Multicast"<<endl;
            case 0:
                cout<<"Error"<<endl;
        }
    }
    return 0;
}
This is the initial prototype. Because the pass rate is not high, it is optimized later, mainly because the main function has no break, and the judgment of the intermediate code is not very accurate. Finally, the output code:
#include <iostream>
#include <string>
using namespace std;

int DifferentType(string s){
    if(s.size()==0||s.size()>39)
        return 0;
    if(s=="::")
        return 1;
    if(s=="::1")
        return 2;
    string tmp=s.substr(0,2);
    string tmp1=s.substr(0,3);
    if(tmp1=="FE8"||tmp1=="FE9"||tmp1=="FEA"||tmp1=="FEB")
        return 3;
    if(tmp=="FE")
        return 4;
    if(tmp=="FF")
      return 6;
    else
        return 5;
}

int main(){
    string s;
    while(cin>>s){
        switch(DifferentType(s)){
            case 1:
                cout<<"Unspecified"<<endl;
                break;
            case 2:
                cout<<"Lookback"<<endl;
                break;
            case 3:
                cout<<"LinkLocal"<<endl;
                break;
            case 4:
                cout<<"SiteLocal"<<endl;
                break;
            case 5:
                cout<<"GlobalUnicast"<<endl;
                break;
            case 6:
                cout<<"Multicast"<<endl;
                break;
            case 0:
                cout<<"Error"<<endl;
        }
    }
    return 0;
}
In the end, I passed 57%, um, I still failed to pass.

The third question, sorry I forgot to save it. . Probably an optimal solution. And only 20% passed, but the code is not saved, I will leave it to you to solve it yourself
 
Generally speaking, the difficulty of the written test questions for Huawei interns is not very difficult compared to NetEase and Ali, but they still need to have clear ideas and solutions. Special circumstances should be considered. It's really a bit uncomfortable if you only pass part of the test but not all of it, after all, there are no test cases. . . There is really no change in thinking. On the importance of test cases for test engineers, hahaha!



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728617&siteId=291194637