2019 programming interview questions summary

Foreword

Code is just some ideas, some basis for comparison not be verified, if the problem or have better wording, welcome advice.

Byte beating

A topic

Reverse binary tree

void Fun(Tree * node){
    if(node!=nullptr){
        Fun(node->left);
        Fun(node->right);
        
        Tree * temp = node->left;
        node->left = node->right;
        node->right = temp;
    }
}

US Mission

A topic

In order to achieve the traversal cycle
following the cycle is panacea:

void Fun(Tree* node){
    stack<Tree *> trees;
    stack<bool> signs;
    
    trees.push(node);
    signs.push(false);
    
    while(trees.size()>0){
        Tree *tree = trees.top();
        bool sign = signs.top();
        
        trees.pop();
        signs.pop();
        
        if(tree!=nullptr){
            if(sign){
                cout<<tree->val<<endl;
            }else{
                //前序遍历
                //trees.push(tree->right);
                //trees.push(tree->left);
                //trees.push(tree);
                //signs.push(false);
                //signs.push(false);
                //signs.push(true);
                
                //中序遍历
                trees.push(tree->right);
                trees.push(tree);
                trees.push(tree->left);
                signs.push(false);
                signs.push(true);
                signs.push(false);
                
                //后序遍历
                //trees.push(tree->right);
                //trees.push(tree->left);
                //trees.push(tree);
                //signs.push(false);
                //signs.push(false);
                //signs.push(true);
            }
        }
    }
}

The results of the interviewer to the sentence, this method you need more of a bool type of memory, can not do without the extra space to achieve it?

//中序遍历
void Fun(Tree *node){
    stack<Tree*> trees;
    trees.push(node);
    node = node->left;
    
    while(trees.size()>0){
        if(node!=nullptr){
            trees.push(node);
            node = node->left;
        }else{
            cout<<trees.top()->val<<endl;
            node = trees.top();
            trees.pop();
        }
    }
}

So preorder traversal of it?

//前序遍历
void Fun(Tree *node){
    stack<Tree*> trees;
    trees.push(node);

    while(trees.size()>0){
        Tree *tree = trees.top();
        trees.pop();

        if(tree!=nullptr){
            cout<<tree->val<<endl;
            trees.push(tree->right);
            trees.push(tree->left);
        }
    }
}

So postorder it? Postorder seemingly not, ha ha, go panacea it.

Ape counseling

A topic

ASC has a array, moved to the right of the k bits:

1 2 3 4    k=0
4 1 2 3    k=1
3 4 1 2    k=2
...

Now we know a list, returns k.

//最简单的做法,找到那个不是升降排序的节点,返回即可
int Fun(vector<int> nums){
    for(int i=1;i<nums.size();++i){
        if(nums[i-1]>nums[i]){
            return i+1;
        }
    }
    return 0;
}

Then the interviewer said, the complexity of the algorithm is O (N), there is no easier. There, look for a half:

int Fun(vector<int> nums){
    //判断数组是否为空,且是否k=0;
    if(nums.size() == 0 || nums[0] < nums.back())
        return 0;
    
    int low=0;
    int high=nums.size()-1;
    
    while(low<high){
        int mid = (low + high) / 2;
        
        if(mid == low)
            break;
        
        if(nums[mid] > nums[low])
            low = mid;
        else if(nums[mid] < nums[high])
            high = mid;
    }
    
    return low;
}

Topic two

To the root node of a binary tree, to the value of two nodes, the minimum required to find their common parent. For example, the following tree:

To D, F, the minimum common parent A
to H, E, minimum common parent B

struct Tree{
    int val;
    Tree *left;
    Tree *right;
};

Tree * Fun(Tree* node,int A,int B){
    if(node!=nullptr){
        if(node->val == A || node->val == B)
            return node;
        
        Tree *left = Fun(node->left,A,B);
        Tree *right= Fun(node->right,A,B);
        
        if(left!=nullptr&&right!=nullptr)
            return node;
        else if(left!=nullptr)
            return left;
        else if(right!=nullptr)
            return right;
    }
    return node;
}

Topic three

An unordered list to request the list into odd and even chain, while the parity Yaoan list in ascending order.

struct List{
    int val;
    List * next;
};

List* Insert(List* root,List* node){
    if(root == nullptr){
        node->next = nullptr;
        return node;
    }else if(node->val<root->val){
        node->next = root;
        return node;
    }
    
    root->next = Insert(root->next,node);
    return root;
}

void Fun(List *root){
    List * odd = nullptr;
    List * even = nullptr;
    
    while(root!=nullptr){
        List * node = root;
        root = root->next;
        if(node->val%2==1){
            odd = Insert(odd,node);
        }else{
            even = Insert(even, node);
        }
    }
}

Tencent

first question

How to generate a random number between 0 and 100 and 100 can not be repeated.

void swap(int &x,int &y){
    int temp = x;
    x = y;
    y = temp;
}

void Fun(){
    vector<int> nums(100);
    //赋值
    for(int i=0;i<100;++i)
        nums[i] = i;
    
    //每次随机取一个数和最后面的数字进行交换
    for(int i=100;i>0;--i){
        int r = rand()%(i+1);
        swap(nums[r],nums[i-1]);
    }
}

The second question

Multiplying large numbers (multiplied by the array)

vector<int> Fun(vector<int> num1,vector<int> num2){
    vector<int> num(num1.size()+num2.size());
    
    //先不考虑进位,把每位乘得的结果加到对应的位置上去
    for(int i=(int)num1.size()-1;i>=0;--i){
        int index = (int)num1.size()-i-1;
        
        for(int j=(int)num2.size()-1;j>=0;--j,++index){
            num[index]+=num1[i]*num2[j];
        }
    }
    
    //准备进位
    for(int i=0,sign=0;i<num.size();++i){
        num[i]+=sign;
        sign = num[i]/10;
        num[i] = num[i]%10;
    }
    
    //去掉多余的位数
    while(num.back()==0)
        num.pop_back();
    
    //反转
    reverse(num.begin(), num.end());
    
    return num;
}

quick worker

first question

Binary tree traversal performed by the hierarchy.

void Fun(Tree * root){
    vector<Tree *> trees;
    trees.push_back(root);
    
    int index = 0;
    while(index<trees.size()){
        if(trees[index]!=nullptr){
            trees.push_back(trees[index]->left);
            trees.push_back(trees[index]->right);
        }
        
        ++index;
    }
    
    for(int i=0;i<trees.size();++i){
        if(trees[i] != nullptr){
            cout<<trees[i]->val<<" ";
        }
    }
}
Published 63 original articles · won praise 73 · views 70000 +

Guess you like

Origin blog.csdn.net/jjwwwww/article/details/100165968