由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 再发两道F电面题
相关主题
砸了面试,发面题google电面第一轮面经 求bless
问一道无聊的bloomberg电面题帮我看看这两个题目回答
贡献一道电面题二维数组参数怎么传好?
问一个电面题一道image processing题
问个bit struct的面试题 急atoi overflow怎么办?
看一道面试题一个C语言概念题
问一个关于xor的题我的几个面试算法解答。
离奇的Amzaon第一轮电面分享NVIDIA的第一轮面试题
相关话题的讨论汇总
话题: bits话题: known话题: sntnl话题: value话题: in2
进入JobHunting版参与讨论
1 (共1页)
p****e
发帖数: 37
1
1. print a linked list reversely.
I write 3 methods,
1. recursive
2. use a temporary vector to save list nodes
3. reverse the linked list first, and then print (we could reverse it back
if required)
Then I was asked in which situation method 2 is better than 3. (the answer
he want is multi-thread, I didn't get it :( )
2. Suppose we have a following struct:
struct Bits{ unsigned known, unsigned value};
if a certain bit in known is set, it means the value of this bit is known,
and its corresponding value is saved in Bits->value.
for example, if known = 0011, value = 0001, that means Bits has last 2 bits
value known, one is 0 and the other is 1.
implement the following method to calculate the or of 2 Bits:
void or(Bits* out, const Bits& in1, const Bits& in2);
BTW, still didn't got hr's feedback yet....
g*****i
发帖数: 2162
2
bless
第一题多线程为啥2好?感觉list被其他thread修改的话两种方法都受影响啊
第二题value初始值都是0吗?如果是的话似乎3行code就出来了啊.
int k=in1.known & in2.known;
int v=in1.value | in2.value;
return k & v;
P**********c
发帖数: 3417
3
我觉得可能是解法2没有对原linked list做改动,对这个函数本身而言是thread safe
的。当然如果其他thread对它有改动还是会有问题,但是这个至少有可能做到thread
safe。如果大家都是读,不需要lock.
解法3 reverse的时候自己就把linked list改了,没法thread safe了,只能lock了。
而且这个lock必须从第一次reverse linked list开始,一直到reverse back
为止。否则期间其他thread access linked list就乱七八糟了。

【在 g*****i 的大作中提到】
: bless
: 第一题多线程为啥2好?感觉list被其他thread修改的话两种方法都受影响啊
: 第二题value初始值都是0吗?如果是的话似乎3行code就出来了啊.
: int k=in1.known & in2.known;
: int v=in1.value | in2.value;
: return k & v;

g*****i
发帖数: 2162
4
多谢解释

safe

【在 P**********c 的大作中提到】
: 我觉得可能是解法2没有对原linked list做改动,对这个函数本身而言是thread safe
: 的。当然如果其他thread对它有改动还是会有问题,但是这个至少有可能做到thread
: safe。如果大家都是读,不需要lock.
: 解法3 reverse的时候自己就把linked list改了,没法thread safe了,只能lock了。
: 而且这个lock必须从第一次reverse linked list开始,一直到reverse back
: 为止。否则期间其他thread access linked list就乱七八糟了。

c*******n
发帖数: 63
5
写个简单的第二题
void OR(Bits *out, const Bits &in1, const Bits &in2)
{
unsigned int sntnl = 1;
assert(out);
out->known = 0;
out->value = 0;
while(sntnl >0)
{
if( (in1.known&sntnl && in1.value&sntnl)
|| (in2.known&sntnl && in2.value&sntnl) )
{
out->value |= sntnl;
out->known |= sntnl;
}
else if(in1.known&sntnl && in2.known&sntnl)
{
out->known |= sntnl;
}
sntnl <<= 1;
}
}
f*******t
发帖数: 7549
6
第二题,有两种情况下输出的值是确定的
1.in1->known = 1, in2->known = 1
2.某个inX->known = 1,以及inX->value = 1,因为是or操作,所以输出必然是1
我觉得答案是:
out->known = (in1.known & in1.value) | (in2.known & in2.value) | (in1.known & in2.known);
out->value = in1.value | in2.value;
l**********d
发帖数: 29
7
第二题,这样如何?
out->known = in1.known | in2.known;
out->value = (in1.value&in1.known) | (in2.value&in2.known);
d****3
发帖数: 93
8
out->known = (in1->known&in1->value) | (in2->known&in2->value) | ((in1->
known&in2->known)&(in1->value|in2->value));
out->value = (in1->known&in1->value) | (in2->known&in2->value) | (in1->known
&in2->known);

【在 p****e 的大作中提到】
: 1. print a linked list reversely.
: I write 3 methods,
: 1. recursive
: 2. use a temporary vector to save list nodes
: 3. reverse the linked list first, and then print (we could reverse it back
: if required)
: Then I was asked in which situation method 2 is better than 3. (the answer
: he want is multi-thread, I didn't get it :( )
: 2. Suppose we have a following struct:
: struct Bits{ unsigned known, unsigned value};

r*******g
发帖数: 1335
9
看不懂第二题,物理意义是什么,到底是or两个known部分还是or两个value部分?
貌似Bits这个struct维护的就是哪些bit对应有value,这样的话貌似结果就是known的&
和value的|就行了?

【在 p****e 的大作中提到】
: 1. print a linked list reversely.
: I write 3 methods,
: 1. recursive
: 2. use a temporary vector to save list nodes
: 3. reverse the linked list first, and then print (we could reverse it back
: if required)
: Then I was asked in which situation method 2 is better than 3. (the answer
: he want is multi-thread, I didn't get it :( )
: 2. Suppose we have a following struct:
: struct Bits{ unsigned known, unsigned value};

1 (共1页)
进入JobHunting版参与讨论
相关主题
分享NVIDIA的第一轮面试题问个bit struct的面试题 急
被鄙视了的C语言题目,找工作真难啊看一道面试题
一道难题问一个关于xor的题
GOOGLE 第二轮电面离奇的Amzaon第一轮电面
砸了面试,发面题google电面第一轮面经 求bless
问一道无聊的bloomberg电面题帮我看看这两个题目回答
贡献一道电面题二维数组参数怎么传好?
问一个电面题一道image processing题
相关话题的讨论汇总
话题: bits话题: known话题: sntnl话题: value话题: in2