TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Most common phone interview question at Google

22 点作者 jquave大约 5 年前

8 条评论

asdginioubnou大约 5 年前
Both solutions are O(1). The alphabet is finite. Let&#x27;s say there are M different characters in the alphabet and N different characters in the string. If there is a duplicate, it is guaranteed to occur within the first M + 1 characters of the string by the pigeonhole principle. As N grows without bound, the number of characters that needs to be checked reaches a limit.<p>(I guess, most precisely, we should define K to be min(M + 1, N). Then the first solution is O(K^2) and the second solution is O(K))<p>EDIT: I assumed the first solution would compare each character to the characters visited so far, i.e.<p><pre><code> for i in [0..N] for j in [0..i] if arr[i] == arr[j] return arr[i] </code></pre> Maybe it&#x27;s<p><pre><code> for i in [0..N] for j in [0..N] if arr[i] == arr[j] &amp;&amp; i != j return arr[i] </code></pre> That would make it O(KN). Or O(N) if we treat the size of the alphabet as fixed.
评论 #23056200 未加载
评论 #23055838 未加载
chrisseaton大约 5 年前
&gt; This means is a magnitude faster than the previous one!<p>Is this right? It&#x27;s a quadratic not an order of magnitude difference isn&#x27;t it? And can you be an order of magnitude faster in an O notation expression anyway? Isn&#x27;t an order of magnitude a coefficient? Don&#x27;t we drop coefficients in O notation?
评论 #23054088 未加载
评论 #23054086 未加载
评论 #23054079 未加载
ayberk大约 5 年前
I used to use this question before it became too common. Any reasonably-well-prepared candidate should be able come up with the O(N) solution and the follow-up is always good for a discussion: &quot;what if the string is huge but the alphabet is small? Eg, a DNA sequence.&quot;<p>There&#x27;s a single pass solution that&#x27;s left as an exercise for the reader.
评论 #23054056 未加载
评论 #23056437 未加载
评论 #23054105 未加载
kinkrtyavimoodh大约 5 年前
I love that this question is very basic, tests only entry-level data structures, and can at least be talked and reasoned about even if you don&#x27;t know the exact API of a hashmap, and YET I am sure people will find a reason to complain about this claiming they never actually need to find a recurring character in their actual job role.
jrootabega大约 5 年前
Congrats, it&#x27;s now off the list of phone interview questions at Google.
thiht大约 5 年前
&gt; Most peoples immediate thought would be a double for loop<p>Really? I&#x27;ve a hard time believing this
jimbob45大约 5 年前
Usually, part of the question is to ask about legal characters. If they tell you it&#x27;s letters only, then you can get away with a pre-allocated array (and give a concrete big O answer).
TheOtherHobbes大约 5 年前
It&#x27;s not an order of magnitude faster. The details depend on the hash table implementation and the language and the processor specifics and the statistical distribution of string sizes and whether you can make assumptions about the number of symbols being used and whether a tight double loop for a small symbol set and string size would fit in the cache and whether it would stay there long enough to make a useful difference.<p>Naive Big O is just as likely to be wrong as naive anything else.<p>I hope this isn&#x27;t a genuine phone screen question, because if someone punted this at me as a pass&#x2F;fail I&#x27;d fail them for being confused about how real computers and real languages work behind those nice clean-looking &quot;if thing is in other_thing...&quot; statements.
评论 #23054373 未加载