📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 两轮电面 8/26 9/7

全局:

2017(7-9月) 码农类General 本科 全职@google - 网上海投 - 技术电面  | | Fail | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
第一次是一个中国面试官,给一个sorted array,  定义popular number是array里出
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
能因为我是妹子,两道题都不难,是我自己太紧张脑子不转了。

上一篇:Amazon OA1
下一篇:问一个FB DESIGN题涉及到的CACHE一致性的问题

本帖被以下淘专辑推荐:

推荐
yiyizheliu 2016-9-29 09:57:13 | 只看该作者
全局:
第一题我倒是想到一个很类似binary search的方法:
从array a, index start to end中找最小的popular number:
求大家指正,或者有没有更好的方法
  1. index = (start + end)/4;
  2. if(a[index] == a[start]) return a[start];
  3. else{
  4.    find the first ele which is not equal to a[start];
  5.    set start there.
  6. }
复制代码
回复

使用道具 举报

🔗
ymsf 2016-9-29 08:59:40 | 只看该作者
全局:
是 “出现次数 >= array_size/4"吧
回复

使用道具 举报

🔗
 楼主| Clytze 2016-9-29 09:01:29 | 只看该作者
全局:
ymsf 发表于 2016-9-29 08:59
是 “出现次数 >= array_size/4"吧

对,手快打错了
回复

使用道具 举报

🔗
mren 2016-9-29 10:29:46 | 只看该作者
全局:
第一题有四个潜在答案,在n/4, 2n/4, 3n/4, n处,因为是有序的,所以依次做二分查找这四个位置的值的左右边界,如果长度够n/4就可以返回了.
第二题和leetcode的一个rearrange charactor相似,可以直接按照那个思路来做距离设为2.也就是先对字符统计个数,利用优先队列优先安排个数多的字符,2个为一组,完了个数减一再扔回优先队列.
回复

使用道具 举报

🔗
yiyizheliu 2016-9-29 10:32:34 | 只看该作者
全局:
写了一下第二题的代码,请大家critique一下:
  1. public class Reorder {
  2.         public List<String> solution(String s){
  3.                 //same method to get the permutation
  4.                 //but every time check the previous char in the result string
  5.                 //choose a different character
  6.                 List<String> result = new ArrayList<String>();
  7.                 Map<Character, Integer> counts = new HashMap<Character, Integer>();

  8.                 //get the counts
  9.                 getCounts(counts, s);

  10.                 getPermutation(counts, result, new StringBuffer(), s.length());
  11.                 return result;
  12.         }
  13.        
  14.         private void getPermutation(Map<Character, Integer> counts, List<String> result, StringBuffer sb, int size){
  15.                 if(sb.length() == size){
  16.                         result.add(sb.toString());
  17.                         return;
  18.                 }

  19.                 //when we add a char in the string, we decrement the corresponding count
  20.                 //as we need to backtrack, when the count == 0, we don't remove it
  21.                 //but we have to check the count each time
  22.                 for(char c : counts.keySet()){
  23.                         if(counts.get(c) == 0) continue;
  24.                         if(sb.length() != 0 && sb.charAt(sb.length() - 1) == c) continue;
  25.                         sb.append(c);
  26.                         counts.put(c, counts.get(c) - 1);
  27.                         getPermutation(counts, result, sb, size);
  28.                         sb.deleteCharAt(sb.length() - 1);
  29.                         counts.put(c, counts.get(c) + 1);
  30.                 }
  31.         }

  32.         private void getCounts(Map<Character, Integer> map, String s){
  33.                 for(int i = 0; i < s.length(); i++){
  34.                         map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
  35.                 }
  36.         }
  37.        
  38.         public void printResult(List<String> list){
  39.                 System.out.println("===== test =====");
  40.                 for(String s : list){
  41.                         System.out.print(s + "  ");                       
  42.                 }
  43.                 System.out.println();
  44.         }
  45.         public static void main(String[] args) {
  46.                 // TODO Auto-generated method stub
  47.                 Reorder ro = new Reorder();
  48.                 String test1 = "ab";
  49.                 ro.printResult(ro.solution(test1));
  50.                
  51.                 String test2 = "aaab";
  52.                 ro.printResult(ro.solution(test2));
  53.                
  54.                 String test3 = "aab";
  55.                 ro.printResult(ro.solution(test3));
  56.                
  57.                 String test4 = "acbd";
  58.                 ro.printResult(ro.solution(test4));
  59.         }

  60. }
复制代码
回复

使用道具 举报

🔗
WhatsFLAG 2016-9-29 10:45:20 | 只看该作者
全局:
第二题的确不难,第一题我有个思路感觉不够精炼,不知道楼主能否帮我矫正?用分治法来求左边出现次数最多的元素,判断是否大于四分之一
回复

使用道具 举报

🔗
 楼主| Clytze 2016-9-29 11:16:58 | 只看该作者
全局:
mren 发表于 2016-9-29 10:29
第一题有四个潜在答案,在n/4, 2n/4, 3n/4, n处,因为是有序的,所以依次做二分查找这四个位置的值的左右边 ...

第一题我后来跟同学讨论后得出最合理的解法应该就是你说的。
第二题统计次数后从个数最多的数开始,奇偶位置插空即可。
回复

使用道具 举报

🔗
dokolo 2016-9-29 11:23:58 | 只看该作者
全局:
4楼的解法是正解
不过可以加一些判断,比如连续两个分点都是一个元素直接返回该元素
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表