Airbnb 高频题目总结和分析

Airbnb 的面试跟很多独角兽公司(比如snapchat,uber)一样,不仅仅是把代码写出来,要求编译通过,并且可以跑通。这样其实对debug的能力有不小要求,Java的话可以用 System.out.println() 来debug。别的语言就是用自己的print 函数啦。
接下来要说的题目和出现频率数据来自面试学生的反馈,地里的帖子以及 Glassdoor。
2018年10 - 12月 期间,出现最多的店面题目是 Pour Water 和 Sliding Puzzle。
Pour Water 这题虽然 leetcode上有, 链接是 https://leetcode.com/problems/pour-water/ 。题目如下:

We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each index is 1. After V units of water fall at index K , how much water is at each index?

Water first drops at index K and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:

  • If the droplet would eventually fall by moving left, then move left.
  • Otherwise, if the droplet would eventually fall by moving right, then move right.
  • Otherwise, rise at it’s current position.
    Here, “eventually fall” means that the droplet will eventually be at a lower level if it moves in that direction. Also, “level” means the height of the terrain plus any water in that column.

We can assume there’s infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block.

Note:

  1. heights will have length in [1, 100] and contain integers in [0, 99] .
  2. V will be in range [0, 2000] .
  3. K will be in range [0, heights.length - 1] .
Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3
Output: [2,2,2,3,2,2,2]
Explanation:
#       #
#       #
##  # ###
#########
 0123456    <- index

The first drop of water lands at index K = 3:

#       #
#   w   #
##  # ###
#########
 0123456    

When moving left or right, the water can only move to the same level or a lower level.
(By level, we mean the total height of the terrain plus any water in that column.)
Since moving left will eventually make it fall, it moves left.
(A droplet "made to fall" means go to a lower height than it was at previously.)

#       #
#       #
## w# ###
#########
 0123456    

Since moving left will not make it fall, it stays in place.  The next droplet falls:

#       #
#   w   #
## w# ###
#########
 0123456  

Since the new droplet moving left will eventually make it fall, it moves left.
Notice that the droplet still preferred to move left,
even though it could move right (and moving right makes it fall quicker.)

#       #
#  w    #
## w# ###
#########
 0123456  

#       #
#       #
##ww# ###
#########
 0123456  

After those steps, the third droplet falls.
Since moving left would not eventually make it fall, it tries to move right.
Since moving right would eventually make it fall, it moves right.

#       #
#   w   #
##ww# ###
#########
 0123456  

#       #
#       #
##ww#w###
#########
 0123456  

Finally, the fourth droplet falls.
Since moving left would not eventually make it fall, it tries to move right.
Since moving right would not eventually make it fall, it stays in place:

#       #
#   w   #
##ww#w###
#########
 0123456  

The final answer is [2,2,2,3,2,2,2]:

    #    
 ####### 
 ####### 
 0123456 

还有别的例子如下:

Input: heights = [1,2,3,4], V = 2, K = 2
Output: [2,3,3,4]
Explanation:
The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height.
Input: heights = [3,1,3], V = 5, K = 1
Output: [4,4,4]

但是在真实面试中的要求其实跟 leetcode 题目并不一样。首先会要求把 terrain 和 droplet 的形状打印出来,类似下面

#       #
#       #
##ww# ###
#########

需要写一个函数把上图画出来。
接下来就是跟leetcode题目要求一致了。但是水滴的流向等需求也有变换。有时候认为左右两边是无限高的墙,水滴碰到就停在那里。有时候是假设左右两边是无限大的深渊,水滴碰到就没了 - 掉到深渊里去了。
这题难度挺大,稍微一变种代码就得改动很大。
中心解题思路应该是找 valley。逻辑主要是怎么找到真正的valley,特别是碰到一段水平线的情况下,如下图:

\
 \
  \
   ---------------
                  \
                   \
                    \
                     --    <- valley

这题在做的过程中需要不断打印形状,验证逻辑正确。跟面试官对一些假设的交流也很重要。

Sliding Puzzle 这题 leetcode也有收录, 参考 https://leetcode.com/problems/sliding-puzzle/solution/
题目如下:

On a 2x3 board , there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.

A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.

The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].

Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.

Note:

  1. heights will have length in [1, 100] and contain integers in [0, 99] .
  2. V will be in range [0, 2000] .
  3. K will be in range [0, heights.length - 1] .

例子:

Input: board = [[1,2,3],[4,0,5]]
Output: 1
Explanation: Swap the 0 and the 5 in one move.

Input: board = [[1,2,3],[5,4,0]]
Output: -1
Explanation: No number of moves will make the board solved.
Input: board = [[4,1,2],[5,0,3]]
Output: 5
Explanation: 5 is the smallest number of moves that solves the board.
An example path:
After move 0: [[4,1,2],[5,0,3]]
After move 1: [[4,1,2],[0,5,3]]
After move 2: [[0,1,2],[4,5,3]]
After move 3: [[1,0,2],[4,5,3]]
After move 4: [[1,2,0],[4,5,3]]
After move 5: [[1,2,3],[4,5,0]]
Input: board = [[3,2,4],[1,5,0]]
Output: 14

