microsoft aptitude test

来源:互联网 发布:php 清除缓存 编辑:程序博客网 时间:2024/05/17 02:51

I took the microsoft aptitude test on april 10th 2011.the test content is as follows:

 

1.time complexity of quicksort

the optimal time complexity of quicksort is O(nlogn)
the worst time complexity of quicksort is O(n^2)
the average time complexity of quicksort is O(nlogn)


2. size of class.
class T
{
    int b;
    static int a;
};
sizeof(T)=4. sizeof will not count static data member in.

in x86 32 bit processor. memory address will be aligned by 4.

class T
{
    Test(){}
    ~Test(){}
};
sizeof(T)=1

class Test
{
    Test(){}
    virtual ~Test(){}
    int a;
};

sizeof(T)=8. if a class contains a virutal function then the class has to contains a pointer that  points to a virtual table of the class.


3. calculate max sum for substrings.

the complexity of calculating max substring sum is O(n). it can be implemented by dynamic

programming.
int test(int * array,int n)
{
    int temp_max-0,max=0;
    for(int i = 0 ; i < n ;++i)
    {
        temp_max+=a[i];
        if(temp_max > max){
            max=temp_max;
        }
        else if(temp_max<0){
            temp_max = 0;
        }
    }
    return max;
}


4.binary tree traversal.

the post traversal of a binary tree is DEBFCA. so the possible pre traversal of the tree is:
ABDECF or ABEDCF or ABDEFC or ABEDFC. if we have a post traversal of a binary tree then we might
have more than 2 pre traversal result.because in post traversal both left child and right child will
be visited before parent node.so if we determine the parent node then left and right nodes can be
swapped which produce 2 results. the same principle can be applied to other nodes in the tree.


5. probability theory

Two 20*20 squares are drawn randomly in a 100*100 square. the probability that two squares are
overlapped or cover each other is?
(40*40)/(80*80)*0.36 + (40*20)/(80*80)*0.48 + (20*20)/(80*80)*0.16 = 0.16
in above formula:we divide the big square into three area: center area, edge area and corner area.
the proportion of each area in the big square is 36%,48% and 16%,respectively.
we will calculate overlap probability in each area and add them together to generate the final result.

 

6.time complexity of finding the max distance between two nodes in the weighted binary tree.

it can be resolved by dynamic programming.

dp formula is dp[i,j] = dp[i,f[j]]+weight(j,f(j)).

dp means distance between node i and node j

weight(i,j)means the weight of edge between node i and j.

f(j) means parent node of node j

the dp formula means distance between node i and j equals to distance between node i and parent node of j added by weight of edge between node j and parent node of j.

dp can be retrieved with time O(n^2) so we can get max distance between two nodes in the tree with time O(n^2).

原创粉丝点击