由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - G面经
相关主题
谁有trapping rain water的code?贡献几个 c 电面问题
java多线程问题请教 (转载)两个月没做题了~~
leetcode那道longest valid parenthese的题很诡异G题,F题,leetcode题
不明白leetcode OJ wordladder 2 总是 Time Limit ExceededReverse Words in a String
M onsite面经遍历二叉树除了recursion还有啥好办法?
请问如果要求in place的话,递归是不是就不能用了?G电面
面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。Amazon coding question
Linux context switch 高通 面试题。??题目: iterative binary tree post order traversal
相关话题的讨论汇总
话题: int话题: prev话题: sum话题: bar话题: return
进入JobHunting版参与讨论
1 (共1页)
b*****n
发帖数: 760
1
1. You have a class that supports to input sample records and to compute the
average of the samples. The class has two members: total and count. How
would you make the class thread-safe? If 99% of the time average() is called
, how to optimize for that?
2. Talk about your recent interesting project/bug.
3. You have 100 files, each containing 10G sorted integers. How to merge all
integers into one sorted file?
4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
g**********y
发帖数: 14569
2
赞!Bless.
h**********d
发帖数: 4313
3
祝福
a**********2
发帖数: 340
4
bless,这个是什么 level的题吗?
8. 遍历 n*second max 复杂度该是多少啊?
12. bitset还是list啊?
谁来讲讲
g**********y
发帖数: 14569
5
13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
1/k > p >= 1/(k+1) 时,该发k份邀请。
b******v
发帖数: 1493
6
第13题我的想法是这样的,不知道对不对
P(exactly one accept) = N*p*(1-p)^{N-1}
logP = logN + log(p/(1-p)) + Nlog(1-p)
d(logP)/dN = 0 ==> N = 1/log(1/(1-p))

【在 g**********y 的大作中提到】
: 13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
: 1/k > p >= 1/(k+1) 时,该发k份邀请。

g**********y
发帖数: 14569
7
前面的推导对,后面我不sure:你怎么证明最大值一定出现在微分为0的地方而且解唯一?我的微积分是忘干净了。
我是直接推算的,f(N) = N*p*(1-p)^(N-1)
f(N+1) - f(N) = (1-p)^(N-1) * [(N+1)*p*(1-p) - N*p]
sgn[f(N+1) - f(N)] = sgn[(N+1)*p*(1-p) - N*p]
= sgn[p(1-(N+1)*p] = sgn[1-(N+1)*p]
递归可以证明:
p = 1~1/2, N=1最好
p = 1/2~1/3, N=2最好
p = 1/3~1/4, N=3最好
。。。

【在 b******v 的大作中提到】
: 第13题我的想法是这样的,不知道对不对
: P(exactly one accept) = N*p*(1-p)^{N-1}
: logP = logN + log(p/(1-p)) + Nlog(1-p)
: d(logP)/dN = 0 ==> N = 1/log(1/(1-p))

m****t
发帖数: 555
8
两种方法应该都对。
最大值出现在导数为0的地方,很容易证明。
当N < 1/log(1/(1-p)) 时,P的导数大于0,说明P单调递增;
当N > 1/log(1/(1-p)) 时,P的导数小于0,说明P单调递减;
所以N = 1/log(1/(1-p))时,P取最大值。

唯一?我的微积分是忘干净了。

【在 g**********y 的大作中提到】
: 前面的推导对,后面我不sure:你怎么证明最大值一定出现在微分为0的地方而且解唯一?我的微积分是忘干净了。
: 我是直接推算的,f(N) = N*p*(1-p)^(N-1)
: f(N+1) - f(N) = (1-p)^(N-1) * [(N+1)*p*(1-p) - N*p]
: sgn[f(N+1) - f(N)] = sgn[(N+1)*p*(1-p) - N*p]
: = sgn[p(1-(N+1)*p] = sgn[1-(N+1)*p]
: 递归可以证明:
: p = 1~1/2, N=1最好
: p = 1/2~1/3, N=2最好
: p = 1/3~1/4, N=3最好
: 。。。

g**********y
发帖数: 14569
9
第8题,O(n)
public int hold(int[] a) {
Stack stack = new Stack();
int level = 0;
int total = 0;
for (int i=0; i while (!stack.isEmpty()) {
Bar prev = stack.peek();
if (prev.height <= a[i]) {
total += (i - prev.x - 1) * (prev.height - level);
level = prev.height;
stack.pop();
}
else {
break;
}
}

if (stack.isEmpty()) level = 0;
stack.push(new Bar(i, a[i]));
}
return total;
}

class Bar {
int x;
int height;
public Bar(int x, int height) {
this.x = x;
this.height = height;
}
}
b*****n
发帖数: 760
10
13 题数学题
不要用连续函数思考, 要用离散
用log那种做法不是非常严谨
加入N是最大可能性, 那么 f(N+1)
相关主题
请问如果要求in place的话,递归是不是就不能用了?贡献几个 c 电面问题
面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。两个月没做题了~~
Linux context switch 高通 面试题。??G题,F题,leetcode题
进入JobHunting版参与讨论
b*****n
发帖数: 760
11
不清楚啊, 应该就是一般的developer吧。。。。
不是很senior的职位吧
LD面试回来直说, google还是不错的, 问得问题还是很有水平的。。。。。

【在 a**********2 的大作中提到】
: bless,这个是什么 level的题吗?
: 8. 遍历 n*second max 复杂度该是多少啊?
: 12. bitset还是list啊?
: 谁来讲讲

b*****n
发帖数: 760
12
LD面试回来后, 给偶做, 提示了不能用连续函数来思考, 要用离散, 偶大概前前后
后做了30分钟搞定, 现场LD也没有完全作对。。。。。

【在 g**********y 的大作中提到】
: 13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
: 1/k > p >= 1/(k+1) 时,该发k份邀请。

y***m
发帖数: 7027
13
为啥不是 1/p 的取整?
e.g. p=0.3
0.3*3+0.1=1 应该发4份?

【在 b*****n 的大作中提到】
: 13 题数学题
: 不要用连续函数思考, 要用离散
: 用log那种做法不是非常严谨
: 加入N是最大可能性, 那么 f(N+1)
b*****n
发帖数: 760
14
是取整 啊。。。。
1/P down to 1/P+1 取整

【在 y***m 的大作中提到】
: 为啥不是 1/p 的取整?
: e.g. p=0.3
: 0.3*3+0.1=1 应该发4份?

o*******y
发帖数: 810
15
赞很详细的面经!
bless!
d*******d
发帖数: 2050
16
c++ version
int water(int * a, int n){
stack > mystack;
int water = 0;
for(int i = 0; i while(!mystack.empty() && mystack.top().second <= a[i]){
int bottom = mystack.top().second;
mystack.pop();
if( mystack.empty())
break;
int top = mystack.top().second < a[i]? mystack.top().second: a[i];
int length = i - mystack.top().first - 1;
water = water + (top-bottom)* length;
}
mystack.push(make_pair(i, a[i]));
}
return water;
}

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

l*****e
发帖数: 1374
17
记得好像有一道题大致意思是:
给出一个矩阵,每个元素表示那个方格的高度,然后求下雨后能装多少水。
例如:
2,2,2
2,1,2
2,2,2
返回1。
有什么好方法吗?

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

g**********y
发帖数: 14569
18
考古一下吧,那是个经典题,用flood fill做的。

【在 l*****e 的大作中提到】
: 记得好像有一道题大致意思是:
: 给出一个矩阵,每个元素表示那个方格的高度,然后求下雨后能装多少水。
: 例如:
: 2,2,2
: 2,1,2
: 2,2,2
: 返回1。
: 有什么好方法吗?

X********i
发帖数: 28
19
Bless.
c******r
发帖数: 225
20
程序我看懂了,可是题我没有读懂
能用白话文给我讲一遍,这题什么意思吗?

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

相关主题
Reverse Words in a StringAmazon coding question
遍历二叉树除了recursion还有啥好办法?题目: iterative binary tree post order traversal
G电面challenge: 找bug
进入JobHunting版参与讨论
h***m
发帖数: 1869
21
bless.

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

P**********c
发帖数: 3417
22
Lots of multi-threading.

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

n***y
发帖数: 2730
23
13: Negative Binomial Distribution! Google it.
a***u
发帖数: 383
24
不确定自己是否理解题意。对这个程序有点疑问,如果给的数组中第一个数是最大的,
那么程序的返回值total是0?

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

P**********c
发帖数: 3417
25
ms有问题,每次for结尾都push进stack,而且prev.x是在pop之前用, total+的似乎永远
是0。

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

b*******y
发帖数: 2048
26
big bless
P**********c
发帖数: 3417
27
这个应该是对的。

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

P**********c
发帖数: 3417
28
1,5,7,9,10,12没人讨论吗?感觉是复习multithread的好题目。

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

z*****1
发帖数: 130
29
Bless !!

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

g*****i
发帖数: 2162
30
同求多线程题目的思路

【在 P**********c 的大作中提到】
: 1,5,7,9,10,12没人讨论吗?感觉是复习multithread的好题目。
:
: the
: called
: all
: 890

相关主题
这个histgram的最大面积问题java多线程问题请教 (转载)
新鲜的T电面题leetcode那道longest valid parenthese的题很诡异
谁有trapping rain water的code?不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded
进入JobHunting版参与讨论
g*****i
发帖数: 2162
31
是不是应该加个判断保证top>bottom?

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

g**********y
发帖数: 14569
32
我写的那个有错,dinohound那个是对的,而且简洁。

【在 a***u 的大作中提到】
: 不确定自己是否理解题意。对这个程序有点疑问,如果给的数组中第一个数是最大的,
: 那么程序的返回值total是0?

v********y
发帖数: 2
33

It should be one invitation. the probability is: p
if you sent two invitation, the probability will be: p(1-p) < p

【在 g**********y 的大作中提到】
: 13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
: 1/k > p >= 1/(k+1) 时,该发k份邀请。

L*****s
发帖数: 24744
34
难度比我那时候容易多了..
t****x
发帖数: 20
35
I don't think the code works. Suppose input is {9, 8, 7, 6} (descending
order), then the code only pushes the numbers into stack then return. The
water is 0.

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

d*******d
发帖数: 2050
36
这个就该是0阿.谁都沿着斜坡流走了,呆不住.

【在 t****x 的大作中提到】
: I don't think the code works. Suppose input is {9, 8, 7, 6} (descending
: order), then the code only pushes the numbers into stack then return. The
: water is 0.

