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

请教airbnb IP CIDR这道题

全局:

2016(10-12月) 码农类General 硕士 全职@airbnb - 内推 - Onsite  | | Other | 在职跳槽

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

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

x
快要onsite了 IP CIDR这道题在好几个面经都有看到 但是都没有讲具体思路是什么 网上搜了搜只有一个很tricky的解法感觉根本不明白 求哪位大神能指点一二 拜谢
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
style="color:rgb(255, 255, 255)">
然后给你一个起始ip,和数量。用最少的cidr表示这个区间



上一篇:pocket gems面经,必考题
下一篇:发个迟到的微软面经,顺便求组织

本帖被以下淘专辑推荐:

推荐
twfx1123 2016-12-9 04:04:51 | 只看该作者
全局:
我来做个搬运工。。。
  1. import java.util.ArrayList;
  2. import java.util.List;

  3. public class RangeToCidr {
  4.     public static List<String> range2cidrlist( String startIp, String endIp ) {
  5.         // check parameters
  6.         if (startIp == null || startIp.length() < 8 ||
  7.             endIp == null || endIp.length() < 8) return null;
  8.         long start = ipToLong(startIp);
  9.         long end = ipToLong(endIp);
  10.         // check parameters
  11.         if (start > end) return null;

  12.         List<String> result = new ArrayList<String>();
  13.         while (start <= end) {
  14.             // identify the location of first 1's from lower bit to higher bit of start IP
  15.             // e.g. 00000001.00000001.00000001.01101100, return 4 (100)
  16.             long locOfFirstOne = start & (-start);
  17.             int maxMask = 32 - (int) (Math.log(locOfFirstOne) / Math.log(2));

  18.             // calculate how many IP addresses between the start and end
  19.             // e.g. between 1.1.1.111 and 1.1.1.120, there are 10 IP address
  20.             // 3 bits to represent 8 IPs, from 1.1.1.112 to 1.1.1.119 (119 - 112 + 1 = 8)
  21.             double curRange = Math.log(end - start + 1) / Math.log(2);
  22.             int maxDiff = 32 - (int) Math.floor(curRange);

  23.             // why max?
  24.             // if the maxDiff is larger than maxMask
  25.             // which means the numbers of IPs from start to end is smaller than mask range
  26.             // so we can't use as many as bits we want to mask the start IP to avoid exceed the end IP
  27.             // Otherwise, if maxDiff is smaller than maxMask, which means number of IPs is larger than mask range
  28.             // in this case we can use maxMask to mask as many as IPs from start we want.
  29.             maxMask = Math.max(maxDiff, maxMask);

  30.             // Add to results
  31.             String ip = longToIP(start);
  32.             result.add(ip + "/" + maxMask);
  33.             // We have already included 2^(32 - maxMask) numbers of IP into result
  34.             // So the next round start must add that number
  35.             start += Math.pow(2, (32 - maxMask));
  36.         }
  37.         return result;
  38.     }

  39.     private static long ipToLong(String strIP) {
  40.         String[] ipSegs = strIP.split("\\.");
  41.         long res = 0;
  42.         for (int i = 0; i < 4; i++) {
  43.             res += Long.valueOf(ipSegs[i]) << (8 * (3 - i));
  44.         }
  45.         return res;
  46.     }

  47.     private static String longToIP(long longIP) {
  48.         StringBuffer sb = new StringBuffer();
  49.         sb.append(longIP >>> 24).append(".")
  50.           .append((longIP & 0x00FFFFFF) >>> 16).append(".")
  51.           .append(String.valueOf((longIP & 0x0000FFFF) >>> 8)).append(".")
  52.           .append(String.valueOf(longIP & 0x000000FF));

  53.         return sb.toString();
  54.     }
  55. }
复制代码

评分

参与人数 2大米 +4 收起 理由
agraynel + 2 给你点个赞!
大雷若潘 + 2 感谢分享

查看全部评分

回复

使用道具 举报

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

使用道具 举报

🔗
goodluck888 2016-12-8 06:32:13 | 只看该作者
全局:
lz求电面和skype的题目
回复

使用道具 举报

🔗
 楼主| XCQ 2016-12-8 09:05:12 | 只看该作者
全局:
goodluck888 发表于 2016-12-8 06:32
lz求电面和skype的题目

我不是new grad 店面就是2D数组那道
回复

使用道具 举报

🔗
bearcat001 2016-12-8 10:27:12 | 只看该作者
全局:
http://stackoverflow.com/questions/5020317/in-java-given-an-ip-address-range-return-the-minimum-list-of-cidr-blocks-that

我看的这个解法,然后自己研究了一下改了改~
回复

使用道具 举报

🔗
twfx1123 2016-12-9 03:20:22 | 只看该作者
全局:
http://stackoverflow.com/questions/33443914/how-to-convert-ip-address-range-to-cidr-in-java
回复

使用道具 举报

🔗
 楼主| XCQ 2016-12-9 03:53:25 | 只看该作者
全局:
gy21 发表于 2016-12-9 03:20
http://stackoverflow.com/questions/33443914/how-to-convert-ip-address-range-to-cidr-in-java

谢谢 这个好理解多了!
回复

使用道具 举报

🔗
twfx1123 2016-12-9 04:05:12 | 只看该作者
全局:
XCQ 发表于 2016-12-8 11:53
谢谢 这个好理解多了!

那个first1的location是100(4)你怎么理解的呢?
回复

使用道具 举报

🔗
twfx1123 2016-12-9 04:14:09 | 只看该作者
全局:
gy21 发表于 2016-12-8 12:05
那个first1的location是100(4)你怎么理解的呢?

取反+1再ANd就会得到4.。自问自答下。
回复

使用道具 举报

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

本版积分规则

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