Google Singapore 電面

1460
1
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

X: 10
Y: 12
→ Output = 12 - 10 = 2

interviewer agree with this solution


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
# if last seen valid character is Y
if is_X = False:
dist = index - last_index
min_dist = min(min_dist, dist)
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
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
# if last seen valid cahracter is Y
if is_X = False:
last_index = index
return min_dist


Problem 2: get min distance between X Y 2D version

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
  • 5
1条回复