I recently completed the following interview:
'The robot can be programmed to start the spaces "a", "b", "c" ... "n" and it takes t a , t b , t c ... t n minutes, respectively. Once it runs to the programmed kilometers, it should be turned off for minutes "m".
After minutes "m", it can again be programmed to start for additional "a", "b", "c" ... "n" kilometers.
How would you program this robot to the exact number of kilometers in minimal time? ''
I thought this was a variation of the unlimited knapsack problem, in which the size would be the number of kilometers and the value, the time required to complete each section. The main difference is that we need to minimize, not maximize, the value. Therefore, I used the equivalent of the following solution: http://en.wikipedia.org/wiki/Knapsack_problem#Unbounded_knapsack_problem in which I choose the minimum.
Finally, since we need an exact solution (if any), over a map constructed by the algorithm for all different distances, I iterate through each and every programmed distance of the robot to find the exact distance and minimum time between those.
I think that the pause that the robot takes between runs is a small red herring, and you just need to include it in your calculations, but this does not affect the approach taken.
I am probably mistaken because I failed the test. I have no other feedback regarding the expected solution.
Edit: maybe I was not mistaken, and I am not for various reasons. I just wanted to confirm my approach to this problem.
import static com.google.common.collect.Sets.*; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Maps; public final class Robot { static final Logger logger = Logger.getLogger (Robot.class); private Set<ProgrammedRun> programmedRuns; private int pause; private int totalDistance; private Robot () {