整数除法
我曾面临以下面试问题。
考虑这个函数声明:
void 测验(int i) { 如果(i>1) { 测验(i / 2); 测验(i / 2); } 写输出(“*”); }
函数调用
quiz(5)
打印了多少个星号?
我的回答是:
具有整数除法结果类型的语言(Javascript、PHP 等)是 浮动 - 七个星号。函数测验被调用:
- 当 i=5 – 一次,打印星号。
- 当 i=2.5 – 两次时,打印星号。
- i=1.25 – 四次,打印星号。
- i=0.625 – 八次,不打印星号
除法结果类型名称为的语言(C/C++、C#、Java 等) 整数 - 三个星号。函数测验被调用:
- 当 i=5 – 一次,打印星号。
- 当 i=2 – 两次时,打印星号。
- 当 i=1 – 四次时,不打印星号。
问题语法类似于C/C++、Java,所以答案是三个
面试是闭卷考试
- 在面试期间我无法运行此代码并检查它。面试官告诉我,我的回答并不绝对正确(或者至少,他们没想到会是这样)。然而,我已经在家里运行了这段代码(使用 PHP、Javascript 和 C#),结果正如我所描述的那样。
那么,我是否遗漏了一些警告,或者我的回答比他们预期的更详细?
I have faced the following interview question.
Consider this function declaration:
void quiz(int i) { if (i > 1) { quiz(i / 2); quiz(i / 2); } writeOutput("*"); }
How many asterisks are printed by the function call
quiz(5)
?
My answer was:
Languages (Javascript, PHP, etc.) with integer division result type is
float - seven asterisks. Function quiz get called:
- With i=5 – once, asterisk printed.
- With i=2.5 – twice, asterisks printed.
- With i=1.25 – four times, asterisks printed.
- With i=0.625 – eight times, no asterisks printed
Languages (C/C++, C#, Java, etc.) which division result type name is
integer - three asterisks. Function quiz get called:
- With i=5 – once, asterisk printed.
- With i=2 – twice, asterisks printed.
- With i=1 – four times, asterisks not printed.
Question syntax is like C/C++, Java, so the answer would be three
The interview was a closed book exam
- during the interview I was unable to run this code and check it. The interviewer told me that my answer is not absolutely correct (or at least, they didn't expect it to be like this). Hovewer, I've ran this code (with PHP, Javascript and C#) at home and the result was as I described.
So, are there some caveats I'm missing or my answer was just more detailed than they were expecting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果将代码更改为:
您将看到
quiz(5)
的结果是:因此,您得到了每个 i 的正确调用次数,您只是没有注意到 writeOutput是在 if 之外,而不是在 if 之内。
If you change the code to this:
You'll see that the result for
quiz(5)
is:So, you got the correct number of calls per i, you just didn't notice that the writeOutput is outside the if, not inside it.
当 i <= 1 时, writeOutput 也会被调用。
writeOutput is going to be called when i <= 1, too.
鉴于该函数采用
int
作为参数,它(在通常情况下)永远不会将2.5
作为参数。当然,除非有人说#define double int
,但在这种情况下,没有什么是可靠的了。所以你的答案可能应该只是你答案的第二部分。
Given that the function takes an
int
as a parameter, it will (under usual circumstances) never get2.5
as an argument. Unless of course one says#define double int
, but in that case nothing is reliable anymore.So your answer should probably just have been the second part of your answer.
我在 Java 中得到 7:
输出 =
有两件事使得这个简单的函数很难手动跟踪。
第一:递归。递归函数使用调用堆栈,很难在纸上跟踪 i 的状态......尤其是在面试中;-)
第二:就像其他人所说的那样, print(*) 位于 if 之外......也许有一点收获!
I get 7 in Java:
output =
Two things make this simple function hard to trace manually.
First : Recursion. Recursive function use the call stack and it can be hard to keep trace of the state of i on paper.... especially in an interview ;-)
Second : like other said, the print(*) was outside the if... maybe a little catch!
我认为除了答案之外,如果你能提供复杂性分析,面试官会很高兴。
复杂度为
2**ceil(lg(n))-1
(其中**
是幂运算符):您可以通过编写一棵树来可视化它以及我们得到的结果是一个严格且完整的二叉树(下面是 n=5 的情况,为简洁起见省略了几个节点)
另外,从问题来看,我不认为面试官期望对其他语言进行分析,例如 python 3.x,其中默认行为是浮点除法,*会打印更多次。当预设浮点数时,调用次数为:
2 ** (ceil(lg(n))+1) -1
,(调用树会比整数除法,让我们练习一下:))您可以运行以下Python代码:
在Python 2.x上:(整数除法)
在Python 3.x上:(浮点)
HTH。
I think along with the answer, the interviewer would have been happy if you could provide the complexity analysis.
The complexity is
2**ceil(lg(n))-1
(where**
is power operator) :You can visualize this by writing a tree and what we get is a strict and complete binary tree (below is for n=5 and few nodes omitted for brevity)
Also from the question, I don't think the interviewer was expecting an analysis for other languages such as python 3.x, where default behavior is floating point division and the * will be printed even more times. when floating point numbers are preset, the number of calls is :
2 ** (ceil(lg(n))+1) -1
, (the call tree will be even more larger than the one for integer division, lets as excercise :) )You can run this python code:
on python 2.x: (integer division)
on python 3.x: (floating point)
HTH.