彭博面试题:生成包含通配符的二进制串的所有可能字符串

59次阅读
没有评论

Bloomberg Interview Question: Generate All Binary Strings from Wildcards

彭博面试题:生成包含通配符的二进制串的所有可能字符串

Implement a procedure that will print out all permutations of a given binary string 
that may or may not contain one or more "wildcards".

Consider a "binary string" such as:
"0110"

Now imagine we swap a character in this string for a "wildcard" (represented by "?")
"01?0"

For the purposes of this question, we say a "wildcard" evaluates to one of two possible 
values: "0" or "1".

If your method is passed a binary string with one or more wildcards, it should print out 
all possible permutations of that string with the wildcard.

e.g.
"0?1" --> "001" "011"

这道题要求将一个可能包含多个 ? 的二进制字符串展开,? 可以变成 01,需要打印出所有组合。典型做法是用 DFS 或回溯,每遇到一个通配符就产生两条分支。


Bloomberg Interview Question: Maximum Travel Distance on a Circular Moon Route

彭博面试题:计算探测车在环形轨道上能跑的最远距离

You wish to send a research rover to survey the equator of a small moon.
Fuel has been dropped along the desired route by previous missions, and you can choose 
to drop your rover at any starting point on the route.

Each unit of fuel can move the rover 1 mile.
Given the location and volume of each fuel drop as well as the circumference of the moon, 
find the farthest distance your rover can travel before running out of fuel.

The rover can only travel in one direction (east).

The input consists of tuples (distance east from first fuel drop, amount of fuel) 
and the circumference.

Input:
fuel_drops = [(0, 20), (20, 10), (60, 40)]
circumference = 100

Expected output: 70

# Explanation:
# Start at 60  (location: 60, fuel reserves: 40)
# Drive for 40  (location: 100|0, fuel reserves: 0)
# Refuel at 0   (location: 100|0, fuel reserves: 20)
# Drive for 20  (location: 20, fuel reserves: 0)
# Refuel at 20  (location: 20, fuel reserves: 10)
# Drive for 10  (location: 30, fuel reserves: 0)

这道题是环形路线的“加油站”变形:探测车可以选择任何 fuel drop 作为起点,向东行驶,消耗油量,到站点会获得额外燃料。要计算从哪个起点出发能跑得最远,总行驶距离最大。它与经典的 Gas Station Problem 类似,但要求输出最大可达距离。

正文完
 0