这题也是一样,需要写个函数把 puzzle 打印出来,这是leetcode 并没有要求的。有时候面试官会简化条件,默认是3 * 3的matrix。虽然这道题理论上应该用 A* Search 来做,这个算法我也研究过,但是不建议在面试中使用,因为面试官有的都不知道,而且可能表示你做过这题。用基本的 BFS 来做就可以了,虽然有可能这种做法会爆 - runtime 和 内存开销太大。 具体代码和思路,特别是OOD相关的,我会在 第七课:Airbnb 专题之OOD 中细讲,这里就不多说了。感兴趣的可以看下 OOD 与多线程 Udemy 课程

别的低频题有 airbnb电面
要猜出一四位数字,每位数字上是1到6。
提供一个API,返回有几个数字是对的(不考虑位置),以及考虑位置有几个数字是严格match的。
要求多次调用这个 API,返回这个数字究竟是多少。
例子:

# correct code: 3264
# GUESS 1111 => 0 0 (no correct digits)
# GUESS 1214 => 2 2 (digits 2 and 4 are correct and on correct position)
# GUESS 6111 => 1 0 (digit 6 is present, but on a different position)
# GUESS 6211 => 2 1 (digits 6 and 2 are present, only 2 is on correct position)

这题在onsite是挺高频的。很多人在研究算法,优化猜的次数。其实重点还在临时查找网上的socket programming 来编程,调用这个API。Airbnb起了个Server来提供这个API的。airbnb允许你自己去放狗搜,解决问题,这也是实际工作的能力考察。如果你network programming和TCP 编程很熟,那就不用查了。其实就是参考网上的例子改改。这步调通以后,才开始写算法部分。算法我就不具体讨论了。其实不需要非常优化,就不断枚举猜都行。这块并不是重点。

附送别的同学总结的2017深秋版(他家题目目前还没变)

3 Likes

楼主能否讲下socket programming在这个题里的应用?举个例子。谢谢了。

Airbnb起了个Server来提供这个API的。
你需要通过socket来发请求调用这个接口。

2019 题目统计: 统计298题,数据取自2017到今年1月全部面经,共计33类型的题目,可以分为5档,频率从高到底往下排

一档:超超超高频(超大概率遇到,甚至可能3轮全都考到)

(46) 15.4% 倒水 (1. 没有高墙 2. 打印倒水前后情况)

(46) 15.4% 所有有向图找最大最小问题(cheapest flights/10 wizards/滑雪/nodes最大得分/换着方式问) (1. 找出路径 2. 一题多解 BFS+剪枝,DFS+剪枝,Dijkstra,DP)

(40) 13.4% file system (1. implement watch function 2. Trie or HashTable)

(27) 9.1% alien dictionary (1. how to solve with DFS 2. 如何找出所有可能方案 3. What if has cycle)

二档: 高频(一档的补充,一二档基本覆盖面试百分之90情况)

(16) 5.4% sliding puzzle (1. OOP 定义board, shuffle 2. can solve/solve 3. 优化)

(13) 4.4% Palindrome Pairs (1. 先写isPanlindrome热身)

(12) 4% find the median in large file

(12) 4% 2D list iterator (1. remove 2. 如果有多层嵌套怎么处理)

(11) 3.7% menu combination/prices sum to k (1. 菜品可以/不可以重复,每个菜选一次/多次 2. 注意double精度问题)

(11) 3.7% IP to CIDR (1. 题目不难但是很难解释)

(9) 3% Boggle game (1. 直接两层DFS + Trie求解,先把代码写出来再说 2. 如果可以斜着走)

(9) 3% guess number

三档: 低频(有可能会遇到,有时间还是需要复习)

(7) 2.3% queue using fixed size array(1. 实现pop)

(7) 2.3% wiggle sort 2 (1. quick select find median就可以,不需要spaceO(1))

(6) 2% travel buddy

(5) 1.7% display page

(4) 1.3% Collatz Conjecture(1. iterative and recursion 2. hashmap来优化)

(4) 1.3% Minimum Vertices to Traverse Directed Grap

四档: 超低频(2年内偶尔出现,或者是比较新的题,数据较少)

(2) 0.7% employee interval

(2) 0.7% csv parser (移动到OA)

(2) 0.7 % bank system

(2) 0.7% Board Score(*新题 皇冠题,每个点开始DFS计算得分,类似于Number of Island)

(2) 0.7% Maximum Number a Night You Can Accommodate(house robber)/Interval Schedule Profit (带间隔的DP问题)

(2) 0.3% flip tile(*新题,类似于shut the box)

五档: (deprecated, 2年内没有出现过,或者我miss掉了,基本可以直接无视)

(0) 0% Calculator

(0) 0% Regular Expression

(0) 0% Hilbert Curve

(0) 0% Simulate Diplomacy

(0) 0% Finding Ocean

(0) 0% K Edit Distance

(0) 0% String Pyramids Transition Matrix

(0) 0% Number of Intersected Rectangle

(0) 0% echo TCP client

想要稳过线,做到以下3点:

  1. 每道题吃透,清楚每一个解法,自己在coderpad上能15分钟内写完

  2. 和面试官讨论清楚题目和假设,给出解题的思维过程, 写完后给几组test,分析复杂度

  3. 代码干净,逻辑清晰

开始写之前预留10分钟分析题目,讲题目

说句实话,他们家coding要求是真滴高,要求compile, bug free. 题目难度medium~hard,

推荐大家不要说太久,根本写不完好吧。 我把我精炼的Solution补上来了

https://res.cloudinary.com/bbs1024/raw/upload/v1562867810/src-airbnb-2656.rar

2 Likes