注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Google SG phone interview
date: 2019/June/11
Problem 1 get distance between X Y:
The string composed of X Y O, please return the closest distance between any pair of X and Y
e.g.
YOOOOOXOOOXOY
. 1point 3acres
X: 10
Y: 12
→ Output = 12 - 10 = 2
interviewer agree with this solution-baidu 1point3acres
- def get_dist(str):
- min_dist = float('inf')
- is_X = None
- last_index = None
- for index, char in enumerate(str):
- if char == "X":
- if is_X is None:
- is_X = True
- last_index = index. From 1point 3acres bbs
- # if last seen valid character is Y
- if is_X = False:
- dist = index - last_index
- min_dist = min(min_dist, dist). 1point3acres.com
- is_X = True
- last_index = index
- # if last seen valid cahracter is X
- if is_X = True:
- last_index = index
- if char == "Y":
- if is_X is None: ..
- is_X = False. 1point3acres
- last_index = index
- # if last seen valid character is X
- if is_X = True: ..
- dist = index - last_index
- min_dist = min(min_dist, dist)
- is_X = False
- last_index = index. check 1point3acres for more.
- # if last seen valid cahracter is Y
- if is_X = False:
- last_index = index
- return min_dist
复制代码
.1point3acres
Problem 2: get min distance between X Y 2D version
. Waral dи,
description:
2d array with 3 types of characters: X Y and O, get the shortest distance between any pair of X and Y
OOOOXX
XOOOOO
OOYYOO
Output = 3
求大米!十分感謝!
八月要去onsite
. 1point3acres
. 1point 3acres
. 1point 3acres
|