An interview question from MicroStrategy

去年校招时的一道面试题,觉得蛮有意思,贴出来。

 

Question:

 

Spy start at a, during an interval he moves |b| to right when b >=0; moves |b| to left when b <0; I can ask the spy if he is in position x, if he is then I win.


Answer:


Here is how I analyze this problem:

1) First assume a = 0; then b could be 0, 1, 2….

i. |b| = 0 , I can guess 0 at first interval just caught the spy;

ii. |b| = 1 , then b could be 1 or -1, so I can guess 1 at first interval ; if not caught the spy, guess -2 at second interval and I will catch the spy;

iii. |b| = 2 , then b could be 2 or -2, so I can guess 2 at first interval ; if not caught the spy, guess -4 at second interval and I will catch the spy;

iv. ……

To sum it up ,I will take this list to catch the spy with n intervals,n start at 1:

 

0*1, 1*2, (-1)*3, (2)*4, (-2)*5, 3*6, (-3)*7, 4*8, (-4)*9 …… ((n/2)(-1)^n)*n;

 


2) But |a| can have many values not only zero ,when a=0,this is one-dimensional problem. We can regard (a, b) as a point in a Plane coordinate system .

  


This make the (a,b) in many squares, and we find the spy’s location from the most inside square to outside, and in every square we start at the point(|a|,0) as the red point showed in the picture. The same to the first problem is the time interval increases in Linear growth. And we can use the point’s value to calculate the location referenced to the point ,and can get the spy.



For example
1) (0,0): 0 + 0*1(this is the number of intervals);

2) (1,0): 1+ 0*2; (1,1): 1+ 1*3; (0,1): 0+ 1*4; (-1,1): -1+ 1*5……

3) As the picture shows;

posted @   刘浩de技术博客  阅读(753)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示