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

Square新鲜面经 (3.3.2020)

全局:

2020(1-3月) MachineLearningEng 硕士 全职@ - 网上海投 - 技术电面  | | Other | 在职跳槽

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

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

x
今天上午刚刚结束的电面,趁着还记得赶紧发个帖,求大米~现在还不到188分还有好多帖子不能看。。。

电面题目:

让写一个程序,实现这样一个功能:

你已经有一个单词,比如apple

然后用户依次输入一个char, a - z之间的

如果正确,那console显示:You’ve entered correctly, the
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
,所以+3分。

要求在猜完所有字母以后,返回得到的总分。

这里需要实现一个Trie,然后用这个来记录分数。我自己没有写完时间就到了,Trie还是不太熟练,不过感觉面试官还算满意,不知道能不能过。

评分

参与人数 5大米 +15 收起 理由
wxhxx + 1 很有用的信息!
xwang699 + 1 给你点个赞!
cee + 2 给你点个赞!
匿名用户-AB4VV + 9
adlxk + 2 很有用的信息!

查看全部评分


上一篇:疫情下非死不可的VC
下一篇:数学工作 EDG OA
🔗
cee 2020-3-11 09:44:31 | 只看该作者
全局:
follow up 也是一个字母一个字母的猜吗?猜完一个再猜另一个?
回复

使用道具 举报

🔗
 楼主| xiaomaogy 2020-3-11 09:54:19 | 只看该作者
全局:
cee 发表于 2020-3-11 09:44
follow up 也是一个字母一个字母的猜吗?猜完一个再猜另一个?

嗯嗯一样的,只是算分的方式不一样。
回复

使用道具 举报

🔗
xwang699 2020-7-4 13:05:10 | 只看该作者
全局:
请问楼主后面有去onsite嘛.. 先给楼主加米了
回复

使用道具 举报

🔗
jamesthu 2020-12-20 03:50:13 | 只看该作者
全局:
请问楼主后来去了吗?后边的面试都被问到什么问题了?
回复

使用道具 举报

🔗
ZlatanQuant 2021-2-18 07:45:51 | 只看该作者
全局:
本帖最后由 ZlatanQuant 于 2021-2-18 07:48 编辑

谢谢lz的题目!!
我把lz的题写了一下,大家可以交流!!
第一个子问题:猜一个单词

  1. class Solution:
  2.     def __init__(self, groundtruth_word):
  3.         self.groundtruth_word = groundtruth_word
  4.         self.input_word = ""
  5.     def take_input(self):
  6.         char = input('Guess a char: ')
  7.         prevL = len(self.input_word)
  8.         if char == self.groundtruth_word[prevL]:
  9.             self.input_word += char
  10.             print("You've entered correctly, the current is  "  + self.input_word)
  11.         else:
  12.             print("You've entered wrong, the current is  "  + self.input_word)
  13.     def checkLenth(self):
  14.         if len(self.input_word) == len(self.groundtruth_word):
  15.             return True
  16.         else:
  17.             return False

  18. S = Solution('apple')
  19. while(not S.checkLenth()):
  20.     S.take_input()
  21. print('Successful ! Program exit.')
  22. sys.exit()
复制代码







评分

参与人数 1大米 +1 收起 理由
ahenry015 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
ZlatanQuant 2021-2-18 07:46:37 | 只看该作者
全局:
本帖最后由 ZlatanQuant 于 2021-2-18 07:48 编辑

第二个子问题,猜一个list 返回分数
  1. import sys

  2. #Define Trie Node
  3. class Node:
  4.     def __init__(self):
  5.         self.children = {} #key:char #val: tree Node
  6.         self.val = 0
  7.         self.isEnd = False
  8.         
  9. #Define Trie
  10. class Trie:
  11.     def __init__(self):
  12.         """
  13.         Initialize your data structure here.
  14.         """
  15.         self.dummy = Node()

  16.     def insert(self, word: str) -> None:
  17.         """
  18.         Inserts a word into the trie.
  19.         """
  20.         head = self.dummy
  21.         for idx, char in enumerate(word):
  22.             if char not in head.children:
  23.                 head.children[char] = Node()
  24.             head = head.children[char]
  25.             head.val += 1
  26.             if idx == len(word)-1:
  27.                 head.isEnd = True
  28.         
  29.     def search(self, word: str) -> bool:
  30.         """
  31.         Returns if the word is in the trie.
  32.         """
  33.         head = self.dummy
  34.         for idx, char in enumerate(word):
  35.             if char not in head.children:
  36.                 return False
  37.             head = head.children[char]
  38.             if idx == len(word) - 1:
  39.                 return head.isEnd
  40.         
  41.     def startsWith(self, prefix: str) -> bool:
  42.         """
  43.         Returns if there is any word in the trie that starts with the given prefix.
  44.         """
  45.         head = self.dummy
  46.         for idx, char in enumerate(prefix):
  47.             if char not in head.children:
  48.                 return False
  49.             head = head.children[char]
  50.             if idx == len(prefix) - 1:
  51.                 return True
  52.    
  53.     def obtainScore(self, prefix: str) -> bool:
  54.         head = self.dummy
  55.         score = 0
  56.         for idx, char in enumerate(prefix):
  57.             if char not in head.children:
  58.                 return score
  59.             head = head.children[char]
  60.             score += head.val
  61.             if idx == len(prefix) - 1:
  62.                 return score
  63.         
  64. class Solution:
  65.     def __init__(self, groundtruth_word, trie):
  66.         self.trie = trie
  67.         self.input_word = ""
  68.         
  69.     def take_input(self):
  70.         char = input('Guess a char: ')
  71.         if char == 'EXIT!!':
  72.             print('The author wants to exit')
  73.             return True
  74.    
  75.         if self.trie.startsWith(self.input_word + char):
  76.             self.input_word += char
  77.             print("There indeed exists word with prefix "  + self.input_word + " in the list")
  78.         else:
  79.             print("There is not word with prefix  "  + self.input_word + char + " in the list")
  80.         print('The current score is ' + str(trie.obtainScore(self.input_word)))
  81.         if self.trie.search(self.input_word):
  82.             print("The word " + self.input_word + " is in the list! Exit")
  83.             return True
  84.         print(' ')
  85.         
  86.         return False
  87.         
  88. List = ['apple', 'air', 'ad', 'bc']
  89. trie = Trie()
  90. for word in List:
  91.     trie.insert(word)

  92. S = Solution('apple', trie)
  93. while(True):
  94.     Exit = S.take_input()
  95.     if Exit:
  96.         sys.exit()
复制代码

回复

使用道具 举报

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

本版积分规则

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