推理题



  1. celebrity problem:一个party N个人,如果一个人不认识任何其他人,又被任何其他人认识,此人为celebrity。怎么找到这个celebrity.
    一个做法就是列一个N by N的表格, 然后用1表示认识, 0表示不认识, 如果有celebrity, 那么那一行应该是全1的, 然后剩下的就是一行一行的搜索, 所以时间复杂度是O(n^2).另一个更快的可以在O(n)时间里面解决, 具体解法是:把这些人编号1,2,3,4….n。1问2认不认识1, 2的回答只有两种, Yes, or No,如果是No, 1就不可能是celebrity, 保留2;如果是Yes, 2就不可能是celebrity, 保留1;然后如此类推, 这样每问一次可以消去1个人, 最后n-1次之后只有一个编号为 i 的人有可能是celebrity, (注意, 是可能而已, 我们还不能确定), 我们只要再扫一遍所有的人, 如果答案都是别人认识i, i不认识别人, 那么bingo, 我们找到了celebrity, 如果有例外, 那么就不存在celebrity.In total, time complexity is O(n)

  2. 史密斯夫妇邀请另外n对夫妇就餐,已知他们每个人都不和自己握手,不和自己的配偶握手,且不和同一个人握手一次以上。在大家见面握手寒暄后,史密斯先生问大家握手了几次,每个人的答案都不一样。问:史密斯太太握手几次?
    总共有2*n+2个人, 在史密斯先生问的2n+1个人当中, 很容易知道握手数是:0,1,……2n,(因为自己不可能和配偶握手所以不可能有人能握2n+1次)
    然后我们可以证明丈夫和夫人的握手数之和一定是2n,比如考虑到2n的那个人, 除了和配偶没有握手之外和其余的人都握手了, 那么其余的人至少握手一次, 那么说明0次的只能是2n人的配偶.(而且0次的人有且只有一个, 说明史密斯先生不可能是0次拉)。然后2n-1的人, 他没有和2个人握手, 1个是0次的那个人, 另一个自然是他的配偶, 这样其他人除了和2n的人握手了, 又都和2n-1的人握手了, 所以只有1次的那个人是2n-1的配偶.类推….. 这样只有 n次的那个人落单了, 那个人一定是Mrs. Smith.
    遇到这种问题, 如果实在没有思路, 那么就递推, 不要以为这样傻, 递推是个很有效的方法 一般都能推出通解来.那么 如果再多问一步, 那么史密斯先生握了几次手呢? 答案也是n次, (因为他和2n,…..n+1的人都握手了)

  3. 一个数组里面, 存放的是1–100的整数, 然后两两不同, 现在有且仅有一个数重复了一次, 请问怎么找出那个数.
    当然可以用hash table做, 只是这样需要O(n)的空间复杂度, 下面容重推出”异或”的方法.^表示异或(exclusive or) 1^0 = 1, 1^1 = 0^0 = 0;
    我们把s记作s=1^2^3…….^100, 然后把数组中的所有数的异或算出来 t=a[0]^…a[99],然后重复的数就是 s^t, (这个方法可以推广, 适用于一些更复杂的题目)

  4. 假设我们有m个黑球, n个白球, 一共放在一个罐子里面, 我们每次从里面拿2个球,我们现在采用这样的策略:
    (a) 如果是两个黑球或者是两个白球(即同色), 我们放回一个黑球,(b) 如果是一个黑球或者一个白球(即异色), 我们放回一个白球。然后放回的黑球和白球是从另外一个罐子里面拿的, 因为每次我们相当于净拿一个球出来, 所以最后一 定有一个情况, 就是罐子里面最后只剩一个球, 请问是什么颜色的球?

    同色 就放黑球, 不同色就放 白球, 诶, 如果记黑色为0, 白色为 1, 那么这不就是一个异或的表示法则嘛
    EX: 拿出两个黑球, 0^0 = 0, 异或出来结果的那个0 又被放进去了, 所以最后一个球是s = (0^0……m..^0)^(1^1…..n….^1)
    当n是偶数 结果 s = 0, 最后是黑球;当n是奇数 结果s = 1, 最后是白球

  5. 总共有n节楼梯, 假设我们的策略是, 每次只能走1步或者2步, 请问最后走上n节楼梯可能会有多少种可能?
     
    很巧妙的解法, 我们把到第n节阶梯可能的方法数记为f(n), 考虑到第n节的前一步, 要么是从n-1节阶梯那里踏1步过来, 要么是从n-2节阶梯那里踏两步过来,所以有 f(n) = f(n-1) + f(n-2), 而且很明显我们有f(1) = 1, f(2) = 2, 那么这就是一个Fibonacci数列了, 所以我们就知道f(n)的值了.


  6. Is your husband a cheat?  (http://toostep.com/insight/google-interview-questions-part-1)

    A certain town comprises of 100 married couples. Everyone in the town lives by the following rule: If a husband cheats on his wife, the husband is executed as soon as his wife finds out about him. All the women in the town only gossip about the husbands of other women. No woman ever tells another woman if her husband is cheating on her.  So every woman in the town knows about all the cheating husbands in the town except her own. It can also be assumed that a husband remains silent about his infidelity. One day, the mayor of the town announces to the whole town that there is at least 1 cheating husband in the town. What do you think happens?


    Answer:-

    Stumped? Let’s solve this methodically. Say there was only 1 cheating husband in the town. There will be 99 women who know exactly who the cheater is. The 1 remaining woman, who is being cheated on, would have assumed there are no cheaters. But now that the mayor has confirmed that there is at least one cheater, she realizes that her own husband must be cheating on her. So her husband gets executed on the day of the announcement.

    Now let’s assume there are 2 cheaters in the town. There will be 98 women in the town who know who the 2 cheaters are. The 2 wives, who are being cheated on, would think that there is only 1 cheater in the town.  Since neither of these 2 women know that their husbands are cheaters, they both do not report their husbands in on the day of the announcement. The next day, when the 2 women see that no husband was executed, they realize that there could only be one explanation – both their husbands are cheaters. Thus, on the second day, 2 husbands are executed.

    Through induction, it can be proved that when this logic is applied to n cheating husbands, they all die on the n th day after the mayor’s announcement.

  7. 5 Pirates Fight for 100 Gold Coins      (http://toostep.com/insight/google-interview-questions-part-1)

    Five pirates discover a chest containing 100 gold coins. They decide to sit down and devise a distribution strategy. The pirates are ranked based on their experience (Pirate 1 to Pirate 5, where Pirate 5 is the most experienced). The most experienced pirate gets to propose a plan and then all the pirates vote on it. If at least half of the pirates agree on the plan, the gold is split according to the proposal. If not, the most experienced pirate is thrown off the ship and this process continues with the remaining pirates until a proposal is accepted. The first priority of the pirates is to stay alive and second to maximize the gold they get. Pirate 5 devises a plan which he knows will be accepted for sure and will maximize his gold. What is his plan?

    Answer:-

    To understand the answer, we need to reduce this problem to only 2 pirates. So what happens if there are only 2 pirates. Pirate 2 can easily propose that he gets all the 100 gold coins. Since he constitutes 50% of the pirates, the proposal has to be accepted leaving Pirate 1 with nothing.

    Now let’s look at 3 pirates situation, Pirate 3 knows that if his proposal does not get accepted, then pirate 2 will get all the gold and pirate 1 will get nothing. So he decides to bribe pirate 1 with one gold coin. Pirate 1 knows that one gold coin is better than nothing so he has to back pirate 3. Pirate 3 proposes {pirate 1, pirate 2, pirate 3} {1, 0, 99}. Since pirate 1 and 3 will vote for it, it will be accepted.

    If there are 4 pirates, pirate 4 needs to get one more pirate to vote for his proposal. Pirate 4 realizes that if he dies, pirate 2 will get nothing (according to the proposal with 3 pirates) so he can easily bribe pirate 2 with one gold coin to get his vote. So the distribution will be {0, 1, 0, 99}.

    Smart right? Now can you figure out the distribution with 5 pirates? Let’s see. Pirate 5 needs 2 votes and he knows that if he dies, pirate 1 and 3 will get nothing. He can easily bribe pirates 1 and 3 with one gold coin each to get their vote. In the end, he proposes {1, 0, 1, 0, 98}. This proposal will get accepted and provide the maximum amount of gold to pirate 5.

    Bonus: Think about what would happen if there are 15 pirates or 25 pirates. Post the answer if you get it......

  8. 8 Identical Balls Problem     (http://toostep.com/insight/google-interview-questions-part-1)

    You are given 8 identical looking balls. One of them is heavier than the rest of the 7 (all the others weigh exactly the same). You a provided with a simple mechanical balance and you are restricted to only 2 uses. Find the heavier ball.

    Answer:-

     For convenience sake, let’s name the balls 1-8. First we weigh {1,2,3} on the left and {4,5,6} on the right. There are three scenarios which can arise from this.

    If the left side is heavier, then we know that one of 1, 2 or 3 is the heavier ball. Weigh {1} on the left and {2} on the right. By doing this, we will know if 1 or 2 is heavier. If they balance out, then 3 is the heavier one.

    If the right side is heavier, then we know that either 4, 5 or 6 is the heavier ball. Weigh {4} on the left and {5} on the right. By doing this we will know if 4 or 5 is heavier. If they balance out, then 6 is the heavier one.

    If {1,2,3} and {4,5,6} balance out, then we know either 7 or 8 is the heavier one. Weigh both of them to find out which one is heavier.

    Confused yet? or was it too easy? This is one of the basic identical ball problems. Here  is a more complex problem involving 12 balls with one fake.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值