回复: 27
跳转到指定楼层
上一主题 下一主题
收起左侧

facebook 新鲜电面

全局:

2016(4-6月) 码农类General 硕士 全职@meta - 内推 - 技术电面  | | Pass | 应届毕业生

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

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

x
上周四面的fb,面试官人非常nice,在讨论的过程中我感觉就像大家在聊天,分享自己的经验和感悟。题目我认为是之前merge matrix的变种,Given k sorted lists of O(n) integers each, impl
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
运,加油

上一篇:新鲜的 Bloomberg phone 面经
下一篇:Google Pittsburgh Onsite 面经

本帖被以下淘专辑推荐:

推荐
ykwwind 2016-5-18 08:22:48 | 只看该作者
全局:
wrapper存第一个element, 再存一个iterator.
每次最小的踢出去,iterator还有的话,改下element, 存回去...
回复

使用道具 举报

推荐
ryanlr 2016-5-18 02:52:35 | 只看该作者
全局:
是不是这样?

  1. package heap;

  2. import java.util.*;

  3. class Wrapper{
  4.         public ArrayList<Integer> list;
  5.         public int index;
  6.        
  7.         public Wrapper(ArrayList<Integer> list, int index){
  8.                 this.list = list;
  9.                 this.index= index;
  10.         }
  11. }

  12. public class MergeKList {
  13.         PriorityQueue<Wrapper> queue;
  14.        
  15.         public MergeKList(ArrayList<ArrayList<Integer>> lists){
  16.                 queue = new PriorityQueue<Wrapper>(new Comparator<Wrapper>(){
  17.                         public int compare(Wrapper w1, Wrapper w2){
  18.                                 return w1.list.get(w1.index) - w2.list.get(w2.index);
  19.                         }
  20.                 });
  21.                
  22.                 for(ArrayList<Integer> list: lists){
  23.                         Wrapper w = new Wrapper(list, 0);
  24.                         queue.offer(w);
  25.                 }
  26.         }
  27.        
  28.         public boolean hasNext(){               
  29.                 if(!queue.isEmpty())
  30.                         return true;
  31.                
  32.                 return false;
  33.         }
  34.        
  35.         public Integer next(){
  36.                
  37.                 Wrapper w = queue.poll();
  38.                
  39.                 ArrayList<Integer> list = w.list;
  40.                 int index = w.index;
  41.                
  42.                 if(index < list.size()-1){
  43.                         w.index = index+1;
  44.                         queue.offer(w);
  45.                 }
  46.                
  47.                 return list.get(index);
  48.         }
  49.        
  50.         public static void main(String[] args) {
  51.                
  52.                 ArrayList<Integer> l1 = new ArrayList<Integer>();
  53.                 ArrayList<Integer> l2 = new ArrayList<Integer>();
  54.                 ArrayList<Integer> l3 = new ArrayList<Integer>();
  55.                
  56.                 l1.add(2);
  57.                 l1.add(4);
  58.                 l1.add(6);
  59.                 l2.add(1);
  60.                 l2.add(3);
  61.                 l3.add(5);
  62.                
  63.                 ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
  64.                 lists.add(l1);
  65.                 lists.add(l2);
  66.                 lists.add(l3);
  67.                
  68.                 MergeKList mk = new MergeKList(lists);
  69.                 while(mk.hasNext()){
  70.                         System.out.println(mk.next());
  71.                 }
  72.         }

  73. }
复制代码
回复

使用道具 举报

