wrjc1hod 发表于 2024-9-4 07:01:19

Google一个很棒的面试问题


    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/3ef7d84508bf44c4b9fccf02a53fb236~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1725974794&amp;x-signature=lvIXE%2BWpWfvyhmM2R7IHb3gG4Z8%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在2010年至2019年<span style="color: black;">时期</span>,我在谷歌采访了数十名软件工程师候选人。我几乎总是问<span style="color: black;">一样</span>的面试问题。<span style="color: black;">另外</span>,这个问题恰好在谷歌的禁名单上,<span style="color: black;">由于</span>它在Glassdoor和其他面试网站上公开<span style="color: black;">供给</span>,但我继续<span style="color: black;">运用</span>它,<span style="color: black;">由于</span>我从候选人那里得到了良好的信号。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Daniel Tunkelang对这个问题<span style="color: black;">亦</span>有类似的积极经历,但在2011年就退役了:“<span style="color: black;">离休</span>是一个伟大的面试问题”。这本身<span style="color: black;">便是</span>一本很好的读物,但<span style="color: black;">因为</span>我对这个问题的经验更广泛,我决定把它写下来。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">为何</span>这是一个很好的面试问题</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">问题<span style="color: black;">起始</span>很简单。</span><span style="color: black;">非常</span><span style="color: black;">简单,以至于<span style="color: black;">有些</span>候选人认为<span style="color: black;">她们</span><span style="color: black;">无</span>正确理解它。从那里,它探索了候选人对<span style="color: black;">各样</span>基本算法和数据结构概念的理解。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">另外</span>,正如我所介绍的那样,这个问题有足够的范围来筛选出好的候选人,即使候选人以前见过这个问题,这<span style="color: black;">便是</span><span style="color: black;">为何</span>我成功地<span style="color: black;">运用</span>了它,尽管它被谷歌禁止了。我两次问招聘委员会我<span style="color: black;">是不是</span>应该停止<span style="color: black;">运用</span>这个,<span style="color: black;">她们</span>对此<span style="color: black;">无</span>问题。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">面试</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是一个45分钟的软件工程职位技术面试,从初级到中级候选人。<span style="color: black;">亦</span>许令人惊讶的是,初级候选人有时在这个问题上排名更高,<span style="color: black;">由于</span><span style="color: black;">她们</span>的数据结构和算法知识更<span style="color: black;">鲜嫩</span>。但这种模式并不<span style="color: black;">广泛</span>。优秀的高级候选人对面试完全<span style="color: black;">无</span>问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">有些</span>轶事<span style="color: black;">始终</span>留在我身边:</span></p><span style="color: black;">我曾经有一个有15年工作经验的候选人给了我<span style="color: black;">非常多</span>态度,<span style="color: black;">能够</span>问<span style="color: black;">她们</span>这么简单的问题,<span style="color: black;">同期</span>又在<span style="color: black;">奋斗</span><span style="color: black;">处理</span>。</span><span style="color: black;">我让候选人用所有的铃铛和口哨<span style="color: black;">处理</span><span style="color: black;">全部</span>问题最快的是在20分钟内,由滑铁卢大学的一名</span><span style="color: black;">新生完成</span><span style="color: black;">,他是参加高中比赛的那种。</span><span style="color: black;">我看到的最令人<span style="color: black;">懊丧</span>的失败是多伦多大学的一名博士毕业生,他<span style="color: black;">没法</span>在45分钟内为问题的<span style="color: black;">第1</span>部分生成工作代码。</span><span style="color: black;">一位候选人预先告诉我,<span style="color: black;">她们</span><span style="color: black;">已然</span>看到了问题。我咧嘴一笑,说:“太好了,<span style="color: black;">因此</span>你应该<span style="color: black;">晓得</span><span style="color: black;">怎样</span><span style="color: black;">处理</span>它!”<span style="color: black;">她们</span>仍然在与之斗争。</span><span style="color: black;">另一位候选人清楚地记住<span style="color: black;">认识</span>决<span style="color: black;">方法</span>代码,并立即编写了它。我<span style="color: black;">需求</span><span style="color: black;">她们</span>退后一步,完成我<span style="color: black;">需求</span><span style="color: black;">她们</span>完成的<span style="color: black;">过程</span>。<span style="color: black;">她们</span><span style="color: black;">亦</span>挣扎了。<span style="color: black;">她们</span>以前<span style="color: black;">亦</span>不诚实地看过它。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我<span style="color: black;">首要</span>告诉候选人,我<span style="color: black;">爱好</span>听<span style="color: black;">她们</span>的思维过程,在我<span style="color: black;">需求</span><span style="color: black;">她们</span><span style="color: black;">这般</span>做之前,我不想让<span style="color: black;">她们</span>写代码。<span style="color: black;">而后</span>我告诉<span style="color: black;">她们</span>,这个问题有两个部分。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">第1部分</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">问题</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">“写一个给定字符串的函数,确定它<span style="color: black;">是不是</span>是两个字典单词的串联。”</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当被问到时,我解释说,字典<span style="color: black;">能够</span><span style="color: black;">做为</span><span style="color: black;">查找</span>成员字符串的功能。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">处理</span><span style="color: black;">方法</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">任何优秀的候选人都应该能够快速找到问题的蛮力/琐碎<span style="color: black;">处理</span><span style="color: black;">方法</span>,即:在所有可能的位置拆分输入字符串,并在字典中<span style="color: black;">查找</span>双方。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">不外</span>,你会惊讶于有多少候选人难以找到这个<span style="color: black;">处理</span><span style="color: black;">方法</span>。有些人想出了一个</span><span style="color: black;">贪婪的</span><span style="color: black;">算法,找到字典中的<span style="color: black;">第1</span>个前缀,<span style="color: black;">而后</span>返回字符串的其余部分<span style="color: black;">是不是</span>在字典中。有些人可能会认为蛮力<span style="color: black;">处理</span><span style="color: black;">方法</span>太简单/</span><span style="color: black;">效率低下</span><span style="color: black;">,并立即寻求更快的<span style="color: black;">处理</span><span style="color: black;">方法</span>。我试着引导<span style="color: black;">她们</span>先提出</span><span style="color: black;">任何</span><span style="color: black;"><span style="color: black;">处理</span><span style="color: black;">方法</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这一点上,我<span style="color: black;">需求</span><span style="color: black;">她们</span>为它编写代码。以下是Python中的单行<span style="color: black;">处理</span><span style="color: black;">方法</span>代码,<span style="color: black;">运用</span>生成器:</span></p><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">splitString2</span>(<span style="color: black;">s</span>):</span>
    返回任何(inDict(s[<span style="color: black;">:i</span>])和inDict(s[<span style="color: black;">i:</span>])<span style="color: black;">针对</span>i<span style="color: black;">in</span> range(<span style="color: black;">1</span>, len(s)))<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大<span style="color: black;">都数</span>候选人都写一个for循环,这<span style="color: black;">一样</span>是<span style="color: black;">能够</span>接受的。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">运行时间</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>我<span style="color: black;">需求</span>候选人分析这个<span style="color: black;">处理</span><span style="color: black;">方法</span>的运行时间。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大<span style="color: black;">都数</span>候选人<span style="color: black;">意见</span>O(n)<span style="color: black;">做为</span>答案。(让<span style="color: black;">咱们</span>假设制作子字符串s[:i]和s是常量时间。)我会问<span style="color: black;">她们</span><span style="color: black;">怎样</span>实现字典函数,使这个<span style="color: black;">处理</span><span style="color: black;">方法</span>是O(n)。<span style="color: black;">她们</span>大多<span style="color: black;">意见</span>,<span style="color: black;">倘若</span><span style="color: black;">做为</span>散列函数实现,将是O(1)<span style="color: black;">查询</span>。<span style="color: black;">而后</span>我推动<span style="color: black;">她们</span><span style="color: black;">这般</span>做,直到<span style="color: black;">她们</span><span style="color: black;">认识</span>到字符串的字典<span style="color: black;">查询</span>函数的任何实现充其量都是o(n),<span style="color: black;">因此呢</span><span style="color: black;">处理</span><span style="color: black;">方法</span>的总运行时间是O(n²)。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">更快的<span style="color: black;">处理</span><span style="color: black;">方法</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>候选人<span style="color: black;">火速</span>就能走这么远(即不到20分钟),我会问候选人<span style="color: black;">她们</span><span style="color: black;">是不是</span>能想出一种<span style="color: black;">办法</span>来加快这个<span style="color: black;">处理</span><span style="color: black;">方法</span>。大<span style="color: black;">都数</span>候选人会有点挣扎。我告诉<span style="color: black;">她们</span>,<span style="color: black;">倘若</span>字典<span style="color: black;">查询</span>按原样<span style="color: black;">运用</span>函数,<span style="color: black;">那样</span>这个算法是渐近最优的(我不<span style="color: black;">需求</span><span style="color: black;">她们</span>证明,但你能证明这一点吗?)。<span style="color: black;">因此呢</span>,任何加速都需要字典的<span style="color: black;">区别</span><span style="color: black;">暗示</span>方式。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">非常好的候选人<span style="color: black;">认识</span>到,前缀的字典<span style="color: black;">查询</span>正在做<span style="color: black;">非常多</span>重复的工作,<span style="color: black;">经过</span>以树的形式<span style="color: black;">暗示</span>字典来避免,更确切地说,这是一个</span><span style="color: black;">尝试</span><span style="color: black;">。我让几个候选人<span style="color: black;">就地</span>重新尝试,而其他人则依稀记得<span style="color: black;">她们</span>。无论<span style="color: black;">怎样</span>,将字典<span style="color: black;">暗示</span>为一个尝试将使字典O(n)总数中的所有前缀都<span style="color: black;">检测</span>。<span style="color: black;">咱们</span>仍然有<span style="color: black;">检测</span>所有后缀的问题。<span style="color: black;">经过</span>对<span style="color: black;">叫作</span>性,<span style="color: black;">咱们</span>还<span style="color: black;">能够</span>将字典中<span style="color: black;">包括</span>单词的另一个尝试</span><span style="color: black;">反转</span><span style="color: black;">。届时,<span style="color: black;">检测</span>字典成员资格的所有后缀<span style="color: black;">亦</span>是O(n)。<span style="color: black;">咱们</span>只需要一个o(n)<span style="color: black;">体积</span>的数组来<span style="color: black;">保留</span>这个。<span style="color: black;">而后</span>,<span style="color: black;">咱们</span>只需要将前缀和后缀结果相交,并找到重叠。总算法是O(n),<span style="color: black;">因此呢</span>是最优的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我不<span style="color: black;">需求</span>候选人为此编写代码。<span style="color: black;">倘若</span><span style="color: black;">她们</span>被卡太久,我会跳到第2部分。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">第2部分</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">问题</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">“编写一个给定字符串的函数,确定它<span style="color: black;">是不是</span>是多个字典单词的串联。”</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">一样</span>,<span style="color: black;">咱们</span>从将字典<span style="color: black;">暗示</span>为<span style="color: black;">查找</span>函数<span style="color: black;">起始</span>。当被问及字典中的字符串本身<span style="color: black;">是不是</span>应该<span style="color: black;">经过</span>测试时,答案是肯定的。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">处理</span><span style="color: black;">方法</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我再次强调,我<span style="color: black;">爱好</span>先看到</span><span style="color: black;">任何</span><span style="color: black;"><span style="color: black;">处理</span><span style="color: black;">方法</span>。优秀的候选人认识到,<span style="color: black;">她们</span><span style="color: black;">能够</span>简单地将<span style="color: black;">处理</span><span style="color: black;">方法</span>从<span style="color: black;">第1</span>部分修改为递归<span style="color: black;">处理</span><span style="color: black;">方法</span>。例如:</span></p><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">splitString</span>(<span style="color: black;">s</span>):</span>
    <span style="color: black;">倘若</span>inDict(s):
    返回True
    返回任何(inDict(s[<span style="color: black;">:i</span>])和splitString(s[<span style="color: black;">i:</span>]) <span style="color: black;">for</span> i <span style="color: black;">in</span>range(<span style="color: black;">1</span>, len(s)))<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然而,许多候选人在为递归<span style="color: black;">处理</span><span style="color: black;">方法</span>而苦苦挣扎。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">运行时间</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">分析递归<span style="color: black;">处理</span><span style="color: black;">方法</span>的运行时间对<span style="color: black;">有些</span>人<span style="color: black;">来讲</span>可能是一个挑战。<span style="color: black;">一样</span>,假设字符串拆分操作是O(1),归根结底是认识到,在最坏的<span style="color: black;">状况</span>下,字符串被拆分为所有可能的拆分,并且有o(2^n),与字典<span style="color: black;">查询</span>成本相结合,<span style="color: black;">引起</span>O(n.2^n)运行时。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">更快的<span style="color: black;">处理</span><span style="color: black;">方法</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>我<span style="color: black;">需求</span>一个更快的<span style="color: black;">处理</span><span style="color: black;">方法</span>,这次我期望候选人找到一个。优秀的候选人观察到,<span style="color: black;">针对</span>相同的输入,splitString函数被递归调用,<span style="color: black;">因此呢</span>到达了记忆或动态编程<span style="color: black;">做为</span><span style="color: black;">解救</span><span style="color: black;">办法</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我不<span style="color: black;">需求</span><span style="color: black;">她们</span>为此编写代码,只需<span style="color: black;">转</span>到运行时分析。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">运行时间</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">这儿</span>正确分析的关键是认识到,<span style="color: black;">咱们</span><span style="color: black;">仅在</span>带有原始字符串后缀的splitString函数上递归,该后缀有n。<span style="color: black;">针对</span><span style="color: black;">每一个</span>后缀,<span style="color: black;">咱们</span>执行O(n)操作,<span style="color: black;">思虑</span>到字典<span style="color: black;">查询</span>成本,该操作变<span style="color: black;">成为了</span>O(n²)。总运行时为O(n³)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我不<span style="color: black;">需求</span>候选人展示这个,但假设字典是<span style="color: black;">做为</span><span style="color: black;">查询</span>功能<span style="color: black;">供给</span>的,<span style="color: black;">瞧瞧</span>你<span style="color: black;">是不是</span>能证明这是最优的。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">更快的<span style="color: black;">处理</span><span style="color: black;">方法</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span>大<span style="color: black;">都数</span>候选人<span style="color: black;">来讲</span>,<span style="color: black;">咱们</span><span style="color: black;">日前</span><span style="color: black;">已然</span><span style="color: black;">无</span>时间了,但<span style="color: black;">倘若</span>有时间,我问候选人<span style="color: black;">她们</span><span style="color: black;">是不是</span><span style="color: black;">能够</span>想出一个更快的<span style="color: black;">处理</span><span style="color: black;">方法</span>。<span style="color: black;">倘若</span><span style="color: black;">她们</span><span style="color: black;">已然</span>到达了第1部分的三方解,<span style="color: black;">她们</span>将在<span style="color: black;">这儿</span><span style="color: black;">意见</span>三<span style="color: black;">办法</span>,这将<span style="color: black;">形成</span>一个组合的O(n²)运行时。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">瞧瞧</span>你<span style="color: black;">是不是</span>能证明这是最优的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">更新:</span></strong><span style="color: black;">假设字典中的单词数量比n大得多。</span></p>




情迷布拉格 发表于 2024-9-10 01:59:13

百度seo优化论坛 http://www.fok120.com/
页: [1]
查看完整版本: Google一个很棒的面试问题