microsoft aptitude test

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).

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值