推荐
Ruby2333 2017-9-13 21:40:17 | 只看该作者
全局:
  1. import java.util.*;

  2. class QueueNode {
  3.         int value, array, index;
  4.         public QueueNode(int array, int index, int value) {
  5.                 this.array = array;
  6.                 this.index = index;
  7.                 this.value = value;
  8.         }
  9. }
  10. public class SortedKArrays {
  11.         private PriorityQueue<QueueNode> pq;
  12.         private int[][] arrays;
  13.        
  14.         public SortedKArrays(int[][] arrays) {
  15.                 this.arrays = arrays;
  16.                 int k = arrays.length;
  17.                
  18.                 pq = new PriorityQueue<>(k, new Comparator<QueueNode>() {
  19.                         @Override
  20.                         public int compare(QueueNode a, QueueNode b) {
  21.                                 return a.value - b.value;
  22.                         }
  23.                 });
  24.                
  25.                 for (int i = 0; i < k; i++) {
  26.                         if (arrays[i].length > 1) {
  27.                                 pq.offer(new QueueNode(i, 0, arrays[i][0]));
  28.                         }
  29.                 }
  30.         }
  31.         public boolean hasNext() {
  32.                 return !pq.isEmpty();
  33.         }
  34.         public int next() {
  35.                 QueueNode cur = pq.poll();
  36.                 int newIndex = cur.index + 1;
  37.                 if (newIndex < arrays[cur.array].length) {
  38.                         pq.offer(new QueueNode(cur.array, newIndex, arrays[cur.array][newIndex]));
  39.                 }
  40.                 return cur.value;
  41.                
  42.         }
  43.         public static void main(String[] args) {
  44.                 int[][] arrays = {{1, 4, 5}, {2, 7, 10}, {2, 4}, {2, 3}, {2, 8}};
  45.                 SortedKArrays sa = new SortedKArrays(arrays);
  46.                 while (sa.hasNext()) {
  47.                         System.out.println(sa.next());
  48.                 }
  49.         }
  50. }
复制代码
回复

使用道具 举报

全局:
恭喜恭喜,请问下内推后大约多久给的电面啊?
回复

使用道具 举报

🔗
 楼主| kemeng1314 2016-5-17 03:27:37 | 只看该作者
全局:
lookbackinanger 发表于 2016-5-17 03:21
恭喜恭喜,请问下内推后大约多久给的电面啊?

大概一周多吧
回复

使用道具 举报

🔗
adiggo 2016-5-17 03:33:19 | 只看该作者
全局:
楼主 问一下 为什么是维护k size的queue,而不是维护k size的heap呢。 祝楼主 好运
回复

使用道具 举报

🔗
 楼主| kemeng1314 2016-5-17 03:37:47 | 只看该作者
全局:
adiggo 发表于 2016-5-17 03:33
楼主 问一下 为什么是维护k size的queue,而不是维护k size的heap呢。 祝楼主 好运

说错了,不好意思,就是k size的heap
回复

使用道具 举报

🔗
wtcupup 2016-5-17 20:42:14 | 只看该作者
全局:
  1. class KSortedListIterator {

  2.         private ArrayList<Integer> list= new ArrayList<Integer>();
  3.         Iterator iterator = list.iterator();

  4.         public KSortedListIterator(ArrayList<ArrayList<Integer>> lists) {
  5.                 this.list = mergeKSortedLists(lists);
  6.       iterator=list.iterator();
  7.         }

  8.         public boolean hasNext() {
  9.                 return iterator.hasNext();
  10.         }

  11.         public int next() {
  12.                 return iterator.next();
  13.         }

  14. }
复制代码


这样?
回复

使用道具 举报

🔗
 楼主| kemeng1314 2016-5-17 22:56:50 | 只看该作者
全局:

恩,merge之后的list只要是sorted,直接用它的iterator就行了,核心就是merge k lists了,你用的什么思路呢
回复

使用道具 举报

🔗
wtcupup 2016-5-17 23:21:18 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
blackrose 2016-5-17 23:29:10 | 只看该作者
全局:
给的是sorted list of integer,http://blog.csdn.net/github_34333284/article/details/51345092。 如果是Linked List 就是leetcode原题。
回复

使用道具 举报

🔗
 楼主| kemeng1314 2016-5-18 00:15:39 | 只看该作者
全局:
wtcupup 发表于 2016-5-17 23:21
如果面试官要求的是merge k linked list的话 那就可以leetcode上的照搬了 如果是arraylist就比较麻烦了

给的输入是arraylist。确实和linkedlist差别比较大
回复

使用道具 举报

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

本版积分规则

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