P**********c
发帖数: 3417
37
top==bottom的时候total+0, 所以不判断也可以。不会有top
【在 g*****i 的大作中提到】
: 是不是应该加个判断保证top>bottom?
t****x
发帖数: 20
38
My bad. I misunderstood the question.

【在 d*******d 的大作中提到】
: 这个就该是0阿.谁都沿着斜坡流走了,呆不住.
s*******e
发帖数: 4188
39
讨论一下#1:
我的思路(Java)一直是用synchronized,so the idea is:
public synchronized void add(int n) {
sum += n;
count++;
}
public synchronized void average() {
return sum/count;
}
曾经有个interviewer问我不用synchronized keyword (no synchronized method or
synchronized block)怎么办?所以想听听别的思路。
g***s
发帖数: 3811
40
the question assumed 99% cpu are used for average. so adding a field '
average' will improve performance much. After doing that, we dont need
synchronized for average(), but dont forget to add volatile key for the
field.

【在 s*******e 的大作中提到】
: 讨论一下#1:
: 我的思路(Java)一直是用synchronized,so the idea is:
: public synchronized void add(int n) {
: sum += n;
: count++;
: }
: public synchronized void average() {
: return sum/count;
: }
: 曾经有个interviewer问我不用synchronized keyword (no synchronized method or

相关主题
不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。
M onsite面经Linux context switch 高通 面试题。??
请问如果要求in place的话,递归是不是就不能用了?贡献几个 c 电面问题
进入JobHunting版参与讨论
s*******e
发帖数: 4188
41
#4 我的思路是recursion:
int reverseN = reverse(n, 0);
public static int reverse(int n, int sum) {
if (n == 0) return sum;
sum = sum*10 + n%10;
return reverse(n/10, sum);
}
还有别的什么想法?
g***s
发帖数: 3811
42
try to avoid tail-recursion.

【在 s*******e 的大作中提到】
: #4 我的思路是recursion:
: int reverseN = reverse(n, 0);
: public static int reverse(int n, int sum) {
: if (n == 0) return sum;
: sum = sum*10 + n%10;
: return reverse(n/10, sum);
: }
: 还有别的什么想法?

r*******n
发帖数: 344
43
How about using readWriteLock, readLock in average(), writeLock in add()...

【在 s*******e 的大作中提到】
: 讨论一下#1:
: 我的思路(Java)一直是用synchronized,so the idea is:
: public synchronized void add(int n) {
: sum += n;
: count++;
: }
: public synchronized void average() {
: return sum/count;
: }
: 曾经有个interviewer问我不用synchronized keyword (no synchronized method or

g**********y
发帖数: 14569
44
public int reverse(int n) {
return n>=0? reversePositive(n) : -reversePositive(-n);
}

private int reversePositive(int n) {
return Integer.parseInt(new StringBuilder("" + n).reverse().toString());
}

【在 s*******e 的大作中提到】
: #4 我的思路是recursion:
: int reverseN = reverse(n, 0);
: public static int reverse(int n, int sum) {
: if (n == 0) return sum;
: sum = sum*10 + n%10;
: return reverse(n/10, sum);
: }
: 还有别的什么想法?

a**********2
发帖数: 340
45
int reverseInteger(int val)
{
int result =0 ;
while(val)
{
result *=10;
result += val%10;
val /= 10;
}
return result;
}

【在 s*******e 的大作中提到】
: #4 我的思路是recursion:
: int reverseN = reverse(n, 0);
: public static int reverse(int n, int sum) {
: if (n == 0) return sum;
: sum = sum*10 + n%10;
: return reverse(n/10, sum);
: }
: 还有别的什么想法?

n***y
发帖数: 2730
46
13: last night I did not have time to go to detail. Here are some quick
thoughts on this problem for after work entertaining.
1-----
This is very close to negative binormial distribution (see http://en.wikipedia.org/wiki/Negative_binomial_distribution). According to wiki, let r be number of failure (use wiki term, which means accept invitation here), then the mode for nbd should be at floor(pr/(1-p)). (p is probability of sucess, same as in wiki)
Now noticing
p(NB(r, p) = k) = p (r-1 failures in k + r-1 trials) * p (failure). After
some index switch, we can see that p(r failure with N = k+r trials) reach
maximum at N = floor(r/1-p).
Now, we are assuming accept is failure, using the p as same meaning in prob
13 and r=1, we can see that the probabilty reaches the maximum at floor(1/P)
, here P is the probability mentioned in 13.
2----
berktov mentioned that the N should be a number neighboring -1/ln(1-p). This
answer is also correct. It is not hard to see:
0 < (1/p) + 1/ln(1-p) < 1.
3----
LZ mentioned solving f(N+1) But I am not impressed....
s*******e
发帖数: 4188
47
can you elaborate with some (pseudo-)code?

【在 r*******n 的大作中提到】
: How about using readWriteLock, readLock in average(), writeLock in add()...
s*******e
发帖数: 4188
48
you are right. I didn't try to address the optimization part.
is there a general approach in place of synchronized method or block?

【在 g***s 的大作中提到】
: the question assumed 99% cpu are used for average. so adding a field '
: average' will improve performance much. After doing that, we dont need
: synchronized for average(), but dont forget to add volatile key for the
: field.

g***s
发帖数: 3811
49
因为只有<1%的时间在add上面,所以用synchronized/lock/cas差别不大,用
synchronized反而简单易懂。

【在 s*******e 的大作中提到】
: you are right. I didn't try to address the optimization part.
: is there a general approach in place of synchronized method or block?

s*******e
发帖数: 4188
50
this is what i'm looking for.

【在 a**********2 的大作中提到】
: int reverseInteger(int val)
: {
: int result =0 ;
: while(val)
: {
: result *=10;
: result += val%10;
: val /= 10;
: }
: return result;

相关主题
两个月没做题了~~遍历二叉树除了recursion还有啥好办法?
G题,F题,leetcode题G电面
Reverse Words in a StringAmazon coding question
进入JobHunting版参与讨论
C***U
发帖数: 2406
51
要学多久才能回答上这些问题啊
刚准备开始学cs

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

b********1
发帖数: 49
52
第八题怎么是5?
从3到10 (0 based).
我用的是O(N3), 慢但应该正确呀...
import java.util.*;
public class Water {
int test(int arr[]) {
int N = arr.length;
int max = 0;
for (int i = 0; i < N - 2; i++) {
for (int j = i + 2; j < N; j++) {
int min = Math.min(arr[i], arr[j]);
int sum =0;
for (int k = i + 1; k < j; k++) {
int v = min - arr[k];
if (v > 0) {
sum += v;
}
}
if (sum > max) {
max = sum;
System.out.println(i + " -> " + j);
}
}
}
return max;
}

public static void main(String args[]) {
System.out.println(new Water().test(new int[]{0,1,0,2,1,0,1,3,2,1,2,
1}));
}
}
h***i
发帖数: 1970
53
tail-recursion是好事吧,编译器可以优化。

【在 g***s 的大作中提到】
: try to avoid tail-recursion.
g***s
发帖数: 3811
54
编译器可以优化是指它不好,在编译的时候来避免掉。不是所有编译器都能做这个优化
的。

【在 h***i 的大作中提到】
: tail-recursion是好事吧,编译器可以优化。
P**********c
发帖数: 3417
55
第9题是放一个timer再放一个static的counter吗?
第12题的意思是说这个class是个integer, 可以无穷大,还是说这个class有无穷多个
integer?
add (int) 是说加进一个integer到这个set, 还是这个integer和BigInt相加?

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

b*****n
发帖数: 760
56
LD看大家讨论这么激烈, 让我在第一楼加上了自战解说。。。。
让讨论来的更加激烈吧。。。。。。
P**********c
发帖数: 3417
57
第12题vector能存多少数是有上限的吧,毕竟它的size()返回值其实是
unsigned int. 存成vector的好处是什么呢?

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

c*******t
发帖数: 39
58
I feel that it is just an easy application of expectation.

prob
P)

【在 n***y 的大作中提到】
: 13: last night I did not have time to go to detail. Here are some quick
: thoughts on this problem for after work entertaining.
: 1-----
: This is very close to negative binormial distribution (see http://en.wikipedia.org/wiki/Negative_binomial_distribution). According to wiki, let r be number of failure (use wiki term, which means accept invitation here), then the mode for nbd should be at floor(pr/(1-p)). (p is probability of sucess, same as in wiki)
: Now noticing
: p(NB(r, p) = k) = p (r-1 failures in k + r-1 trials) * p (failure). After
: some index switch, we can see that p(r failure with N = k+r trials) reach
: maximum at N = floor(r/1-p).
: Now, we are assuming accept is failure, using the p as same meaning in prob
: 13 and r=1, we can see that the probabilty reaches the maximum at floor(1/P)

s*****y
发帖数: 897
59
为什么不用linklist的?

【在 P**********c 的大作中提到】
: 第12题vector能存多少数是有上限的吧,毕竟它的size()返回值其实是
: unsigned int. 存成vector的好处是什么呢?
:
: the
: called
: all
: 890

P**********c
发帖数: 3417
60
linked list idea不错。怎么实现add(int)呢。要先把int转换成linked list, 然后
recursive的进位吗?

【在 s*****y 的大作中提到】
: 为什么不用linklist的?
相关主题
题目: iterative binary tree post order traversal新鲜的T电面题
challenge: 找bug谁有trapping rain water的code?
这个histgram的最大面积问题java多线程问题请教 (转载)
进入JobHunting版参与讨论
P**********c
发帖数: 3417
61
第9题用queue存time stamp, multithreading的情况下是每次update queue的时候都
lock吗?

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

k*******n
发帖数: 16
62
第四题不考虑负数?
P**********c
发帖数: 3417
63
考虑吧,例子里就有-890 -98,这个记录一个flag就可以了,不难实现。

【在 k*******n 的大作中提到】
: 第四题不考虑负数?
h**********8
发帖数: 267
64
mark
k*******n
发帖数: 16
65
是的,只是这么多天没人提,我也只是提醒下大家表忘了。这题没难度,是不是面试的
题目不仅答对还要最优???我一直是那种只要能编译通过从来不考虑complexity的人
...以后得加强这方面训练了。

【在 P**********c 的大作中提到】
: 考虑吧,例子里就有-890 -98,这个记录一个flag就可以了,不难实现。
k****n
发帖数: 1334
66
这个算法是神马意思,谁给解释一下?

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

g*****i
发帖数: 2162
67
我觉得应该要lock吧

【在 P**********c 的大作中提到】
: 第9题用queue存time stamp, multithreading的情况下是每次update queue的时候都
: lock吗?
:
: the
: called
: all
: 890

m**q
发帖数: 189
68
Good work.
I just coded a version using O(1) extra space, the down side
is it's not as clean (sigh) and I wasn't able to code it bug free
in my first try. The below code works for the specified input.
int water2(int a[], int n)
{
int i=0, prev=INT_MIN, sum=0;
int prev_high, new_high, prev_index;

while (i=prev) {
prev=a[i]; i++;
}
if (i==n)
return 0;

prev_high=prev, prev_index=i-1;

while (i int tmp=0;
while(i int diff = prev_high-a[i];
tmp += diff;
prev=a[i]; i++;
}
if (i==n)
return sum;
while (i=prev) {
if (a[i] < prev_high) {
int diff = prev_high - a[i];
tmp += diff;
}
prev=a[i]; i++;
}
new_high=prev;
if (new_high < prev_high) {
tmp -= (prev_high-new_high) * (i-1-prev_index);
}

prev_high=new_high; prev_index=i-1;
sum += tmp;

}

return sum;
}

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

m**q
发帖数: 189
69
The code looks pretty neat. Draw the picture on a paper
it should be more clear.

【在 k****n 的大作中提到】
: 这个算法是神马意思,谁给解释一下?
t**r
发帖数: 3428
70
谢谢LZ分享
慢慢学习
相关主题
java多线程问题请教 (转载)M onsite面经
leetcode那道longest valid parenthese的题很诡异请问如果要求in place的话,递归是不是就不能用了?
不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。
进入JobHunting版参与讨论
i**********e
发帖数: 1145
71
反例:
[5,4,1,2]
Your code returns -1.

【在 m**q 的大作中提到】
: Good work.
: I just coded a version using O(1) extra space, the down side
: is it's not as clean (sigh) and I wasn't able to code it bug free
: in my first try. The below code works for the specified input.
: int water2(int a[], int n)
: {
: int i=0, prev=INT_MIN, sum=0;
: int prev_high, new_high, prev_index;
:
: while (i=prev) {

i**********e
发帖数: 1145
72
除了以上的反例之外,你的代码还不能处理比较复杂的情况:
例如:
【5 5 1 7 1 1 5 2 7 6】
你忽略了上面 左7 与 右7 在上边能装上 8 的状况。
你的代码返回的是 15,而正确的答案应该是 23.
利用 stack 的思路是 O(n) time 和 O(n) space,O(n) space 的状况就是一个严格递
减的数组。
不知道有没有 O(n) time 和 O(1) space 的解,直觉告诉我没有。
用 stack 的代码如下:
typedef pair PI;
int waterShed(int A[], int n) {
int total = 0;
stack s;
for (int i = 0; i < n; i++) {
int prevHeight = A[i];
while (!s.empty()) {
total += (min(s.top().first, A[i]) - prevHeight) * (i - s.top().second - 1);
prevHeight = s.top().first;
if (s.top().first > A[i]) break;
s.pop();
}
s.push(PI(A[i], i));
}
return total;
}
i**********e
发帖数: 1145
73
Try this input:
[4, 2, 3]

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

g**********y
发帖数: 14569
74
我的那个代码有错。LZ给的答案就是O(n)复杂度,O(1)空间的。
public int hold(int[] a) {
int h = 0;
int N = a.length;
for (int i=1; i a[h]) h = i;
int total = 0;
int left = a[0];
for (int i=1; i if (a[i] > left) {
left = a[i];
}
else {
total += left - a[i];
}
}
int right = a[N-1];
for (int i=N-2; i>h; i--) {
if (a[i] > right) {
right = a[i];
}
else {
total += right - a[i];
}
}
return total;
}

【在 i**********e 的大作中提到】
: Try this input:
: [4, 2, 3]

s*****y
发帖数: 897
75
好牛的方法,简单得来不容易code错。

【在 g**********y 的大作中提到】
: 我的那个代码有错。LZ给的答案就是O(n)复杂度,O(1)空间的。
: public int hold(int[] a) {
: int h = 0;
: int N = a.length;
: for (int i=1; i a[h]) h = i;
: int total = 0;
: int left = a[0];
: for (int i=1; i: if (a[i] > left) {
: left = a[i];

m**q
发帖数: 189
76
嗯,我的代码是错的,把问题考虑的太简单了。

【在 i**********e 的大作中提到】
: 除了以上的反例之外,你的代码还不能处理比较复杂的情况:
: 例如:
: 【5 5 1 7 1 1 5 2 7 6】
: 你忽略了上面 左7 与 右7 在上边能装上 8 的状况。
: 你的代码返回的是 15,而正确的答案应该是 23.
: 利用 stack 的思路是 O(n) time 和 O(n) space,O(n) space 的状况就是一个严格递
: 减的数组。
: 不知道有没有 O(n) time 和 O(1) space 的解,直觉告诉我没有。
: 用 stack 的代码如下:
: typedef pair PI;

d***n
发帖数: 65
77
第八题, 贴个时间O(n),空间O(1)的,试了前面提到的所有test case。貌似全都正确
。整个数组只需扫描一遍。
int floodFill(int B[], int n)
{
int w = 0;
int i = 0, j = n;
int maxL = 0, maxR = 0;
while(i {
while(B[i]<=max(maxL, maxR) && i {
if(B[i]>maxL)
maxL = B[i];
else
w +=maxL - B[i];
i++;
}
if(i==j) break;
maxL = B[i];//found new high
while(B[--j] {
if(B[j]>maxR)
maxR = B[j];
else
w += maxR - B[j];
}
if(i==j) break;
maxR = B[j];//may be higher than maxL
i++;
}
return w;
}
n******n
发帖数: 49
78
在想这题的时候 觉得它的思路跟largest rectangle under histogram的stack解法很
像。只是现在是栈顶元素比当前元素小的时候,弹出栈顶元素;之前histogram那题是
栈顶元素比当前元素大的时候,弹出。
原先那道histogram的题参考这个
http://wansishuang.appspot.com/?p=3002

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

s*******f
发帖数: 1114
79
//4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -
890
//--> -98.
int ReverseDigits (int n){
int ret = 0;
while (n){
ret = ret * 10 + n % 10;
n /= 10;
}
return ret;
}
s*******f
发帖数: 1114
80
楼主以前做trading systme吗? 这么多多线程。
相关主题
Linux context switch 高通 面试题。??G题,F题,leetcode题
贡献几个 c 电面问题Reverse Words in a String
两个月没做题了~~遍历二叉树除了recursion还有啥好办法?
进入JobHunting版参与讨论
s*******f
发帖数: 1114
81
//8. Given a non-negative integer array representing an elevation map
whereas
//the width of each bar is 1, compute how much water it can contain after
//raining. For example, given [0,1,0,2,1,0,1,3,2,1,2,1], returns 6. What is
//the complexity of your solution?
// 1 time scan; O(1) space
int HoldWater(int a[], int len){
int i = 0;
int j = len - 1;
int sum;
int ibig = INT_MIN;
int jbig = INT_MIN;
while (i <= j){
if (ibig < jbig){
if (ibig < a[i]){
ibig = a[i];
}else{
sum += ibig - a[i];
}
++i;
}else{
if (jbig < a[j]){
jbig = a[j];
}else{
sum += jbig - a[j];
}
--j;
}
}
return sum;
}
m*****k
发帖数: 731
82
can you try [3, 0, 1] ?

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

m*****k
发帖数: 731
83
can you try [ 5, 0, 3, 4]?

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

e***s
发帖数: 799
84
Bless, MARK下慢慢看
l****s
发帖数: 165
85
Google jobs:
http://jobguiding.com/it-jobs/it-companies/google.html

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

e***s
发帖数: 799
86
不用FLAG,负数求出来的余数也是负数吧

【在 P**********c 的大作中提到】
: 考虑吧,例子里就有-890 -98,这个记录一个flag就可以了,不难实现。
c**j
发帖数: 103
87
13. 我从2,3 推到n,maximize: np(1-p)^{n-1}
求导。。。。
对不对?
d*******u
发帖数: 186
88
能不能用白话文解释一下,多谢。

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

d*******u
发帖数: 186
89
白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡序列),上坡时:如果
有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
知道过完为止。

【在 d*******u 的大作中提到】
: 能不能用白话文解释一下,多谢。
d*******u
发帖数: 186
90
//白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡
序列),上坡时:如果
//有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
//知道过完为止。
int water(int * a, int n){
stack > mystack;
int water = 0;
for(int i = 0; i while(!mystack.empty() && mystack.top().second <= a[i]){ //上坡
int bottom = mystack.top().second; //底
mystack.pop();
if( mystack.empty()) //堆栈中只有一个元素,不能形成底。
break;
int top = mystack.top().second < a[i]? mystack.top().second: a[i];
//找到顶(次大数)
int length = i - mystack.top().first - 1; //长度
water = water + (top-bottom)* length; //填充
} //当前上坡处理完。
mystack.push(make_pair(i, a[i])); //下坡,进堆栈。
//上坡无底的话,是什么都不做的,因为上次进堆栈的元素直接出堆栈了,只是查
了一下底(至少两元素在堆栈)。
}
return water;


序列),上坡时:如果

【在 d*******u 的大作中提到】
: 白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡序列),上坡时:如果
: 有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
: 知道过完为止。

相关主题
G电面challenge: 找bug
Amazon coding question这个histgram的最大面积问题
题目: iterative binary tree post order traversal新鲜的T电面题
进入JobHunting版参与讨论
d*******u
发帖数: 186
91
有没有人说一说如果是二维数组的, flood fill怎么做呀?

【在 d*******u 的大作中提到】
: //白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡
: 序列),上坡时:如果
: //有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
: //知道过完为止。
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){ //上坡
: int bottom = mystack.top().second; //底

c***8
发帖数: 188
92
many thanks

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890

b*****n
发帖数: 760
93
1. You have a class that supports to input sample records and to compute the
average of the samples. The class has two members: total and count. How
would you make the class thread-safe? If 99% of the time average() is called
, how to optimize for that?
2. Talk about your recent interesting project/bug.
3. You have 100 files, each containing 10G sorted integers. How to merge all
integers into one sorted file?
4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
--> -98.
5. Discuss design challenges of a distributed web crawler running on
commercial PCs. How to utilize network bandwidth of those PCs efficiently?
6. How to test if an API is thread-safe or not?
7. How to convert a single threaded application to multiple threaded?
8. Given a non-negative integer array representing an elevation map whereas
the width of each bar is 1, compute how much water it can contain after
raining. For example, given [0,1,0,2,1,0,1,3,2,1,2,1], returns 6. What is
the complexity of your solution?
9. You have an API and you want to wrap it up such that it cannot be called
more than N times a second. How to do this in a multiple-threaded
environment?
10. Design a system to provide suggestions to the search box. How would you
update such a system with new search data when it is running?
11. Introduce your research.
12. Design a BigInt class to represent unlimited size of integers. Implement
a member function add(int).
13. You have two movie tickets and want to invite a friend to watch movie
together. You can invite several friends simultaneously. Each one of them
independently has probability p to accept your invitation. How many friends
should you invite to maximize the probability that exactly one friend
accepts your invitation?
谨以此感谢版上提供经验的各位前辈并顺求祝福!
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
自战解说:
1. For the 1st part of the question, I answered that read-write mutex is a possible solution and to design a good solution we'd like to know the usage pattern. Thus the 2nd part of the question and I answered that the member to input sample records can update a new member average, so that average() just needs to read and no lock is needed.
2. ...
3. I proposed two solutions: n-way merge and counting sort. Then I was asked to decide which one is better and I said counting sort, because it is easier to paralellize and uses less memory. Then I was asked to implement it but I ran out of time for the details. As a hindsight, I should make sure of these in implementation: what's the range of possible input? Any known distribution? Will my program be 32-bit or 64-bit? Is the count of some number possible to overflow? Can INT_MIN be counted correctly?
4. I came up with a recursion solution at first and changed it to an iterative version per requested. Hindsight: not all input integer can be reversed correctly and the valid input is not a simple continuous range.
5. I have little knowledge about web crawler so I asked a lot of questions. I suggested to utilize the geography information of target websites to dispatch the work load and to pool connection/job at each PC. This did not seem to be the interviewer's expectation as he told me hints about how to resolve DNS to IP addresses efficiently, how to deal with slow web sites, etc.
6. I told him thread-safety should be part of the API's interface and I cannot tell without source code. I just googled and it seems that there's no solution to this problem?
7. I told him that I would first decide if multi-thread is appropriate for the application. And then discussed the strategy of converting thread-unsafe APIs to services. Lots of detailed discussion on this problem about design choices.
8. My idea is to find the global maximum element in the array first, and then look from beginning to the global maximum, identify a "lake" after each current maximum and accumulate the water volume, and then look from the end to the global maximum in symmetry. Time complexity is O(n). Space complexity is O(1).
9. I started with a naive solution using sleep() and figured out the real requirement at that time. Then I talked about using a semaphore and to set up timer/callback to update semaphore. That's not the best solution either and the interviewer hint me to use a queue to record time stamps of the N most recent calls. I should be able to find this solution without hint.
10. A very complicated problem. We discussed browser-side/server-side work, trie (prefix tree) at letter level or word level, client side pre-fetching, how to store/build server-side data structure using lots of commercial PCs, possible data compression schemes, an estimate about how many PCs will I use according to my design, and more stuff I do not remember. For the question about how to update the system with new search trend data, I suggested to process the data offline using another set of PCs and switch live/offline set periodically. I felt quite satisfied about myself on this question.
11. ...
12. I first said that I can use a string to store each digit of the BigInt and realized that it is a dumb design when he asked me to implement it. Then I changed to use vector and a sign bit to store the BigInt. There were continuous questions on my implementation of BigInt::add(int). I am not very satisfied about my solution to this problem as I did not support adding a negative int in the time given.
13. I told him immediately that the answer is floor(1/p). But then he asked me why? I said that I remember this is binomial distribution and wrote down the formula f=n*p*(1-p)^(n-1). The interviewer still asked why and showed me that df/dn=0 does not result in n=1/p. At that time I was puzzled but still insisted that my memory should be correct. I was not able to get the correct deduction in time. So I emailed HR the solution that night and hope he would forward my solution to the interviewer per my request.
心得:本以为会被问到比较难的算法题,比如DP, string matching, suffix tree, graph algo, operations on binary tree啥的。或者OO概念,design pattern之类的设计问题。不过实战都是非常质朴而实际的问题,并没有单纯为了增加难度而出的题。白板编程一定要好好练习,听到问题后第一件事是澄清问题、确认几个测试用例、设法限制输入(assert)、明确边界条件,同时最快时间考虑出一个基本解决方案,然后再考虑优化。即使运气好碰到做过的题,也务必按照顺序给出分析思考解决优化的全过程。白板编程要注意程序篇幅的控制,根据内容多少选择恰当的字体大小;如有可能,程序中可以使用辅助函数并多半不用实现之;遇到程序分支先实现错误处理、特殊情况等短分支,再实现主体算法分支,以便控制篇幅及避免遗忘。
总体看来,G家面官准备的技术问题都颇靠谱。完全没有behavior question这一点也甚合我意。唯一不爽的是好几个面官都时间耗尽,没有机会问他们问题;倒也有几个面官比较体贴,技术问题前让我先问问题,不过他们似乎对我的问题都无准备,回答并不能使我满意。
g**********y
发帖数: 14569
94
赞!Bless.
h**********d
发帖数: 4313
95
祝福
a**********2
发帖数: 340
96
bless,这个是什么 level的题吗?
8. 遍历 n*second max 复杂度该是多少啊?
12. bitset还是list啊?
谁来讲讲
g**********y
发帖数: 14569
97
13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
1/k > p >= 1/(k+1) 时,该发k份邀请。
b******v
发帖数: 1493
98
第13题我的想法是这样的,不知道对不对
P(exactly one accept) = N*p*(1-p)^{N-1}
logP = logN + log(p/(1-p)) + Nlog(1-p)
d(logP)/dN = 0 ==> N = 1/log(1/(1-p))

【在 g**********y 的大作中提到】
: 13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
: 1/k > p >= 1/(k+1) 时,该发k份邀请。

g**********y
发帖数: 14569
99
前面的推导对,后面我不sure:你怎么证明最大值一定出现在微分为0的地方而且解唯一?我的微积分是忘干净了。
我是直接推算的,f(N) = N*p*(1-p)^(N-1)
f(N+1) - f(N) = (1-p)^(N-1) * [(N+1)*p*(1-p) - N*p]
sgn[f(N+1) - f(N)] = sgn[(N+1)*p*(1-p) - N*p]
= sgn[p(1-(N+1)*p] = sgn[1-(N+1)*p]
递归可以证明:
p = 1~1/2, N=1最好
p = 1/2~1/3, N=2最好
p = 1/3~1/4, N=3最好
。。。

【在 b******v 的大作中提到】
: 第13题我的想法是这样的,不知道对不对
: P(exactly one accept) = N*p*(1-p)^{N-1}
: logP = logN + log(p/(1-p)) + Nlog(1-p)
: d(logP)/dN = 0 ==> N = 1/log(1/(1-p))

m****t
发帖数: 555
100
两种方法应该都对。
最大值出现在导数为0的地方,很容易证明。
当N < 1/log(1/(1-p)) 时,P的导数大于0,说明P单调递增;
当N > 1/log(1/(1-p)) 时,P的导数小于0,说明P单调递减;
所以N = 1/log(1/(1-p))时,P取最大值。

唯一?我的微积分是忘干净了。

【在 g**********y 的大作中提到】
: 前面的推导对,后面我不sure:你怎么证明最大值一定出现在微分为0的地方而且解唯一?我的微积分是忘干净了。
: 我是直接推算的,f(N) = N*p*(1-p)^(N-1)
: f(N+1) - f(N) = (1-p)^(N-1) * [(N+1)*p*(1-p) - N*p]
: sgn[f(N+1) - f(N)] = sgn[(N+1)*p*(1-p) - N*p]
: = sgn[p(1-(N+1)*p] = sgn[1-(N+1)*p]
: 递归可以证明:
: p = 1~1/2, N=1最好
: p = 1/2~1/3, N=2最好
: p = 1/3~1/4, N=3最好
: 。。。

相关主题
谁有trapping rain water的code?不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded
java多线程问题请教 (转载)M onsite面经
leetcode那道longest valid parenthese的题很诡异请问如果要求in place的话,递归是不是就不能用了?
进入JobHunting版参与讨论
g**********y
发帖数: 14569
101
第8题,O(n)
public int hold(int[] a) {
Stack stack = new Stack();
int level = 0;
int total = 0;
for (int i=0; i while (!stack.isEmpty()) {
Bar prev = stack.peek();
if (prev.height <= a[i]) {
total += (i - prev.x - 1) * (prev.height - level);
level = prev.height;
stack.pop();
}
else {
break;
}
}

if (stack.isEmpty()) level = 0;
stack.push(new Bar(i, a[i]));
}
return total;
}

class Bar {
int x;
int height;
public Bar(int x, int height) {
this.x = x;
this.height = height;
}
}
b*****n
发帖数: 760
102
13 题数学题
不要用连续函数思考, 要用离散
用log那种做法不是非常严谨
加入N是最大可能性, 那么 f(N+1)
b*****n
发帖数: 760
103
不清楚啊, 应该就是一般的developer吧。。。。
不是很senior的职位吧
LD面试回来直说, google还是不错的, 问得问题还是很有水平的。。。。。

【在 a**********2 的大作中提到】
: bless,这个是什么 level的题吗?
: 8. 遍历 n*second max 复杂度该是多少啊?
: 12. bitset还是list啊?
: 谁来讲讲

b*****n
发帖数: 760
104
LD面试回来后, 给偶做, 提示了不能用连续函数来思考, 要用离散, 偶大概前前后
后做了30分钟搞定, 现场LD也没有完全作对。。。。。

【在 g**********y 的大作中提到】
: 13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
: 1/k > p >= 1/(k+1) 时,该发k份邀请。

y***m
发帖数: 7027
105
为啥不是 1/p 的取整?
e.g. p=0.3
0.3*3+0.1=1 应该发4份?

【在 b*****n 的大作中提到】
: 13 题数学题
: 不要用连续函数思考, 要用离散
: 用log那种做法不是非常严谨
: 加入N是最大可能性, 那么 f(N+1)
b*****n
发帖数: 760
106
是取整 啊。。。。
1/P down to 1/P+1 取整

【在 y***m 的大作中提到】
: 为啥不是 1/p 的取整?
: e.g. p=0.3
: 0.3*3+0.1=1 应该发4份?

o*******y
发帖数: 810
107
赞很详细的面经!
bless!
d*******d
发帖数: 2050
108
c++ version
int water(int * a, int n){
stack > mystack;
int water = 0;
for(int i = 0; i while(!mystack.empty() && mystack.top().second <= a[i]){
int bottom = mystack.top().second;
mystack.pop();
if( mystack.empty())
break;
int top = mystack.top().second < a[i]? mystack.top().second: a[i];
int length = i - mystack.top().first - 1;
water = water + (top-bottom)* length;
}
mystack.push(make_pair(i, a[i]));
}
return water;
}

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

l*****e
发帖数: 1374
109
记得好像有一道题大致意思是:
给出一个矩阵,每个元素表示那个方格的高度,然后求下雨后能装多少水。
例如:
2,2,2
2,1,2
2,2,2
返回1。
有什么好方法吗?

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

g**********y
发帖数: 14569
110
考古一下吧,那是个经典题,用flood fill做的。

【在 l*****e 的大作中提到】
: 记得好像有一道题大致意思是:
: 给出一个矩阵,每个元素表示那个方格的高度,然后求下雨后能装多少水。
: 例如:
: 2,2,2
: 2,1,2
: 2,2,2
: 返回1。
: 有什么好方法吗?

相关主题
请问如果要求in place的话,递归是不是就不能用了?贡献几个 c 电面问题
面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。两个月没做题了~~
Linux context switch 高通 面试题。??G题,F题,leetcode题
进入JobHunting版参与讨论
X********i
发帖数: 28
111
Bless.
c******r
发帖数: 225
112
程序我看懂了,可是题我没有读懂
能用白话文给我讲一遍,这题什么意思吗?

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

h***m
发帖数: 1869
113
bless.

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

P**********c
发帖数: 3417
114
Lots of multi-threading.

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

n***y
发帖数: 2730
115
13: Negative Binomial Distribution! Google it.
a***u
发帖数: 383
116
不确定自己是否理解题意。对这个程序有点疑问,如果给的数组中第一个数是最大的,
那么程序的返回值total是0?

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

P**********c
发帖数: 3417
117
ms有问题,每次for结尾都push进stack,而且prev.x是在pop之前用, total+的似乎永远
是0。

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

b*******y
发帖数: 2048
118
big bless
P**********c
发帖数: 3417
119
这个应该是对的。

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

P**********c
发帖数: 3417
120
1,5,7,9,10,12没人讨论吗?感觉是复习multithread的好题目。

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

相关主题
Reverse Words in a StringAmazon coding question
遍历二叉树除了recursion还有啥好办法?题目: iterative binary tree post order traversal
G电面challenge: 找bug
进入JobHunting版参与讨论
z*****1
发帖数: 130
121
Bless !!

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

g*****i
发帖数: 2162
122
同求多线程题目的思路

【在 P**********c 的大作中提到】
: 1,5,7,9,10,12没人讨论吗?感觉是复习multithread的好题目。
:
: the
: called
: all
: 890

g*****i
发帖数: 2162
123
是不是应该加个判断保证top>bottom?

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

g**********y
发帖数: 14569
124
我写的那个有错,dinohound那个是对的,而且简洁。

【在 a***u 的大作中提到】
: 不确定自己是否理解题意。对这个程序有点疑问,如果给的数组中第一个数是最大的,
: 那么程序的返回值total是0?

v********y
发帖数: 2
125

It should be one invitation. the probability is: p
if you sent two invitation, the probability will be: p(1-p) < p

【在 g**********y 的大作中提到】
: 13题是数学题,很久不做这种,现场还真不知道能不能不出错搞出来
: 1/k > p >= 1/(k+1) 时,该发k份邀请。

L*****s
发帖数: 24744
126
难度比我那时候容易多了..
t****x
发帖数: 20
127
I don't think the code works. Suppose input is {9, 8, 7, 6} (descending
order), then the code only pushes the numbers into stack then return. The
water is 0.

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

d*******d
发帖数: 2050
128
这个就该是0阿.谁都沿着斜坡流走了,呆不住.

【在 t****x 的大作中提到】
: I don't think the code works. Suppose input is {9, 8, 7, 6} (descending
: order), then the code only pushes the numbers into stack then return. The
: water is 0.

P**********c
发帖数: 3417
129
top==bottom的时候total+0, 所以不判断也可以。不会有top
【在 g*****i 的大作中提到】
: 是不是应该加个判断保证top>bottom?
t****x
发帖数: 20
130
My bad. I misunderstood the question.

【在 d*******d 的大作中提到】
: 这个就该是0阿.谁都沿着斜坡流走了,呆不住.
相关主题
这个histgram的最大面积问题java多线程问题请教 (转载)
新鲜的T电面题leetcode那道longest valid parenthese的题很诡异
谁有trapping rain water的code?不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded
进入JobHunting版参与讨论
s*******e
发帖数: 4188
131
讨论一下#1:
我的思路(Java)一直是用synchronized,so the idea is:
public synchronized void add(int n) {
sum += n;
count++;
}
public synchronized void average() {
return sum/count;
}
曾经有个interviewer问我不用synchronized keyword (no synchronized method or
synchronized block)怎么办?所以想听听别的思路。
g***s
发帖数: 3811
132
the question assumed 99% cpu are used for average. so adding a field '
average' will improve performance much. After doing that, we dont need
synchronized for average(), but dont forget to add volatile key for the
field.

【在 s*******e 的大作中提到】
: 讨论一下#1:
: 我的思路(Java)一直是用synchronized,so the idea is:
: public synchronized void add(int n) {
: sum += n;
: count++;
: }
: public synchronized void average() {
: return sum/count;
: }
: 曾经有个interviewer问我不用synchronized keyword (no synchronized method or

s*******e
发帖数: 4188
133
#4 我的思路是recursion:
int reverseN = reverse(n, 0);
public static int reverse(int n, int sum) {
if (n == 0) return sum;
sum = sum*10 + n%10;
return reverse(n/10, sum);
}
还有别的什么想法?
g***s
发帖数: 3811
134
try to avoid tail-recursion.

【在 s*******e 的大作中提到】
: #4 我的思路是recursion:
: int reverseN = reverse(n, 0);
: public static int reverse(int n, int sum) {
: if (n == 0) return sum;
: sum = sum*10 + n%10;
: return reverse(n/10, sum);
: }
: 还有别的什么想法?

r*******n
发帖数: 344
135
How about using readWriteLock, readLock in average(), writeLock in add()...

【在 s*******e 的大作中提到】
: 讨论一下#1:
: 我的思路(Java)一直是用synchronized,so the idea is:
: public synchronized void add(int n) {
: sum += n;
: count++;
: }
: public synchronized void average() {
: return sum/count;
: }
: 曾经有个interviewer问我不用synchronized keyword (no synchronized method or

g**********y
发帖数: 14569
136
public int reverse(int n) {
return n>=0? reversePositive(n) : -reversePositive(-n);
}

private int reversePositive(int n) {
return Integer.parseInt(new StringBuilder("" + n).reverse().toString());
}

【在 s*******e 的大作中提到】
: #4 我的思路是recursion:
: int reverseN = reverse(n, 0);
: public static int reverse(int n, int sum) {
: if (n == 0) return sum;
: sum = sum*10 + n%10;
: return reverse(n/10, sum);
: }
: 还有别的什么想法?

a**********2
发帖数: 340
137
int reverseInteger(int val)
{
int result =0 ;
while(val)
{
result *=10;
result += val%10;
val /= 10;
}
return result;
}

【在 s*******e 的大作中提到】
: #4 我的思路是recursion:
: int reverseN = reverse(n, 0);
: public static int reverse(int n, int sum) {
: if (n == 0) return sum;
: sum = sum*10 + n%10;
: return reverse(n/10, sum);
: }
: 还有别的什么想法?

n***y
发帖数: 2730
138
13: last night I did not have time to go to detail. Here are some quick
thoughts on this problem for after work entertaining.
1-----
This is very close to negative binormial distribution (see http://en.wikipedia.org/wiki/Negative_binomial_distribution). According to wiki, let r be number of failure (use wiki term, which means accept invitation here), then the mode for nbd should be at floor(pr/(1-p)). (p is probability of sucess, same as in wiki)
Now noticing
p(NB(r, p) = k) = p (r-1 failures in k + r-1 trials) * p (failure). After
some index switch, we can see that p(r failure with N = k+r trials) reach
maximum at N = floor(r/1-p).
Now, we are assuming accept is failure, using the p as same meaning in prob
13 and r=1, we can see that the probabilty reaches the maximum at floor(1/P)
, here P is the probability mentioned in 13.
2----
berktov mentioned that the N should be a number neighboring -1/ln(1-p). This
answer is also correct. It is not hard to see:
0 < (1/p) + 1/ln(1-p) < 1.
3----
LZ mentioned solving f(N+1) But I am not impressed....
s*******e
发帖数: 4188
139
can you elaborate with some (pseudo-)code?

【在 r*******n 的大作中提到】
: How about using readWriteLock, readLock in average(), writeLock in add()...
s*******e
发帖数: 4188
140
you are right. I didn't try to address the optimization part.
is there a general approach in place of synchronized method or block?

【在 g***s 的大作中提到】
: the question assumed 99% cpu are used for average. so adding a field '
: average' will improve performance much. After doing that, we dont need
: synchronized for average(), but dont forget to add volatile key for the
: field.

相关主题
不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。
M onsite面经Linux context switch 高通 面试题。??
请问如果要求in place的话,递归是不是就不能用了?贡献几个 c 电面问题
进入JobHunting版参与讨论
g***s
发帖数: 3811
141
因为只有<1%的时间在add上面,所以用synchronized/lock/cas差别不大,用
synchronized反而简单易懂。

【在 s*******e 的大作中提到】
: you are right. I didn't try to address the optimization part.
: is there a general approach in place of synchronized method or block?

s*******e
发帖数: 4188
142
this is what i'm looking for.

【在 a**********2 的大作中提到】
: int reverseInteger(int val)
: {
: int result =0 ;
: while(val)
: {
: result *=10;
: result += val%10;
: val /= 10;
: }
: return result;

C***U
发帖数: 2406
143
要学多久才能回答上这些问题啊
刚准备开始学cs

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

b********1
发帖数: 49
144
第八题怎么是5?
从3到10 (0 based).
我用的是O(N3), 慢但应该正确呀...
import java.util.*;
public class Water {
int test(int arr[]) {
int N = arr.length;
int max = 0;
for (int i = 0; i < N - 2; i++) {
for (int j = i + 2; j < N; j++) {
int min = Math.min(arr[i], arr[j]);
int sum =0;
for (int k = i + 1; k < j; k++) {
int v = min - arr[k];
if (v > 0) {
sum += v;
}
}
if (sum > max) {
max = sum;
System.out.println(i + " -> " + j);
}
}
}
return max;
}

public static void main(String args[]) {
System.out.println(new Water().test(new int[]{0,1,0,2,1,0,1,3,2,1,2,
1}));
}
}
h***i
发帖数: 1970
145
tail-recursion是好事吧,编译器可以优化。

【在 g***s 的大作中提到】
: try to avoid tail-recursion.
g***s
发帖数: 3811
146
编译器可以优化是指它不好,在编译的时候来避免掉。不是所有编译器都能做这个优化
的。

【在 h***i 的大作中提到】
: tail-recursion是好事吧,编译器可以优化。
P**********c
发帖数: 3417
147
第9题是放一个timer再放一个static的counter吗?
第12题的意思是说这个class是个integer, 可以无穷大,还是说这个class有无穷多个
integer?
add (int) 是说加进一个integer到这个set, 还是这个integer和BigInt相加?

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

b*****n
发帖数: 760
148
LD看大家讨论这么激烈, 让我在第一楼加上了自战解说。。。。
让讨论来的更加激烈吧。。。。。。
P**********c
发帖数: 3417
149
第12题vector能存多少数是有上限的吧,毕竟它的size()返回值其实是
unsigned int. 存成vector的好处是什么呢?

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

c*******t
发帖数: 39
150
I feel that it is just an easy application of expectation.

prob
P)

【在 n***y 的大作中提到】
: 13: last night I did not have time to go to detail. Here are some quick
: thoughts on this problem for after work entertaining.
: 1-----
: This is very close to negative binormial distribution (see http://en.wikipedia.org/wiki/Negative_binomial_distribution). According to wiki, let r be number of failure (use wiki term, which means accept invitation here), then the mode for nbd should be at floor(pr/(1-p)). (p is probability of sucess, same as in wiki)
: Now noticing
: p(NB(r, p) = k) = p (r-1 failures in k + r-1 trials) * p (failure). After
: some index switch, we can see that p(r failure with N = k+r trials) reach
: maximum at N = floor(r/1-p).
: Now, we are assuming accept is failure, using the p as same meaning in prob
: 13 and r=1, we can see that the probabilty reaches the maximum at floor(1/P)

相关主题
两个月没做题了~~遍历二叉树除了recursion还有啥好办法?
G题,F题,leetcode题G电面
Reverse Words in a StringAmazon coding question
进入JobHunting版参与讨论
s*****y
发帖数: 897
151
为什么不用linklist的?

【在 P**********c 的大作中提到】
: 第12题vector能存多少数是有上限的吧,毕竟它的size()返回值其实是
: unsigned int. 存成vector的好处是什么呢?
:
: the
: called
: all
: 890

P**********c
发帖数: 3417
152
linked list idea不错。怎么实现add(int)呢。要先把int转换成linked list, 然后
recursive的进位吗?

【在 s*****y 的大作中提到】
: 为什么不用linklist的?
P**********c
发帖数: 3417
153
第9题用queue存time stamp, multithreading的情况下是每次update queue的时候都
lock吗?

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

k*******n
发帖数: 16
154
第四题不考虑负数?
P**********c
发帖数: 3417
155
考虑吧,例子里就有-890 -98,这个记录一个flag就可以了,不难实现。

【在 k*******n 的大作中提到】
: 第四题不考虑负数?
h**********8
发帖数: 267
156
mark
k*******n
发帖数: 16
157
是的,只是这么多天没人提,我也只是提醒下大家表忘了。这题没难度,是不是面试的
题目不仅答对还要最优???我一直是那种只要能编译通过从来不考虑complexity的人
...以后得加强这方面训练了。

【在 P**********c 的大作中提到】
: 考虑吧,例子里就有-890 -98,这个记录一个flag就可以了,不难实现。
k****n
发帖数: 1334
158
这个算法是神马意思,谁给解释一下?

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

g*****i
发帖数: 2162
159
我觉得应该要lock吧

【在 P**********c 的大作中提到】
: 第9题用queue存time stamp, multithreading的情况下是每次update queue的时候都
: lock吗?
:
: the
: called
: all
: 890

m**q
发帖数: 189
160
Good work.
I just coded a version using O(1) extra space, the down side
is it's not as clean (sigh) and I wasn't able to code it bug free
in my first try. The below code works for the specified input.
int water2(int a[], int n)
{
int i=0, prev=INT_MIN, sum=0;
int prev_high, new_high, prev_index;

while (i=prev) {
prev=a[i]; i++;
}
if (i==n)
return 0;

prev_high=prev, prev_index=i-1;

while (i int tmp=0;
while(i int diff = prev_high-a[i];
tmp += diff;
prev=a[i]; i++;
}
if (i==n)
return sum;
while (i=prev) {
if (a[i] < prev_high) {
int diff = prev_high - a[i];
tmp += diff;
}
prev=a[i]; i++;
}
new_high=prev;
if (new_high < prev_high) {
tmp -= (prev_high-new_high) * (i-1-prev_index);
}

prev_high=new_high; prev_index=i-1;
sum += tmp;

}

return sum;
}

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

相关主题
题目: iterative binary tree post order traversal新鲜的T电面题
challenge: 找bug谁有trapping rain water的code?
这个histgram的最大面积问题java多线程问题请教 (转载)
进入JobHunting版参与讨论
m**q
发帖数: 189
161
The code looks pretty neat. Draw the picture on a paper
it should be more clear.

【在 k****n 的大作中提到】
: 这个算法是神马意思,谁给解释一下?
t**r
发帖数: 3428
162
谢谢LZ分享
慢慢学习
i**********e
发帖数: 1145
163
反例:
[5,4,1,2]
Your code returns -1.

【在 m**q 的大作中提到】
: Good work.
: I just coded a version using O(1) extra space, the down side
: is it's not as clean (sigh) and I wasn't able to code it bug free
: in my first try. The below code works for the specified input.
: int water2(int a[], int n)
: {
: int i=0, prev=INT_MIN, sum=0;
: int prev_high, new_high, prev_index;
:
: while (i=prev) {

i**********e
发帖数: 1145
164
除了以上的反例之外,你的代码还不能处理比较复杂的情况:
例如:
【5 5 1 7 1 1 5 2 7 6】
你忽略了上面 左7 与 右7 在上边能装上 8 的状况。
你的代码返回的是 15,而正确的答案应该是 23.
利用 stack 的思路是 O(n) time 和 O(n) space,O(n) space 的状况就是一个严格递
减的数组。
不知道有没有 O(n) time 和 O(1) space 的解,直觉告诉我没有。
用 stack 的代码如下:
typedef pair PI;
int waterShed(int A[], int n) {
int total = 0;
stack s;
for (int i = 0; i < n; i++) {
int prevHeight = A[i];
while (!s.empty()) {
total += (min(s.top().first, A[i]) - prevHeight) * (i - s.top().second - 1);
prevHeight = s.top().first;
if (s.top().first > A[i]) break;
s.pop();
}
s.push(PI(A[i], i));
}
return total;
}
i**********e
发帖数: 1145
165
Try this input:
[4, 2, 3]

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

g**********y
发帖数: 14569
166
我的那个代码有错。LZ给的答案就是O(n)复杂度,O(1)空间的。
public int hold(int[] a) {
int h = 0;
int N = a.length;
for (int i=1; i a[h]) h = i;
int total = 0;
int left = a[0];
for (int i=1; i if (a[i] > left) {
left = a[i];
}
else {
total += left - a[i];
}
}
int right = a[N-1];
for (int i=N-2; i>h; i--) {
if (a[i] > right) {
right = a[i];
}
else {
total += right - a[i];
}
}
return total;
}

【在 i**********e 的大作中提到】
: Try this input:
: [4, 2, 3]

s*****y
发帖数: 897
167
好牛的方法,简单得来不容易code错。

【在 g**********y 的大作中提到】
: 我的那个代码有错。LZ给的答案就是O(n)复杂度,O(1)空间的。
: public int hold(int[] a) {
: int h = 0;
: int N = a.length;
: for (int i=1; i a[h]) h = i;
: int total = 0;
: int left = a[0];
: for (int i=1; i: if (a[i] > left) {
: left = a[i];

m**q
发帖数: 189
168
嗯,我的代码是错的,把问题考虑的太简单了。

【在 i**********e 的大作中提到】
: 除了以上的反例之外,你的代码还不能处理比较复杂的情况:
: 例如:
: 【5 5 1 7 1 1 5 2 7 6】
: 你忽略了上面 左7 与 右7 在上边能装上 8 的状况。
: 你的代码返回的是 15,而正确的答案应该是 23.
: 利用 stack 的思路是 O(n) time 和 O(n) space,O(n) space 的状况就是一个严格递
: 减的数组。
: 不知道有没有 O(n) time 和 O(1) space 的解,直觉告诉我没有。
: 用 stack 的代码如下:
: typedef pair PI;

d***n
发帖数: 65
169
第八题, 贴个时间O(n),空间O(1)的,试了前面提到的所有test case。貌似全都正确
。整个数组只需扫描一遍。
int floodFill(int B[], int n)
{
int w = 0;
int i = 0, j = n;
int maxL = 0, maxR = 0;
while(i {
while(B[i]<=max(maxL, maxR) && i {
if(B[i]>maxL)
maxL = B[i];
else
w +=maxL - B[i];
i++;
}
if(i==j) break;
maxL = B[i];//found new high
while(B[--j] {
if(B[j]>maxR)
maxR = B[j];
else
w += maxR - B[j];
}
if(i==j) break;
maxR = B[j];//may be higher than maxL
i++;
}
return w;
}
n******n
发帖数: 49
170
在想这题的时候 觉得它的思路跟largest rectangle under histogram的stack解法很
像。只是现在是栈顶元素比当前元素小的时候,弹出栈顶元素;之前histogram那题是
栈顶元素比当前元素大的时候,弹出。
原先那道histogram的题参考这个
http://wansishuang.appspot.com/?p=3002

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

相关主题
java多线程问题请教 (转载)M onsite面经
leetcode那道longest valid parenthese的题很诡异请问如果要求in place的话,递归是不是就不能用了?
不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。
进入JobHunting版参与讨论
s*******f
发帖数: 1114
171
//4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -
890
//--> -98.
int ReverseDigits (int n){
int ret = 0;
while (n){
ret = ret * 10 + n % 10;
n /= 10;
}
return ret;
}
s*******f
发帖数: 1114
172
楼主以前做trading systme吗? 这么多多线程。
s*******f
发帖数: 1114
173
//8. Given a non-negative integer array representing an elevation map
whereas
//the width of each bar is 1, compute how much water it can contain after
//raining. For example, given [0,1,0,2,1,0,1,3,2,1,2,1], returns 6. What is
//the complexity of your solution?
// 1 time scan; O(1) space
int HoldWater(int a[], int len){
int i = 0;
int j = len - 1;
int sum;
int ibig = INT_MIN;
int jbig = INT_MIN;
while (i <= j){
if (ibig < jbig){
if (ibig < a[i]){
ibig = a[i];
}else{
sum += ibig - a[i];
}
++i;
}else{
if (jbig < a[j]){
jbig = a[j];
}else{
sum += jbig - a[j];
}
--j;
}
}
return sum;
}
m*****k
发帖数: 731
174
can you try [3, 0, 1] ?

【在 g**********y 的大作中提到】
: 第8题,O(n)
: public int hold(int[] a) {
: Stack stack = new Stack();
: int level = 0;
: int total = 0;
: for (int i=0; i: while (!stack.isEmpty()) {
: Bar prev = stack.peek();
: if (prev.height <= a[i]) {
: total += (i - prev.x - 1) * (prev.height - level);

e***s
发帖数: 799
175
Bless, MARK下慢慢看
l****s
发帖数: 165
176
Google jobs:
http://jobguiding.com/it-jobs/it-companies/google.html

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

e***s
发帖数: 799
177
不用FLAG,负数求出来的余数也是负数吧

【在 P**********c 的大作中提到】
: 考虑吧,例子里就有-890 -98,这个记录一个flag就可以了,不难实现。
c**j
发帖数: 103
178
13. 我从2,3 推到n,maximize: np(1-p)^{n-1}
求导。。。。
对不对?
d*******u
发帖数: 186
179
能不能用白话文解释一下,多谢。

【在 d*******d 的大作中提到】
: c++ version
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){
: int bottom = mystack.top().second;
: mystack.pop();
: if( mystack.empty())
: break;

d*******u
发帖数: 186
180
白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡序列),上坡时:如果
有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
知道过完为止。

【在 d*******u 的大作中提到】
: 能不能用白话文解释一下,多谢。
相关主题
Linux context switch 高通 面试题。??G题,F题,leetcode题
贡献几个 c 电面问题Reverse Words in a String
两个月没做题了~~遍历二叉树除了recursion还有啥好办法?
进入JobHunting版参与讨论
d*******u
发帖数: 186
181
//白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡
序列),上坡时:如果
//有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
//知道过完为止。
int water(int * a, int n){
stack > mystack;
int water = 0;
for(int i = 0; i while(!mystack.empty() && mystack.top().second <= a[i]){ //上坡
int bottom = mystack.top().second; //底
mystack.pop();
if( mystack.empty()) //堆栈中只有一个元素,不能形成底。
break;
int top = mystack.top().second < a[i]? mystack.top().second: a[i];
//找到顶(次大数)
int length = i - mystack.top().first - 1; //长度
water = water + (top-bottom)* length; //填充
} //当前上坡处理完。
mystack.push(make_pair(i, a[i])); //下坡,进堆栈。
//上坡无底的话,是什么都不做的,因为上次进堆栈的元素直接出堆栈了,只是查
了一下底(至少两元素在堆栈)。
}
return water;


序列),上坡时:如果

【在 d*******u 的大作中提到】
: 白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡序列),上坡时:如果
: 有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
: 知道过完为止。

d*******u
发帖数: 186
182
有没有人说一说如果是二维数组的, flood fill怎么做呀?

【在 d*******u 的大作中提到】
: //白话文应该是: 下坡时把数进堆栈 (这样堆栈中最上元素是最小的,保存一个下坡
: 序列),上坡时:如果
: //有底的话(堆栈中至少有两个元素),找出底,flood(累加,顶减底乘长度)。
: //知道过完为止。
: int water(int * a, int n){
: stack > mystack;
: int water = 0;
: for(int i = 0; i: while(!mystack.empty() && mystack.top().second <= a[i]){ //上坡
: int bottom = mystack.top().second; //底

c***8
发帖数: 188
183
many thanks

the
called
all
890

【在 b*****n 的大作中提到】
: 1. You have a class that supports to input sample records and to compute the
: average of the samples. The class has two members: total and count. How
: would you make the class thread-safe? If 99% of the time average() is called
: , how to optimize for that?
: 2. Talk about your recent interesting project/bug.
: 3. You have 100 files, each containing 10G sorted integers. How to merge all
: integers into one sorted file?
: 4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
: --> -98.
: 5. Discuss design challenges of a distributed web crawler running on

d****d
发帖数: 2919
184
用求导数算出来的,是n=-1/ln(1-p),
p小于1时 ln(1-p) 的近似值就是 -p。
(在p=0处泰勒展开,保留一次项。)
所以近似以后就是1/p了。

唯一?我的微积分是忘干净了。

【在 g**********y 的大作中提到】
: 前面的推导对,后面我不sure:你怎么证明最大值一定出现在微分为0的地方而且解唯一?我的微积分是忘干净了。
: 我是直接推算的,f(N) = N*p*(1-p)^(N-1)
: f(N+1) - f(N) = (1-p)^(N-1) * [(N+1)*p*(1-p) - N*p]
: sgn[f(N+1) - f(N)] = sgn[(N+1)*p*(1-p) - N*p]
: = sgn[p(1-(N+1)*p] = sgn[1-(N+1)*p]
: 递归可以证明:
: p = 1~1/2, N=1最好
: p = 1/2~1/3, N=2最好
: p = 1/3~1/4, N=3最好
: 。。。

1 (共1页)
进入JobHunting版参与讨论
相关主题
题目: iterative binary tree post order traversalM onsite面经
challenge: 找bug请问如果要求in place的话,递归是不是就不能用了?
这个histgram的最大面积问题面试时候C++ pop之前是空 大家怎么处理。。返回什么。。 假设stack 元素都是int形的。
新鲜的T电面题Linux context switch 高通 面试题。??
谁有trapping rain water的code?贡献几个 c 电面问题
java多线程问题请教 (转载)两个月没做题了~~
leetcode那道longest valid parenthese的题很诡异G题,F题,leetcode题
不明白leetcode OJ wordladder 2 总是 Time Limit ExceededReverse Words in a String
相关话题的讨论汇总
话题: int话题: prev话题: sum话题: bar话题: return