查看: 3583| 回复: 19
跳转到指定楼层
上一主题 下一主题
收起左侧

[二分/排序/搜索] Google 面试技术题,求教考点在哪?

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Google面试的一道技术题,愣是没弄明白这题的考点在哪,哪个算法?面试官最后提到要用到一个data structure,面完后想了几天也没想到他到底考的什么。题目如下:

Say you have a web server and a logging component. The component has two functions as below. When a request come in to the web server, started(string, int) get called first, then completed(string, int) get called.

+started(reqId:string, timestamp:int64)
+completed(reqId:string, timestamp:int64)

You are required to print out a logging statement for each request in the following format. Sorted by the started time.

Request {ReqId1} started at {Y1} finished at {Z1}
Request {ReqId2} started at {Y2} finished at {Z2}
Request {ReqId3} started at {Y3} finished at {Z3}
Request {ReqId4} started at {Y4} finished at {Z4}

我当时想到可能是考多线程,说了半天多线程,他最后说我miss了题目的point。。。新手请教大家这题到底要考啥?



补充内容 (2019-2-20 18:30):
请教了一位Google的面试官,他给出的答案见7楼 https://www.1point3acres.com/bbs ... 069&pid=5710718

上一篇:求问一道面试算法题
下一篇:转发有人总结的狗家面筋
全局:
需要加一个超时处理,否则会卡在里面。这是我的Revised版本:

void completed(timestamp, requestId) {
  byId[requestId].tFinished = timestamp;
  while(!queue.isEmpty()) {

    if (queue.peek().tFinished != null) {
         byId.remove(queue.peek().id);
        LOGGING FINISH STATEMENT
    } else {
        if (queue.peek().tStarted < now - timeout) {
               byId.remove(queue.peek().id);
               LOGGING TIMEOUT STATEMENT
        }
    }
  }
}

评分

参与人数 1大米 +2 收起 理由
14417335 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

推荐
 楼主| andrew100 2019-2-14 12:50:10 | 只看该作者
全局:
Solution from Google SWE interviewer. Thanks to @bowenzh

Pseudocode:    建一个普通的class即可,class Request {id, tStarted, tFinished}
Queue<Request> queue;
HashMap<String, Request> byId;
void started(timestamp, requestId) {
  Request req = new Request(id=requestId, tStarted=timestamp);
  queue.enqueue(req);
  byId[requestId] = req;
}
void completed(timestamp, requestId) {
  byId[requestId].tFinished = timestamp;
  while(queue.peek().tFinished != null) {
    byId.remove(queue.peek().id);
    LOGGING STATEMENT
  }
}

评分

参与人数 3大米 +3 收起 理由
jeromexlee + 1 很有用的信息!
14417335 + 1 给你点个赞!
kkchenk + 1 赞一个

查看全部评分

回复

使用道具 举报

推荐
 楼主| andrew100 2019-2-7 07:13:00 | 只看该作者
全局:
步惊云 发表于 2019-2-7 06:16
只能瞎猜了..

1)request进来先进先出, 那就是你理解的. 每一个的起始跟结束时间的差只看task, 但是起始 ...

一个request进来先call started(), 执行完task后call completed(), int parameter就是要打印在log里的时间, 打印的顺序要按started()里的这个int的值顺序.

不会真像楼上那位说的就是排序吧, call started()的时候把ReqId and Y存进hash map, call completed()的时候排序+打印hash map的内容?
回复

使用道具 举报

全局:
是不是考排序啊……
回复

使用道具 举报

🔗
alexws 2019-2-7 05:20:47 | 只看该作者
全局:
Y1 Y2 Y3 Y4 跟 Z1 Z2 Z3 Z4 之间有什么前后关系么?

在started里面用个 ConcurrentHashMap 把 start timestamp 用 reqid 存起来,然后在completed里面再取出来?
回复

使用道具 举报

🔗
 楼主| andrew100 2019-2-7 10:24:40 | 只看该作者
全局:
alexws 发表于 2019-2-7 05:20
Y1 Y2 Y3 Y4 跟 Z1 Z2 Z3 Z4 之间有什么前后关系么?

在started里面用个 ConcurrentHashMap 把 start ti ...

Y, Z是request 的开始时间和结束时间, Y1->Z1, Y2->Z2, Y3->Z3, Y4->Z4. log的打印顺序要求按Y排序. 他最后提到我miss one data structure. 可能就是说的hashmap..他不是想问concurrency..
回复

使用道具 举报

🔗
 楼主| andrew100 2019-2-14 12:51:25 | 只看该作者
全局:
qingzi1993 发表于 2019-2-7 01:37
是不是考排序啊……

考Queue, 答案见我上面的回复
回复

使用道具 举报

🔗
 楼主| andrew100 2019-2-14 12:53:06 | 只看该作者
全局:
alexws 发表于 2019-2-7 05:20
Y1 Y2 Y3 Y4 跟 Z1 Z2 Z3 Z4 之间有什么前后关系么?

在started里面用个 ConcurrentHashMap 把 start ti ...

考Queue, 答案见我上面的回复
回复

使用道具 举报

🔗
 楼主| andrew100 2019-2-14 12:53:53 | 只看该作者
全局:
步惊云 发表于 2019-2-7 06:16
只能瞎猜了..

1)request进来先进先出, 那就是你理解的. 每一个的起始跟结束时间的差只看task, 但是起始 ...

终于弄清楚了. 应该是考Queue, 答案见我上面的回复
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表