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

跪后捂胸口贡献一个Square电面。附照片

全局:

2016(1-3月) 码农类General 硕士 全职@square - 网上海投 - 技术电面  | | Fail | 应届毕业生
Square 好像很少出重复的题。
历时一个小时。45分钟都是在写代码讨论代码,pair programming 。

如图这个平面蜂窝结构,每一个cell是正六边形,每个cell都有六个相邻的cells。
整个蜂窝的高和宽 (cell数)是可以任意输入的,注意相邻的列都是上下错开的。设计class + function,
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
ramming电面,被虐一下也好,后面才会百战不殆~~

本帖子中包含更多资源

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

x

评分

参与人数 4大米 +103 收起 理由
pengzewen37 + 15 感谢分享!
hulahu + 3 感谢分享!
夏虫不知雪花 + 60
微斯渝 + 25 Good luck!

查看全部评分


上一篇:facebook一轮
下一篇:purestorage 第一轮电面
推荐
johnnywsd 2017-1-25 15:18:23 | 只看该作者
全局:
  1. # -*- coding: utf-8 -*-


  2. class Cells:
  3.     def __init__(self, height, width):
  4.         self.height = height
  5.         self.width = width
  6.         self.max_idx = height * width - 1

  7.     def get_neighbours(self, idx):
  8.         col, row = self._idx_cord(idx)
  9.         up = (col, row - 1)
  10.         right_1 = (col + 1, row)
  11.         left_1 = (col - 1, row)
  12.         if col % 2 == 0:
  13.             right_2 = (col + 1, row - 1)
  14.             left_2 = (col - 1, row - 1)
  15.         else:
  16.             right_2 = (col + 1, row + 1)
  17.             left_2 = (col - 1, row + 1)
  18.         down = (col, row + 1)
  19.         lefts = sorted([left_1, left_2], key=lambda x: -x[1])
  20.         rights = sorted([right_1, right_2], key=lambda x: x[1])
  21.         res = [self._cord_idx(*cord) for cord in [up] + rights + [down] + lefts]
  22.         return res

  23.     def _idx_cord(self, idx):
  24.         if idx > self.max_idx or idx < 0:
  25.             raise Exception
  26.         else:
  27.             col = idx // self.height
  28.             row = idx % self.height
  29.         return (col, row)

  30.     def _cord_idx(self, col, row):
  31.         if not (0 <= col < self.width and 0 <= row < self.height):
  32.             return -1
  33.         res = self.height * col + row
  34.         if res > self.max_idx:
  35.             return -1
  36.         return res

复制代码

  1. import unittest
  2. from solution import Cells


  3. class Tests(unittest.TestCase):

  4.     def test_1(self):
  5.         height = 3
  6.         width = 4
  7.         index = 4
  8.         cells = Cells(height, width)
  9.         res = cells.get_neighbours(index)
  10.         print res

  11.     def test_2(self):
  12.         height = 3
  13.         width = 4
  14.         index = 3
  15.         cells = Cells(height, width)
  16.         res = cells.get_neighbours(index)
  17.         print res

  18.     def test_3(self):
  19.         height = 3
  20.         width = 4
  21.         index = 8
  22.         cells = Cells(height, width)
  23.         res = cells.get_neighbours(index)
  24.         print res

  25. if __name__ == '__main__':
  26.     unittest.main(verbosity=2)
复制代码
回复

使用道具 举报

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

使用道具 举报

全局:
  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;

  4. class CellularNetwork {
  5. public:
  6.     CellularNetwork(int height, int width): hei(height), wid(width) {
  7.         network = vector<vector<int> >(2+hei, vector<int>(2+wid, -1));
  8.         int id = 0;
  9.         for(int c = 1; c <= wid; c++) {
  10.             for(int r = 1; r <= hei; r++) {
  11.                 network[r][c] = id++;
  12.             }
  13.         }
  14.         odd = vector<pair<int,int>>({
  15.             {-1, 0}, {-1, 1}, {0, 1}, {1, 0}, {0, -1}, {-1, -1}
  16.         });
  17.         even = vector<pair<int,int>>({
  18.             {-1, 0}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}
  19.         });
  20.     }
  21.    
  22.     vector<int> getNeighbor(int index) {
  23.         vector<int> result;
  24.         if(index < 0 || index >= hei * wid) return result;
  25.         int c = 1 + (index / hei);
  26.         int r = 1 + (index % hei);
  27.         vector<pair<int,int>>& dir = c%2 == 1 ? odd : even;
  28.         for(auto& p: dir) {
  29.             int newr = r + p.first, newc = c + p.second;
  30.             result.push_back(network[newr][newc]);
  31.         }
  32.         return result;
  33.     }
  34.    
  35. private:
  36.     int hei, wid;
  37.     vector<vector<int> > network;
  38.     vector<pair<int,int> > odd, even;
  39. };

  40. int main(int argc, const char * argv[]) {
  41.     CellularNetwork sol(3, 6);
  42.     vector<vector<int> > results;
  43.     results.push_back(sol.getNeighbor(4));
  44.     results.push_back(sol.getNeighbor(7));
  45.     results.push_back(sol.getNeighbor(15));
  46.     results.push_back(sol.getNeighbor(8));
  47.     for(auto& vec: results) {
  48.         for(int e: vec) cout << e << "  ";
  49.         cout << endl;
  50.     }
  51.    
  52.     return 0;
  53. }
复制代码
回复

使用道具 举报

🔗
T-Beg 2015-11-19 13:38:03 | 只看该作者
全局:
Zan~~~~同意最后那句话
回复

使用道具 举报

🔗
soulstephen 2015-12-11 11:53:50 | 只看该作者
全局:
谢谢楼主的分享~!想请问一下楼主,pair programming具体是一种什么的方式呢?
回复

使用道具 举报

🔗
 楼主| 巫山云似盖 2015-12-12 05:07:11 | 只看该作者
全局:
soulstephen 发表于 2015-12-11 11:53
谢谢楼主的分享~!想请问一下楼主,pair programming具体是一种什么的方式呢?

就是原帖里讲的那样。,你想问的方式是只什么?
回复

使用道具 举报

🔗
soulstephen 2015-12-13 06:11:00 | 只看该作者
全局:
巫山云似盖 发表于 2015-12-12 05:07
就是原帖里讲的那样。,你想问的方式是只什么?

就是我和面试官一起做题?
回复

使用道具 举报

🔗
MCwong 2015-12-16 15:23:32 | 只看该作者
全局:
lz这题能不能直接存成二维矩阵的形式,当column为奇数时对应一种顺序,偶数时对应另一种,然后打印出来。
回复

使用道具 举报

🔗
 楼主| 巫山云似盖 2015-12-16 23:50:30 | 只看该作者
全局:
MCwong 发表于 2015-12-16 15:23
lz这题能不能直接存成二维矩阵的形式,当column为奇数时对应一种顺序,偶数时对应另一种,然后打印出来。

楼主当场是这么做的。矩阵外边加了一圈 “-1” 的cell,这样就不用考虑边界了。
回复

使用道具 举报

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

使用道具 举报

🔗
JamesJi 2016-2-2 04:03:35 | 只看该作者
全局:
刚刚写了一下,这题思路确实不难,然后就是写的时候一定要仔细头脑清晰,不然很容易犯小错误,楼主那个外层加一圈-1的处理感觉很厉害····
回复

使用道具 举报

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

本版积分规则

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