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

Indeed 电面面经

全局:

2017(7-9月) 码农类General 硕士 全职@indeed - 猎头 - 技术电面  | | Pass | 在职跳槽
大概在上周面的Indeed, 已经收到了Onsite,最近空下来一点点,来写个面经攒点人品。

职位应该是Full Stack Engineer, Experienced.

一个男生面的我,然后题有1道,容易。
T1 (CodePair):
input: [1, 2, 3, 5, 8, 10, 11, 12, 13, 14]
output: "1-3, 5, 8, 10-14"
大概就是从左往右扫一次就可以完成了,遇到连续的就记一下右边界,左边界也记着,遇到不连续的来了,就把前面的1-3输出,单个的也输出,注意一下边界条件,包括到了最后面的情况,好像并没有跑,只是手动run test,这算是一个小面试技巧,你创建一个test case,然后一步一步看各个值是多少,人工跑一次看对不对。

Follow up:
假如说输入的数组没有排序,如何优化?
input: [1, 3, 9, 2, 4, 5]

楼主想法是:使用Interval来解决
Interval {
  int start;
  int end;
}

然后过一次数组,丢到一个Set里,再从index = 0 开始,创建一个Interval,往左往右看,是不是可以Grow这个interval,例如:
1 - 1-2 -> 1-3 -> 1-4 -> 1-5
这样Set
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
int[] test1 = new int[] {
   7,
   1,
   5,
   4,
   2,
   ...

补充内容 (2017-9-10 14:57):
package indeed;

import java.util.*;

public class MergeInterval {

public static void main(String[] args) {
  // write your code here
  int[] test1 = new int[] {
   7,
   1,
   5,
   4,
   2,
   ...

本帖子中包含更多资源

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

x

评分

参与人数 2大米 +8 收起 理由
AlisonM + 5 给你点个赞!
xiaojunji + 3 谢谢你的介绍!

查看全部评分


上一篇:imc逻辑oa
下一篇:亚马逊 冷冻期 换简历 拿OA
 楼主| AdamCMU 2017-9-10 15:38:56 | 只看该作者
全局:
这是解法1的改进,修了BUG,测过有效,只能对sorted有效果!
  1. package indeed;

  2. import java.util.*;

  3. public class MergeInterval {

  4.     public static void main(String[] args) {
  5.             // write your code here
  6.         int[] test1 = new int[]{7,1,5,4,2,0};

  7.         // "1-3, 5, 7-9, 11"
  8.         System.out.println(sortedInterval(new int[]{1, 2, 3, 5, 7, 8, 9, 11}));
  9.     }

  10.     static class Interval {
  11.         public int start;
  12.         public int end;

  13.         public int getStart() {
  14.             return start;
  15.         }

  16.         Interval(int start, int end) {
  17.             this.start = start;
  18.             this.end = end;
  19.         }
  20.     }

  21.     public static String intervalToStr(int start, int end) {
  22.         String s = start + "";
  23.         if (start == end) {
  24.             return s + ',';
  25.         } else {
  26.             return s + '-' + end + ',';
  27.         }
  28.     }

  29.     public static String sortedInterval(int[] arr) {
  30.         int len = arr.length;
  31.         if (len == 0) {
  32.             return "";
  33.         }

  34.         int start = arr[0];
  35.         int end = arr[0];

  36.         StringBuilder sb = new StringBuilder();
  37.         for (int i = 1; i < arr.length; i++) {
  38.             int n = arr[i];
  39.             if (n - end == 1) {
  40.                 end = n;
  41.             } else {
  42.                 sb.append(intervalToStr(start, end));
  43.                 start = n;
  44.                 end = n;
  45.             }
  46.         }

  47.         sb.append(intervalToStr(start, end));
  48.         sb.deleteCharAt(sb.length() - 1);
  49.         return sb.toString();
  50.     }
  51. }
复制代码
回复

使用道具 举报

推荐
 楼主| AdamCMU 2017-9-10 14:57:43 | 只看该作者
全局:
  1. package indeed;

  2. import java.util.*;

  3. public class MergeInterval {

  4. public static void main(String[] args) {
  5.   // write your code here
  6.   int[] test1 = new int[] {
  7.    7,
  8.    1,
  9.    5,
  10.    4,
  11.    2,
  12.    0
  13.   };

  14.   // Output: 0-2,4-5,7
  15.   System.out.println(printInterval(test1));
  16. }

  17. static class Interval {
  18.   public int start;
  19.   public int end;

  20.   public int getStart() {
  21.    return start;
  22.   }

  23.   Interval(int start, int end) {
  24.    this.start = start;
  25.    this.end = end;
  26.   }
  27. }

  28. public static String printInterval(int[] arr) {
  29.   HashSet < Integer > set = new HashSet < > ();
  30.   HashSet < Integer > setUsed = new HashSet < > ();

  31.   if (arr.length == 0) {
  32.    return "";
  33.   }
  34.   StringBuilder sb = new StringBuilder();

  35.   ArrayList < Interval > intervalList = new ArrayList < > ();

  36.   for (int i: arr) {
  37.    set.add(i);
  38.   }

  39.   Iterator < Integer > it = set.iterator();
  40.   while (it.hasNext()) {
  41.    Integer num = it.next();
  42.    if (setUsed.contains(num)) {
  43.     continue;
  44.    }

  45.    int start = num;
  46.    int end = num;
  47.    while (set.contains(end + 1)) {
  48.     end++;
  49.     setUsed.add(end);
  50.    }

  51.    while (set.contains(start - 1)) {
  52.     start--;
  53.     set.remove(start);
  54.     setUsed.add(end);
  55.    }

  56.    intervalList.add(new Interval(start, end));
  57.   }

  58.   intervalList.sort(Comparator.comparing(Interval::getStart));

  59.   for (Interval interval: intervalList) {
  60.    int start = interval.start;
  61.    int end = interval.end;
  62.    if (start == end) {
  63.     sb.append(start + ",");
  64.    } else {
  65.     sb.append(start + "-" + end + ",");
  66.    }
  67.   }

  68.   sb.deleteCharAt(sb.length() - 1);
  69.   return sb.toString();
  70. }
  71. }
复制代码

补充内容 (2017-9-10 14:58):
这个是改进后的代码,测过有效。

补充内容 (2017-9-11 07:12):
有一个小BUG, 67行应删除。
回复

使用道具 举报

🔗
xiaojunji 2017-9-10 08:16:27 | 只看该作者
全局:
这答案测试结果
input : [7, 1, 5, 4, 2, 0]
output: 7,1,5,4,2
好像不对啊
回复

使用道具 举报

🔗
xiaojunji 2017-9-10 08:42:02 | 只看该作者
全局:
unsorted list那题set存的是什么? 是Interval吗?
怎么才能实现1 - 1-2 -> 1-3 -> 1-4 -> 1-5?
回复

使用道具 举报

🔗
 楼主| AdamCMU 2017-9-10 13:09:08 | 只看该作者
全局:
xiaojunji 发表于 2017-9-10 08:16
这答案测试结果
input : [7, 1, 5, 4, 2, 0]
output: 7,1,5,4,2

有可能是有小BUG的 我也没有调。
回复

使用道具 举报

🔗
 楼主| AdamCMU 2017-9-10 13:09:53 | 只看该作者
全局:
xiaojunji 发表于 2017-9-10 08:16
这答案测试结果
input : [7, 1, 5, 4, 2, 0]
output: 7,1,5,4,2

另外,这个答案只是第一步,只能是有序的,对于无序的,这个答案不能处理。
无序的答案,我讲了一下,并没有写在这里。
回复

使用道具 举报

🔗
 楼主| AdamCMU 2017-9-10 13:10:16 | 只看该作者
全局:
xiaojunji 发表于 2017-9-10 08:42
unsorted list那题set存的是什么? 是Interval吗?
怎么才能实现1 - 1-2 -> 1-3 -> 1-4 -> 1-5?

Interval.  答案不是太方便贴,我等一下写一下给你看?
回复

使用道具 举报

🔗
YonghuiFan 2017-9-25 12:29:32 | 只看该作者
全局:
真好,讲的很详细
回复

使用道具 举报

🔗
linlin1990 2017-9-25 12:40:23 | 只看该作者
全局:
弱弱的说下 Palo Alto Networks 不止问算法题 还问很多别的。。。。我面挂过。。。祝好运
回复

使用道具 举报

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

本版积分规则

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