We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

- Prepare
- Data Structures
- Queues
- Truck Tour
- Discussions

# Truck Tour

# Truck Tour

#### Sort by

recency

#### |

#### 358 Discussions

#### |

Please Login in order to post a comment

Who and how decides which question is how difficult? This question is definitely easy but it was marked as difficult :D

If you're like me, you're very confused by solutions that only explore the list up to station

N-1after they find a viable starting station at positionk, and don't "finish the circle" by checking the road fromN-1back tok. What makes it worse is that most solutions on YouTube just say "it's logical/obvious" and don't explain anything.Let me explain it now that I finally understood.You have to keep in mind, the problem implicitly states there always exists a solution, since no return value is specified for when a solution doesn't exist. Assume we found a valid starting station at position

k. This means that the fuel you will have at positionk+1when starting fromkis greater or equal to zero. Ok, let's then assume we are able to reach stationN-1. At this point, simple solutions to this problem will returnk. Here's why this is a correct solution:Assume, for this starting station

k, that we can't "finish the circle" (i.e. go fromN-1back to the starting station). Ok, then assume that you move up your starting station tok+1and assume you can also reach the end of the circle. Then, by construction,you won't be able to finish the circle either.This is because when starting atkand moving tok+1, you know you have a positive or zero fuel balance. So if you were unable to finish the circle starting atk, you won't be able to do the trip fromN-1tokeither when you start atk+1. Inductively, this implies you can't finish the circle for any station after stationkeither. Since you're finding the first station that makes the full trip, you also know that any station beforekwasn't a valid starting station. So, there would be no solution, which goes against the problem statement.So, in a convoluted way, reaching the end of the circle from a particular starting station implies you can get from the end back to this starting station.

The logic for understanding why, when you run out of fuel at station

iwhen starting from some previous stationk, you can just move your starting station toi+1is similar. You know the trip fromktok+1finished with non-negative fuel, so any other trip from stations bigger thankup toiwill also necessarily run out of fuel (they will have, at most, the same fuel available as when starting fromk).Hope this helps!

Cpp Solution:

Here is problem solution - HackerRank Truck Tour problem solution in PYthon Java c++ c and javascript

Insanely broken exercise. Terrible test cases where every test has 1000 or more elements making it impossible to learn anything from the failure and it does not actually expect you to do a tour of the circle. In fact, if you try to you will fail. All the successful solutions only go clockwise and don't try to reach all the petrol pumps, they just go to the end of the array. It's shocking that this made it into the 1-week prep course and implies the rest of the HackerRank exercise library is even worse.