Ali Online Written Test algorithm engineer additional questions

Source: Internet
Author: User

A few days ago to participate in Ali's online written test, the post is an algorithmic engineer, the written test feeling of moderate difficulty, the choice of data structure, discrete mathematics, small intelligence problems and some reading program selected results of the topic. Where the data structures and permutations are the most. At that time was more flustered, did not record. Only three additional questions were noted.

The first question is simple. Requires implementing a method to find the median in two sequential (ascending) array of integers. Pass in 4 parameters, which are two arrays and their size, respectively. Since the topic is simple, it should be more efficient to write. I used the idea of merging sorts, merging two arrays, and finding the median in the process of merging. and to discuss the odd-even situation, note that even if there may be decimals. The code is as follows:

Double findmediansortedarrays (int a[],int m,int b[],int n) {    int sum=m+n;    Double tmp = 0.0;    if (sum%2==0)    {        int pivot=0;        sum/=2;        int i=0,j=0;        while (Pivot<sum)        {            if (A[i]<b[j])            {                tmp=a[i];i++;p ivot++;            }            else            {                tmp=b[j];j++;p ivot++;            }        }        if (A[i]<b[j])            tmp+=a[i];        else            Tmp+=b[j];        tmp/=2;    }    else    {        int pivot=0;        Sum= (sum>>1) +1;        int i=0,j=0;        while (Pivot<sum)        {            if (A[i]<b[j])            {                tmp=a[i];i++;p ivot++;            }            else            {                tmp=b[j];j++;p ivot++;    }}} return TMP;}


The second question is that in 2.5D games, there is a possibility of overlapping and occlusion of individual objects. such as trees, houses and other items. How to tell which object is selected when the player clicks on a place. The question is, what data structures and algorithms are selected, and the pseudo-code is given. I don't know the question, but I feel a little bit like the visibility of learning in graphic learning. One of the founders of Graphic science has said that graphics are sort of. It means that when there is more than one object to display, which object is obscured which object is displayed, sort it out. Therefore, I was more shameless to choose the array and sorting algorithm. Write a simple pseudo-code, hoping to muddle through.

The third question is an ACM problem, more difficult. At that time cut a picture, do not know whether there is a copyright problem.


The difficulty of this problem lies in the requirement of time complexity and space complexity. The solution to Violence is O (n^2), where space complexity requires no additional requests for an array of D with size n. Fortunately, an array is passed in, and we can traverse it from front to back. The way I come up with this is:

Use two temporary variables Maxl and MAXR to record the maximum value when traversing between the two ends. The smaller side of Max takes action, the action content is to access the next element, and if this element is less than Max, then Max does not change, the difference between the element and Max is the amount of water. If the element is greater than Max,max, the element is updated. Until two cursors meet.

The code is as follows:

#include <iostream>using namespace Std;int volume (int *height,int n) {    int max_l=height[0];    int max_r=height[n-1];    int i=0,j=n-1;    int result = 0;    while (i!=j)    {        if (max_l<=max_r)        {            i++;            if (height[i]<max_l)                result+=max_l-height[i];            else                max_l=height[i];        }        else        {            j--;            if (height[j]<max_r)                result+=max_r-height[j];            else                max_r=height[j];        }    }    return result;} int main () {    int array[]={1,0,2,1,0,1,3,2,1,2,1};    int result = Volume (array,11);    cout << result << Endl;    return 0;}


Ali Online Written Test algorithm engineer additional questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.