由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Two CS interview questions
相关主题
这个check whether a binary tree is a BST or notleetcode valid bst new test cases 过不去了。。。
判断 bst 疑问Regular expression matching 在什么输入下时间复杂度是O(2^n)?
G电面面经大家看看ms这个online test题什么做
讨论一道construct BST level by level的问题很奇怪的面试结果,真是很受打击+FB的面经
发个刚面完的rocket fuel的面经吧明天A家onsite
贡献一道题帮发一个同学面G家onsite的面经
大家帮我看看这个程序哪里有问题啊!!Leetcode 的 Surrounded Regions 好难过大OJ (in JAVA)
Elements of Programming Interviews 第16.1题答案是不是有问题?新鲜Google面经
相关话题的讨论汇总
话题: min话题: rand话题: returns话题: bool话题: max
进入JobHunting版参与讨论
1 (共1页)
c******a
发帖数: 198
1
1. Given a method (public int Rand()) that returns any random integer in the
int range, write a wrapper method that returns a random number within a
specific range [min, max], assuming int.MinValue <= min <= max <= int.
MaxValue.
my solution was: min + (Rand() - int.MinValue) * (max - min + 1) / (int.
MaxValue - int.MinValue + 1), and use long type to account for overflow
issue. is this solution correct?
2. Given a M*N matrix where some cells are blocked and there may or may not
be a cell which contains a target object. A person starts from a random cell
on the matrix and moves to find the object. You are given the below helper
method:
bool MoveUp() // Returns true and the person moves up if the upside adjacent
cell is within the border and is not blocked. Otherwise, returns false and
the person stays in current cell.
bool MoveDown()
bool MoveLeft()
bool MoveRight()
bool PickUp() // Returns true if the current cell contains the object.
Otherwise, returns false.
Describe the algorithm and write down the full code of a method (bool
FindObject(IPerson p)) to determine whether there is a target object on the
matrix or not. Note that the method takes an interface parameter.
j********r
发帖数: 127
2
1. return min + (rand() - min) % (max - min + 1);
2. DFS (depth first search) or BFS (breadth first search)

the
not
cell
helper

【在 c******a 的大作中提到】
: 1. Given a method (public int Rand()) that returns any random integer in the
: int range, write a wrapper method that returns a random number within a
: specific range [min, max], assuming int.MinValue <= min <= max <= int.
: MaxValue.
: my solution was: min + (Rand() - int.MinValue) * (max - min + 1) / (int.
: MaxValue - int.MinValue + 1), and use long type to account for overflow
: issue. is this solution correct?
: 2. Given a M*N matrix where some cells are blocked and there may or may not
: be a cell which contains a target object. A person starts from a random cell
: on the matrix and moves to find the object. You are given the below helper

c******a
发帖数: 198
3
is this also correct or wrong? if wrong, what are potential issues? thanks.
min + Rand() * (max - min + 1) / (int.MaxValue - int.
MinValue + 1)
j********r
发帖数: 127
4
溢出啊,来回类型cast啊,而且逻辑也完全错误,
你代入Rand() = 0, 和Rand()= int.MinValue算一下
min + Rand() * (max - min) / (int.MaxValue - int.
MinValue + 1)

【在 c******a 的大作中提到】
: is this also correct or wrong? if wrong, what are potential issues? thanks.
: min + Rand() * (max - min + 1) / (int.MaxValue - int.
: MinValue + 1)

f********y
发帖数: 156
5
From int_min to int_max, there are 2^32 numbers.
if 2^32 is not divisible by (max- min +1), then the probability is not
uniform.

【在 j********r 的大作中提到】
: 1. return min + (rand() - min) % (max - min + 1);
: 2. DFS (depth first search) or BFS (breadth first search)
:
: the
: not
: cell
: helper

j********r
发帖数: 127
6
两个整型区间互相映射,如果两者长度不互为倍数,不可能分布率完全一样。

【在 f********y 的大作中提到】
: From int_min to int_max, there are 2^32 numbers.
: if 2^32 is not divisible by (max- min +1), then the probability is not
: uniform.

c******a
发帖数: 198
7
sorry, my solution was below. this one should be logically correct. (the
previous one had a typo.)
min + (Rand() - int.MinValue) * (max - min + 1) / (int.MaxValue - int.
MinValue + 1)

【在 j********r 的大作中提到】
: 溢出啊,来回类型cast啊,而且逻辑也完全错误,
: 你代入Rand() = 0, 和Rand()= int.MinValue算一下
: min + Rand() * (max - min) / (int.MaxValue - int.
: MinValue + 1)

c******a
发帖数: 198
8
i like your modulo idea. but your solution may also have an overflow issue,
when rand() returns int.MaxValue and min is int.MinValue.
i think this modified one should work without overflow.
return min + rand() % (max - min + 1);

【在 j********r 的大作中提到】
: 两个整型区间互相映射,如果两者长度不互为倍数,不可能分布率完全一样。
T*****u
发帖数: 7103
9
bingo

【在 f********y 的大作中提到】
: From int_min to int_max, there are 2^32 numbers.
: if 2^32 is not divisible by (max- min +1), then the probability is not
: uniform.

f**********e
发帖数: 288
10
two cs 是哪家的。。
l*n
发帖数: 529
11
需要想办法把(M, N)和(m, n)映射到(0, N-M)和(0, n-m),这其中用long来处理
overflow。另外丢弃qmax*(n-m)<=x <=N-M的部分即可,以保证取模时的均匀分布。遇
到丢弃即重新拿一个随机数,最坏情况是丢弃近一半,但是5次不选中概率是1/(2^5),
十次不中是1/(2^10),最终期望是2次。

,

【在 c******a 的大作中提到】
: i like your modulo idea. but your solution may also have an overflow issue,
: when rand() returns int.MaxValue and min is int.MinValue.
: i think this modified one should work without overflow.
: return min + rand() % (max - min + 1);

1 (共1页)
进入JobHunting版参与讨论
相关主题
新鲜Google面经发个刚面完的rocket fuel的面经吧
求教一个, Leetcode 题.贡献一道题
请教两道F面试题的follow up大家帮我看看这个程序哪里有问题啊!!
bfs vs dfsElements of Programming Interviews 第16.1题答案是不是有问题?
这个check whether a binary tree is a BST or notleetcode valid bst new test cases 过不去了。。。
判断 bst 疑问Regular expression matching 在什么输入下时间复杂度是O(2^n)?
G电面面经大家看看ms这个online test题什么做
讨论一道construct BST level by level的问题很奇怪的面试结果,真是很受打击+FB的面经
相关话题的讨论汇总
话题: min话题: rand话题: returns话题: bool话题: max