Google VO 面试实录:排队可见人数统计——完整解析 – 面试辅助 – 代面试 – VO support

这是一场连续4轮的技术面试,不得不感慨谷歌的面试强度和效率。这次石老师给大家分享其中的一道高频题,这题由于很多人不仔细审题会导致错误,面试官抄喜欢出。

原题(English)

Imagine there are n people waiting in a line, all facing the same direction.
Each person has a different height. A person A can see another person B in front of them if there is no one taller than both A and B standing between them.

Q1: How many people can the last one in line see?
Example:
Input: [1, 10, 6, 7, 9, 8, 2, 4, 3, 5]
Output: 6

问题(中文翻译)

假设有 n 个人排成一队,面向同一方向,每人身高各不相同。若 AB 之间没有任何身高同时超过 AB 的人,则 A 能看到 B
问:队尾的那个人最多能看到多少人?示例输入 [1,10,6,7,9,8,2,4,3,5],输出 6

高频提示: 此题近期 Google VO 中已出现多次,关于“视野阻挡”误区可见这篇笔记

我们让候选人用眼神接触的AI与google meeting的面试官沟通,这样就不妨碍候选人查看我们写的代码提示了。

在题目出来的时候,我们第一时间写下了澄清问题和算法思路:

澄清问题

  • 我们可以假设输入列表非空吗?

算法思路

我认为可以通过遍历列表来解决:

  1. 从观者前面一个人开始,向左(队首方向)移动。
  2. 记录观者与当前目标之间已见过的最高身高。
  3. 若某人身高高于他们与观者之间所有人的身高,则该人可见。
  4. 持续统计这类可见人数,直到遍历完列表。


候选人看完了之后,用自己的话与面试官复述我们提供的思路。面试官感到非常满意,示意候选人可以进行下一步的coding操作。

由于这题我们已经做过多次了,所以这次我们光速写出了如下代面,并且提供了详细的注释供候选人和面试官沟通思路和讲解用。

        
def count_visible(heights):

    # Get the height of the last person (the viewer)
    viewer_height = heights[-1]

    # Initialize the counter for how many people the viewer can see
    visible_count = 0

    # Keep track of the tallest person seen so 
    # far between the viewer and the person being evaluated
    tallest_so_far = 0

    # Traverse from the second last person to the
    # front (left to right from the viewer's perspective)
    for i in range(len(heights) - 2, -1, -1):
        person_height = heights[i]

        # Rule: viewer can see this person if there's no
        # one taller than both viewer and target in between
        if person_height > tallest_so_far or viewer_height > tallest_so_far:
            # Update the tallest seen so far
            tallest_so_far = person_height

            # Check if this person is visible to the 
            # viewer (they must not be taller than the viewer)
            if person_height <= viewer_height:
                visible_count += 1

            # Also: if this person is taller than the 
            # viewer, they block further view
            # but we still count them if they're the tallest seen so far
            elif person_height > viewer_height:
                visible_count += 1

    return visible_count

        
    

面试策略 & 总结(CSOahelp 辅助):
保持眼神交流,边写边讲,主动询问边界和输入约束,展现严谨;面后 CSOahelp 会推送考察要点,助力下一轮更进一步。

连续4场的面试官都非常认可候选人,期待喜讯如期而至~

如果你也在准备Apple、Meta、TikTok等大厂的算法与系统设计面试,却不清楚如何拆题和应对各种边界,欢迎添加微信 csvohelp,即可领取北美面试求职通关秘诀。我们也有代面试,面试辅助,OA代写等服务助您早日上岸~

Leave a Reply

Your email address will not be published. Required fields are marked *