Google 面试题 phone interview

摘自一亩三分地


http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=200350&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3086%5D%5Bvalue%5D%3D6%26searchoption%5B3086%5D%5Btype%5D%3Dradio%26searchoption%5B3089%5D%5Bvalue%5D%5B2%5D%3D2%26searchoption%5B3089%5D%5Btype%5D%3Dcheckbox%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311

一个三姐,给一个数字集合,数字是0-9,没有重复,输出由其中的数字构成的所有整数,该整数小于某一个特定的整数
比如:数字集合[1,2,3], 特定整数130。 输出: 1,2,3, 11,12,13,21,22,23,31,32,33, 111,112,113,121,122,123. (下一个数字131 > 130), 输出的顺序无所谓。


2 change the position between the string array and index array

http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=199569&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3086%5D%5Bvalue%5D%3D6%26searchoption%5B3086%5D%5Btype%5D%3Dradio%26searchoption%5B3089%5D%5Bvalue%5D%5B2%5D%3D2%26searchoption%5B3089%5D%5Btype%5D%3Dcheckbox%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311

description:

输入是一个字符串数组,一个int数组,输入的字符串数组是另外一个字符串数组通过int数组变换得到的,int数组的值代表的是原来这位置上的字符串经过变换后的坐标,然后输出是求变换之前的字符串数组,要求用线性时间,o(1)额外空间
. from: 1point3acres.com/bbs 
打个比方,比如一个字符串数组是"cat", "rabbit","dog", "mouse",int数组给的2,0,3,1,意思是string数组第0个词是cat,它本来的位置是在哪呢,我们要看int数组,int数组的0在index 1上,所以说cat之前应该是1号位的,同理rabbit在string数组的1号位,而index数组3号位的值是1,说明rabbit这个词之前应该在3号位上的,依次类推,所以变换前的字符串数组应该是 dog, cat, mouse, rabbit

再打个比方,如果输入是Cat mouse dog rabbit和2,0,1,3,输出也会是dog, cat, mouse, rabbit. visit 1point3acres.com for more.
. 1point3acres.com/bbs
再打个比方,如果输入是Cat mouse dog rabbit, tiger, lion和2,0,1,3,5,4,输出会是dog, cat ,moutse, rabbit, lion, tiger

这个题感觉从变换前的数组求变换后的数组很好做,假设string数组是S,int数组是A, 直接一位一位循环遍历,当A[i] != i 时候把A[i] 与 A[A[i]]以及S[i] 与S[A[i]]一直调换就可以了. more info on 1point3acres.com

但这个题是要从变换后的数组求变换前的数组,做了半天也没做出个好的解法,估计跪了

public class solution{
   public String[] solve(String[] stringArray, int[] indexArray){
    
     for(int i = 0; i < indexArray.length; i++){
          if(index[i] == i){
           continue;}
          else{
             while(index[i] != i){
         swapString(stringArray, index[i], i);
         swapString(indexArray, index[i], i);
                 index[i] = i;
            }
          }
     } return stringArray;
   }
   public void swapString(String[] stringArray, int x, int y){
       String tem = stringArray[x];
       stringArray[x] = stringArray[y];
      stringArray[y] = tem;
   }
   public void swapIndex(int[] indexArray, int x, int y){
     int tem = indexArray[x];
     indexArray[x] = indexArray[y];
     indexArray[y] = tem;
 }
}


3 http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=200078&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3086%5D%5Bvalue%5D%3D6%26searchoption%5B3086%5D%5Btype%5D%3Dradio%26searchoption%5B3089%5D%5Bvalue%5D%5B2%5D%3D2%26searchoption%5B3089%5D%5Btype%5D%3Dcheckbox%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311

昨天一个美国小哥的电面. leetcode的原题361. Bomb Enemy 要求O(n)解决, 他没做出来, 这哥们连leetcode是啥都不知道, 感觉只有中国人和阿三刷的比较厉害.1point3acres缃�


4 http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=200114&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3086%5D%5Bvalue%5D%3D6%26searchoption%5B3086%5D%5Btype%5D%3Dradio%26searchoption%5B3089%5D%5Bvalue%5D%5B2%5D%3D2%26searchoption%5B3089%5D%5Btype%5D%3Dcheckbox%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311

给你一个多叉树。移出所有子树,当这个子树的全部节点的值的和为0。
我都写完了,但是中间出了几个bug. 代码也有点长,效果很不好。挂了。

public class solution{
#use the post-order traversal and if the sum equals to zero, set the node to null
}


5 http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=198685&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3086%5D%5Bvalue%5D%3D6%26searchoption%5B3086%5D%5Btype%5D%3Dradio%26searchoption%5B3089%5D%5Bvalue%5D%5B2%5D%3D2%26searchoption%5B3089%5D%5Btype%5D%3Dcheckbox%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311


第一题:Input一个string,判断它是否存在一种permutation是palindrome。
follow up: 如何test这个function。
正确回答是阐述一遍测试体系unit test, integration test, leak test, performance test 等等……
小哥人很好,我没说到的他都给我科普了一遍。
第二题:Hamming Weight,也就是count bits 1 in a number.
写完O(n)了以后不出所料地让优化。

注意测试体系

https://en.wikipedia.org/wiki/Unit_testing

https://en.wikipedia.org/wiki/Integration_testing


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值