Question asked here to write the code for, is like given below & same question can also been seen at - https://www.gohired.in/2014/07/11/n-petrol-bunks-or-city-arranged-in-circle-you-have-fuel-and-distance-between-petrol-bunks-is-it-possible-to-find-starting-point-so-that-we-can-travel-all-petrol-bunks/
There are n petrol bunks arranged in circle. Each bunk is separated from the rest by a certain distance. You choose some mode of travel which needs 1litre of petrol to cover 1km distance. You can’t infinitely draw any amount of petrol from each bunk as each bunk has some limited petrol only. But you know that the sum of litres of petrol in all the bunks is equal to the distance to be covered. ie let P1, P2, … Pn be n bunks arranged circularly. d1 is distance between p1 and p2, d2 is distance between p2 and p3. dn is distance between pn and p1.Now find out the bunk from where the travel can be started such that your mode of travel never runs out of fuel.
Below I have given kind of Brute-force approach which you can further improve using memoization & I am not sure how much performance improvement can achieved via this, but at least to show your intelligence & satisfy the interviewer, you can explain memoization approach also.
For me this interview was taken by the company's VP(Neeraj Agrawal), but still if you get the chance to ask below question then ask to check your interviewer's views, as most these people just pick the question from internet & check the answers to ask in interviews -
Question : Check whether you need to move clockwise or anti-clockwise in the circle, as answer will be dependent on that.
There are n petrol bunks arranged in circle. Each bunk is separated from the rest by a certain distance. You choose some mode of travel which needs 1litre of petrol to cover 1km distance. You can’t infinitely draw any amount of petrol from each bunk as each bunk has some limited petrol only. But you know that the sum of litres of petrol in all the bunks is equal to the distance to be covered. ie let P1, P2, … Pn be n bunks arranged circularly. d1 is distance between p1 and p2, d2 is distance between p2 and p3. dn is distance between pn and p1.Now find out the bunk from where the travel can be started such that your mode of travel never runs out of fuel.
Below I have given kind of Brute-force approach which you can further improve using memoization & I am not sure how much performance improvement can achieved via this, but at least to show your intelligence & satisfy the interviewer, you can explain memoization approach also.
For me this interview was taken by the company's VP(Neeraj Agrawal), but still if you get the chance to ask below question then ask to check your interviewer's views, as most these people just pick the question from internet & check the answers to ask in interviews -
Question : Check whether you need to move clockwise or anti-clockwise in the circle, as answer will be dependent on that.
Tide
If you are having a big list of petrol pumps in that circle then it is better to use memoization or even better if use multithreading. If anyone needs multithreaded approach for both clockwise & anticlockwise then leave the message, but again multitreading comes with a cost, so if you have big list then only go for multithreading else it will only cause negative impact.