← 返回 snowflake 的题目列表Reach Target via Add / Double / Halve
类型:qbank
Given add(n)=n+2, dub(n)=n*2, split(n)=floor(n/2), implement func(a, b) returning one (not necessarily optimal) sequence of operations that takes a to b. BFS finds a shortest path, but the interviewer pushed for the simplest while-loop construction.
Problem Overview
You are given three operations on a positive integer:
add(num) → num + 2
dub(num) → num * 2
split(num) → floor(num / 2)
All inputs and outputs are positive integers. Implement func(a, b) that returns one possible sequence of operations transforming a into b. The path does not need to be optimal.
Notes
A BFS over reachable values yields a shortest path, but the interviewer here explicitly wanted the simplest construction — e.g. split down toward a small value and then add / dub back up to b in a plain while loop, rather than a search.
No concrete (a, b) example was published; clarify whether any valid path is acceptable and whether intermediate values are bounded before coding.