彭博面试真题 – 一亩三分地 – Bloomberg – 面试辅助 – 代面试

62次阅读
没有评论

彭博面试真题:设计支持双端操作且 O(1) 时间的队列

Implement a queue which has "addfront, addback, popfront, popback, getSize" in O(1)

这道彭博数据结构题要求你实现一个类似双端队列的结构,提供 addfront / addback / popfront / popback / getSize 都是 O(1) 时间。典型做法是用双向链表或循环数组,维护 head、tail 指针以及当前元素个数,每个操作只改动常数个指针,从而保证时间复杂度为常数级。

Bloomberg Interview Question: Minimum Letter Changes to Make Two Words Anagrams
彭博面试真题:最少改几个字母才能把一个单词变成另一个的变位词

Find out how close two words are to being anagrams of each other. A string s1 is an anagram of another string s2 if the same characters exist in both s1 and s2 in any order.

Write a function which accepts two strings, and returns the minimum number of letters that must be changed to make a word an anagram of another?

For example, to make 'bond' an anagram of 'down' you need to change 1 letter: 'b' to 'w'.

If either string contains a number or the strings are different lengths, throw an exception.

这道题把“接近变位词”的程度定义为:把一个单词改成另一个单词的 anagram 所需最少字母替换次数。需要先检查两个字符串是否等长且只含字母,然后统计每个字符在两边的出现次数,通过比较频次差来计算需要替换的总数,从而得到最小修改次数。

彭博面试真题:小车在沙漠中能否在油耗尽前开到绿洲

# Implement a ride(desert, gas) method that returns true if a car can reach an oasis
# before it runs out of gas and false otherwise. The car can drive in four directions:
# top, bottom, left, right. Moving one field requires one unit of gas (used gas is an
# equivalent of the number of steps taken).

# The desert is a 2D m x n array with five types of fields:

# 'c' - the starting point of the car
# 'o' - the oasis, our destination
# '.' - sand, the car can drive through it

desert = [
    ['.', '.', '.', '.', '.', '.', '.', 'o'],
    ['.', '.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.', 'c'],
]

# ride(desert, 3) => false
# ride(desert, 5) => true

这道题要判断一辆车从起点 ‘c’ 出发,在最多 gas 步之内能不能走到绿洲 ‘o’。地图是二维数组,小车每一步可以上下左右移动一格、只能踩在 ‘.’ 或终点上。常见解法是用 BFS 或 Dijkstra 计算从起点到绿洲的最短步数,如果最短距离不超过给定油量就返回 true,否则返回 false。

Bloomberg Interview Question: Substrings in the Most Deeply Nested Brackets

Given a string that may contain brackets, and no unbalanced brackets,
find the substring(s) within the most deeply nested balanced bracket(s).
The following sets of characters should be considered as open/close brackets respectively: (), [], { }
If there are multiple sets of brackets with the same highest depth, your function should return all substrings.
If there are no brackets in the string, then your function should return the entire input string.

"ab(c(d)e)"        -> "d"
"[a{b}c]d(e)]"     -> "b"
"((a)b(cd)ef)"     -> "a", "cd"
"(ab[]c){d{e}}"    -> "","e""Hello, World!"    -> "Hello, World!"

这道题要求在仅含平衡括号的字符串中,找出位于“最大嵌套深度”处的所有子串。括号包括三种类型:圆括号、小括号和花括号。常见做法是顺序遍历字符串,用计数或栈记录当前深度,深度每加一层就记下起始位置,一旦发现新的最大深度就清空结果并重新记录;遍历结束后把这些区间对应的子串全部返回。若根本没有括号,则直接返回整条原始字符串。

正文完
 0