Assuming that you start from one of the gas stations, do you have to get back to the same one or the one just before it?
–
Emmad KareemNov 15 '11 at 11:19

I forgot to say, you can only drive clockwise, and the real question is: where you need to start driving to complete a full circular path
–
Esteban VNov 15 '11 at 11:20

You have said that "You know the ammount of gas that each station has" - can each station have different amounts of gas?
–
Mark BannisterNov 15 '11 at 11:37

2

First of all, this will not necessarily be possible. In fact, you may never make it to the next station no matter where you start. Secondly, is there a limit on how much gas the car can take in?
–
JobNov 15 '11 at 16:30

2

Just be Happy you are not working for this A-Hole.
–
MoronsNov 17 '11 at 19:17

First, we look at the case where we don't have a gas tank with a maximum.

Essentially, in the first for-loop, we just drive the circle around, not caring if our fuel tank has negative fuel or not. The point of this is that no matter where you start, the difference between how much there is in your fuel tank at the start (0) and at the end is the same.

Therefore, if we end up with less fuel than we started (so less than 0), this will happen no matter where we start, and so we can't go a full circle.

If we end up with at least as much fuel as we started after going a full circle, then we search for the moment our fuel tank was at its lowest point (which is always just as we get to a gas station). If we start at this point, we will never end up with less fuel than at this point (because it is the lowest point and because we don't lose fuel if we drive a circle).

Therefore, this point is a valid solution, and in particular, there always is such a point.

Now we'll look at the version where our gas tank can hold only so much gas.

Suppose our initial test (described above) we found out it is not impossible to go the entire circle. Suppose that we start at gas station i, we tank at gas station j, but our gas tank ends up being full, so we miss out on some extra gas the station has available. Then, before we get to station k, we end up not having enough fuel, because of the gas we missed out on.

We claim that in this scenario, this will end up happening no matter where you start. Suppose we start at station l.

If l is between j and k, then we either stop (long) before we can get to station k because we started at a bad station, or we'll always have at most the amount of fuel that we had when we started at i when we try to get to k, because we passed through the same stations (and our tank was full when we passed j). Either case is bad.

If l is not between j and k, then we either stop (long) before we get to j, or we arrive at j with at most a full tank, which means that we won't make it to k either. Either case is bad.

This means that if we make a round starting at a lowest point just like in the case with the infinitely large gas tank, then we either succeed, or we fail because our gas tank was too small, but that means that we will fail no matter which station we pick first, which means that the instance has no solution.

Is it "Perfect"? In the real world, a car fuel tank cannot hold all the gas a station has. Although this answer solves the stated problem, it is unlikely to provide a useable real world solution. These interview questions are designed to test this kind of software developer skill. i.e. Do you blindly code to the requirements, or do you ask questions and clarify when the requirements appear incorrect or inconsistent with the real world.
–
mattnzNov 16 '11 at 22:04

3

@mattnz: on one hand, you're right: as a software engineer, you should always make sure your requirements are clear and appropriate for the situation. However, this question is clearly not a real-life situation: why'd we want to drive in a circle on this road? Why can we start at any station we like? Does an airplane drop us off at a station? Either the question was purely to test algorithmic skills, or the question is a reformulation of an actual question, and in the actual question the equivalent of the gas tank has unlimited capacity. Or at least, that's how I look at it.
–
Alex ten BrinkNov 16 '11 at 23:16

Indeed, it's a correct answer to the question as asked, and presumably as intended. However, now I'm curious to know if the solution does work if the car can only hold a max, or if not, if there's an alternative that would.
–
Jack V.Nov 17 '11 at 16:46

@JackV.: see my update for a modified algorithm that also handles the case where the gas tank isn't infinitely large.
–
Alex ten BrinkNov 17 '11 at 19:10

My first response would be "The requirements appear to be incomplete, are you sure you want me to write the code based on these requirements alone, shall we make them up as we go, or would you like my help completing the requirements before we design the software?".

The incomplete requires are, as stated in other questions,
Fuel capacity of the car.
End point - is it the start point or the station before the start point. (I think you answer this in your comment "complete a full circle implies end = start, in that case. I would clarify this as it is not explicit)
The question implies there is one, and only one solution, is this correct.

Other issues
Is performance of the algorithm a concern, or put another way, what is the budget for this project.

It's a good question - you cannot answer it in it's current form and be certain you have provided the customer with the solution to the problem he is expecting.

I feel like given quiz is missing some vital parts. However, in my reasoning I would use following assumptions:

Car tank have unlimited fuel capacity

It's possible to travel clockwise only

Car starts with 0 gas

Car should finish in the same gas station

Given this assumptions, the task can be easily brute forced with O(n**2) complexity, by trying all gas stations.

If it's required more robust solution, it's possible to transform this quiz to maximum continuous sub-sequence problem with extra constrains, that it should't go below zero. The complexity of this algorithm is O(n) - linear.

For first step, lets transform our initial problem to problem that is solvable by maximum sub-sequence by summing amount of gas at gas station and cost of going to that station.

Second step is to apply maximum sub-sequence algorithm. The algorithm is based on 2 assumptions:

if gas counter went below 0, that is always better to restart from current gas station

otherwise it's better just to fill-in the tank, since it's more than 0 gas here

Applying this algorithm to the circled list of the stations 2 times (2 overlapping sequences), we will get the station that starts sequence which accumulates most of the fuel.

Third step is to verify that goal is reachable with given amount of fuel and given start station, complexity o(n)

+1, for a good algorithm and reference.
–
Emmad KareemNov 16 '11 at 0:21

given that it is an interview question not specifying all the requirements may have been by design i.e. it may test the candidates ability to clarify requirements by asking questions
–
jk.Mar 12 '12 at 10:46